8

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?

Community
  • 1
  • 1
ldiqual
  • 15,015
  • 6
  • 52
  • 90

3 Answers3

10

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 :)

Mike Sherov
  • 13,277
  • 8
  • 41
  • 62
gustavohenke
  • 40,997
  • 14
  • 121
  • 129
1

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.

Community
  • 1
  • 1
Matthew Strawbridge
  • 19,940
  • 10
  • 72
  • 93
0

There is no tool matching your desires. JSLint is applying the style rules Crockford advices (and enforces), JSHint is more flexible, in that you can choose the rules that better suits your coding style, but is not flexible enough to enforce any kind of style.

Usually, you configure your editor so it helps you indent and do the newlines the same way you like your code to be. And use the linter to watch out where it matters (some whitespacing rules, how to use parens…). The role of the linter is more a guard against stupid mistakes from which your language does not prevent you.

zmo
  • 24,463
  • 4
  • 54
  • 90