Tuesday, August 31, 2010 | 3:45 PM

When collaborating with lots of engineers as we do here at Google, it’s important to keep a consistent coding style. To this end, we recently open sourced the Google JavaScript Style Guide.



Today we’re happy to announce we’re open sourcing a tool that will help you follow that style guide with minimal manual effort - the Closure Linter.



Take for example, this code:





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

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

Posted by Robby Walker and Andy Perelson, Software Engineers

When we runwe getEven better, if we run, 7 of the 9 errors are automatically fixed for us!We hope you can use the Closure Linter to improve your style consistency while simultaneously saving time. More information on how to get started can be found on the How to Use Closure Linter page.We welcome comments and questions in the discussion forum