JavaScript代码风格检查器:node-jscs

jopen 11年前

node-jscs 是一个 JavaScript 的代码风格检查工具。可在 Node.js 和浏览器中使用,在浏览器中使用的方法如下:

<script type="text/javascript" src="jscs-browser.js"></script>  <script type="text/javascript">  var checker = new JscsStringChecker();  checker.registerDefaultRules();  checker.configure({disallowMultipleVarDecl: true});  var errors = checker.checkString('var x, y = 1;');  errors.getErrorList().forEach(function(error) {      console.log(errors.explainError(error));  });  </script>

安装

jscs可以使用npm安装

npm install jscs

To run jscs, you can use the following command from the project root:

./node_modules/.bin/jscs path[ path[...]]

配置

jscs is configured using .jscs.json file, located in the project root.

requireCurlyBraces

Requires curly braces after statements.

Type: Array

Values: Arrow of quoted keywords

Example

"requireCurlyBraces": [      "if",      "else",      "for",      "while",      "do",      "try",      "catch",      "case",      "default"  ]
Valid
if (x) {     x++; } 
Invalid
if (x) x++; 

requireSpaceAfterKeywords

Requires space after keyword.

Type: Array

Values: Array of quoted keywords

Example

"requireSpaceAfterKeywords": [      "if",      "else",      "for",      "while",      "do",      "switch",      "return",      "try",      "catch"  ]
Valid
return true; 
Invalid
if(x) {     x++; } 

项目主页:http://www.open-open.com/lib/view/home/1389579390055