2014-12-03 120 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)。