2014-12-03 24 views
12

目標:對待使用@author作爲代碼風格違反

問題的情況下,警告的@author標籤在任何地方使用該項目的.js內的文件。

問:

有什麼事情jshint或其他靜態代碼檢查工具可以幫助?如果不是,我有什麼選擇?

說明:

我完全在Javadoc @author tag good practices螺紋與保羅的答案達成一致,並把@author標記爲不必要的噪音。

而且,在Python世界中,我看到有人檢查標籤的使用情況。例如,Openstack Style Guidelines明確表示不使用@author標記。他們已經開發出了一套定製的flake8檢查,其中包括:

[H105] Don’t use author tags. 

現在,我試圖解決在JavaScript中同樣的問題。

實例(這不應該傳遞一個代碼質量檢查):

/** 
* @author John Smith <[email protected]> 
*/ 

'use strict'; 
+1

不,jshint不能這樣做。只要在尋找'@ author'的源代碼上做一個grep。如果你想要,你可以把它放在一個git pre-commit hook中。或者,如果遇到'@ author',創建文檔時可能會導致JSDoc錯誤。 – 2014-12-03 16:54:20

+0

@torazaburo感謝您的有用評論,它實際上可以是一個合法的答案。 – alecxe 2014-12-03 17:08:29

回答

7

沒有,jshint不能做到這一點。只要在查找@author的源代碼中進行grep即可。如果你想要,你可以把它放在一個git pre-commit hook中。或者,如果遇到@author,創建文檔時可能會使JSDoc錯誤。

5

對不起,我想在發佈答案之前嘗試一下,但賞金的差不多了。 ; ^)

This answer聲稱有一種方法可以編寫自己的JSHint模塊。

讓我們假設它像宣傳的那樣,並已重新合併。

Great instructions here但要注意,這些都對「jshint-未來」的網站。從該頁面

示例代碼:

// This module errs on any identifier that doesn't starts with 'kitty'. 
function myModule(linter) { 
    linter.on("Identifier", function (ident) { 
    if (ident.name && ident.name.slice(0, 5) !== "kitty") 
     linter.report.addError("C001", "More cats please."); 
    }); 
} 

下面是關於如何建立一個棉短絨初始部分:

var Linter = require("jshint").Linter; 
var code = "<your beautiful JavaScript code here>"; 

// Create a new instance of Linter. 
var linter = new Linter(code); 

// Now you can teach JSHint about your predefined variables. 
// Note that default JavaScript identifiers are already there. 
linter.addGlobals({ 
    jQuery: false, 
    MyPlugin: true 
}); 

// If you have any JSHint extensions, you can attach them 
// to the current instance. 
linter.addModule(myModule); 

// Finally, parse your code. 
linter.parse(); 

我知道這是非常通用的(你仍然需要研究linter.on選項超出Identifier;也有String,例如),但它看起來很有前途。再次,您可以看到如何使用說明above進行集成。看起來這是used in style.js的格式。我有不是試過這個呢。只是沒有時間在家;道歉。

是否有特定的原因torazaburo的「只需grep它」的答案不起作用?您是否需要將其作爲代碼質量工作流程的一部分?如果是這樣,這個「寫你自己的模塊」似乎是要走的路。

如果你喜歡它,也有非常明顯的方法來破解JSLint,但我不確定克羅克福德會讚賞。 ; ^)

+0

哈,當然獎金*被授予*我正在寫這個答案。這就是我得到的。 – ruffin 2014-12-18 15:54:37

+0

:)不要擔心 - 我會試試看,並回復給你。非常感謝你。 (在今天的upvotes中,但絕對值得一) – alecxe 2014-12-18 15:55:34

+0

僅供參考,我已經發布了我如何用ESLint解決它(注意簡單性)。無論如何,賞金都會帶給你。感謝您關注此事。 – alecxe 2014-12-26 23:54:28

4

ESLint package解決 - 這是一個可插入的用於JavaScript的linting實用程序。

創建一個custom rule(注是多麼簡單),並將其保存到rules/no-author.js

/** 
* @fileoverview A rule to disallow @author tag in code 
*/ 

module.exports = function (context) { 
    "use strict"; 
    function checkComment(node) { 
     var commentToCheck = node.value.toLowerCase().trim(); 

     if (commentToCheck.indexOf("@author") !== -1) { 
      context.report(node, "A comment unexpectedly contains @author."); 
     } 
    } 

    return { 
     "BlockComment": checkComment, 
     "LineComment": checkComment 
    }; 
}; 

現在,假設我有一個test.js文件違反了使用@author標籤:

/** 
* @author John Smith <[email protected]> 
*/ 

然後看看這個規則是如何應用的:

$ eslint test.js --rulesdir=rules/ --rule='no-author: 2' 

test.js 
    1:0 error A comment unexpectedly contains @author no-author 

✖ 1 problem 

僅供參考,no-author: 2此處means將規則打開爲錯誤(觸發時退出代碼爲1)。