Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript code style enforcer or checker

I'm working on a project that uses a specific styleguide for javascript. For instance, an if/else statement would look like this:

if( condition ){
  // Bla bla
}
else {
  // Another bla bla
}

What I'm looking for is a tool that would allow me to check the syntax of a file according to a specific styleguide. JSHint/JSLint don't focus on style, Closure Linter is not customizable, and Uncrustify reformats the file without warning (and doesn't officially support Javascript).

The best output I could get with this program would be the one from Closure Linter but with custom rules. Answers to this similar question were all wrong.

Does such a tool exist?

like image 613
ldiqual Avatar asked Apr 16 '13 07:04

ldiqual


2 Answers

If you're still looking for an answer, you could try a project called jscs.
You can customize how your code should look like in almost any aspect, and those which you can't yet are being developed (function argument spacing, for example).

Everything is customizable via a .jscsrc file; the following is an example of one of my projects using it:

{
    "requireCurlyBraces":                   [ "if", "else", "for", "while", "do", "switch" ],
    "requireSpaceAfterKeywords":            [ "if", "else", "for", "while", "do", "switch" ],
    "disallowSpaceAfterKeywords":           [],
    "requireSpacesInsideObjectBrackets":    "all",
    "disallowSpaceAfterObjectKeys":         true,
    "disallowImplicitTypeConversion":       [],
    "disallowKeywords":                     [ "with" ],
    // ...
}

If you're using Grunt, feel free to try my own task for jscs - which is grunt-jscs-checker.

BTW, the jQuery team is using jscs :)

like image 115
gustavohenke Avatar answered Oct 06 '22 02:10

gustavohenke


It sounds like you're really after a JavaScript beautifier. Checking can be done via "run it over the files and see if they change" backed by version control.

like image 29
Matthew Strawbridge Avatar answered Oct 06 '22 02:10

Matthew Strawbridge