Google 很重視代碼風格的一致性,而且還公開過一份 JavaScript 代碼風格指南: Google JavaScript Style Guide,現(xiàn)在它們又發(fā)布了一個工具來幫助你檢查 JavaScript 代碼是否嚴格遵循了 Google JavaScript Style Guide :Closure Linter。
假設你有以下代碼:
var x = 10
var y=20;
for(var i = 0;i < 10; i++ ) {
x += i;
y -= i;
}
var z = [10, 20,];
x = y + z[0]
+ 10;
在命令行下運行 gjslint –strict fixme.js 之后,就會出現(xiàn)下面的結果:
Line 1, E:0010: (New error) Missing semicolon at end of line
Line 2, E:0002: Missing space before "="
Line 2, E:0002: Missing space after "="
Line 4, E:0002: Missing space before "("
Line 4, E:0002: Missing space after ";" in for statement
Line 4, E:0001: Extra space before ")"
Line 6, E:0006: (New error) Wrong indentation: expected any of {2} but got 3
Line 9, E:0121: Illegal comma at end of array literal
Line 12, E:0120: Binary operator should go on previous line "+"
Found 9 errors, including 2 new errors, in 1 files (0 files OK).
這個工具甚至可以幫你自動修復一些錯誤,運行 fixjsstyle –strict fixme.js 即可。(注意:不一定可以修復全部錯誤)
下載地址:Closure Linter。