簡體   English   中英

如何使用JSDoc記錄CoffeeScript源代碼?

[英]How to document CoffeeScript source code with JSDoc?

我有一些用CoffeeScript編寫的代碼,我想用Google Closure Compiler優化生成的JavaScript,所以這些文件需要用JSDoc記錄。

我的問題是,如何記錄* .coffee文件以生成包含用於閉包編譯器的工作JSDoc的javascript?

還有一個問題:有沒有辦法在* .coffee中保留單行注釋?

CoffeeScript輸入:

### define function variable before block to avoid code being appended to closing part of JSDoc comment ###
cube = null

###*
 * Function to calculate cube of input
 * @param {number} Number to operate on
 * @return {number} Cube of input
 ###

cube = (x) -> x*x*x

Windows cmd提示的JavaScript輸出: coffee -cpb src.coffee

// Generated by CoffeeScript 1.6.3
/* define function variable before block to avoid code being appended to closing part of JSDoc comment*/

var cube;

cube = null;

/**
 * Function to calculate cube of input
 * @param {number} Number to operate on
 * @return {number} Cube of input
*/

cube = function(x) {
  return x * x * x;
};

編輯

正如其他答案中詳述的那樣,CoffeeScript 1.7.1有更好的方法可以解決這個問題。

由於我不能直接回復上面的Billy,似乎CoffeeScript 1.7.1對此有更好的支持:

###*
# Sets the language and redraws the UI.
# @param {object} data Object with `language` property
# @param {string} data.language Language code
###

handleLanguageSet: (data) ->

輸出

/**
 * Sets the language and redraws the UI.
 * @param {object} data Object with `language` property
 * @param {string} data.language Language code
 */
handleLanguageSet: function(data) {}

你必須進行實驗(很多),但###評論是你的朋友。

咖啡腳本編譯器將保留使用###表單的注釋( 此處為 docs)。

我嘗試使用網站上的'try coffeescript'功能為函數創建一個非常簡單的JsDoc片段:

###* Doc for this function.###
foo = -> 'bar'

這給了:

/** Doc for this function.
*/
var foo;
foo = function() {
   return 'bar';
 };

我不是JsDoc專家,但我猜的是var foo; 上面的函數聲明會產生問題。 如果你之前宣布過foo ,也許..

聽聽它是怎么回事真好。

我建議不要這樣做。 JSDocing您的所有代碼是一個費力的過程,可能從Closure Compiler幾乎沒有任何好處。 在Google本身之外,幾乎沒有人這樣做。 CoffeeScripters / JavaScripters通常更喜歡像docco這樣的輕量級文檔工具。

此外,雖然Closure Compiler背后有Google品牌名稱,但UglifyJS在許多情況下已被證明是更有效的縮小工具。 (jQuery 最近切換到它。)

還有一個問題:有沒有辦法在* .coffee中保留單行注釋?

是:

### foo ###

要么

`// foo`

class有問題

###* this is a class ###
class hello
    v: 4

給出了

// Generated by CoffeeScript 2.0.0-beta5
/** this is a class */
var hello;

hello = (function() {
  class hello {};

  hello.prototype.v = 4;

  return hello;

})();

它在JSDoc中無效

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM