Skip to content
This repository has been archived by the owner on Apr 3, 2019. It is now read-only.

Style guide and automatic enforcement of it #193

Closed
ckarlof opened this issue Jan 8, 2014 · 9 comments
Closed

Style guide and automatic enforcement of it #193

ckarlof opened this issue Jan 8, 2014 · 9 comments

Comments

@ckarlof
Copy link
Contributor

ckarlof commented Jan 8, 2014

Studies suggest code style consistency makes code easier to read and developers more productive. I'd like a style guide and an automatic way to enforce it, when possible via jshint, jscs (jscs-dev/node-jscs#102), or whatever.

Mozilla Webdev style guide: http://mozweb.readthedocs.org/en/latest/js-style.html#js-style

@ckarlof
Copy link
Contributor Author

ckarlof commented Jan 8, 2014

It would also be nice if it was as similar as possible to the style guide for fxa-auth-server (mozilla/fxa-auth-server#472).

@nchapman
Copy link
Contributor

nchapman commented Jan 8, 2014

We do have the beginnings of this going with the jshint grunt task: grunt jshint. We just need to settle on the proper style rules. I believe what we're using now is pretty much what came with Yeoman.

@shane-tomlinson
Copy link

I have added a pre-commit hook to do the hard work before every commit.

The following pre-commit hook, when added to fxa-content-server/.git/hooks/pre-commit, will run jshint before commit:

#!/bin/sh

exec grunt jshint

@shane-tomlinson
Copy link

Link to #174

@pdehaan
Copy link
Contributor

pdehaan commented Jan 8, 2014

Another option is ESLint nzakas/eslint which is fairly configurable and a bit easier to customize warnings/errors. Also has support for Grunt and other stuff.

@pdehaan
Copy link
Contributor

pdehaan commented Jan 10, 2014

I came up with the following .jscs.json config file, but I don't see any setting for ASI, so we still may have to use JSHint Grunt task to fill in some gaps in the Mozilla Style Guide:

{
    "disallowKeywords": ["with", "eval"],
    "disallowKeywordsOnNewLine": ["else"],
    "disallowLeftStickedOperators": ["?", "-", "/", "*", "=", "==", "===", "!=", "!==", ">", ">=", "<", "<="],
    "disallowMultipleLineStrings": true,
    "disallowRightStickedOperators": ["?", "/", "*", ":", "=", "==", "===", "!=", "!==", ">", ">=", "<", "<="],
    "disallowSpaceAfterObjectKeys": true,
    "disallowSpaceAfterPrefixUnaryOperators": ["++", "--", "+", "-"],
    "disallowSpaceBeforePostfixUnaryOperators": ["++", "--"],
    "maximumLineLength": 120,
    "requireCapitalizedConstructors": true,
    "requireCurlyBraces": ["if", "else", "for", "while", "do"],
    "requireLeftStickedOperators": [","],
    "requireLineFeedAtFileEnd": true,
    "requireRightStickedOperators": ["!"],
    "requireSpaceAfterKeywords": ["if", "else", "for", "while", "do", "switch", "return"],
    "requireSpaceBeforeBinaryOperators": ["+", "-", "/", "*", "=", "==", "===", "!=", "!=="],
    "validateLineBreaks": "LF",
    "validateQuoteMarks": true,
    "validateJSDoc": {
        "checkParamNames": true,
        "checkRedundantParams": true,
        "requireParamTypes": true
    }
}

You can test this locally by installing jscs [1.2.0] globally and running $ jscs */.js and you should see about 4 errors/warnings:

$ jscs **/*.js
If statement without curly braces at scripts/run_locally.js :
    26 |  fxaccntbridge.on('exit', function(code, signal) {
    27 |    console.log('fxa-content-server killed, existing');
    28 |    if (done) done(code);
------------^
    29 |    else process.exit(code);
    30 |  });

Else statement without curly braces at scripts/run_locally.js :
    26 |  fxaccntbridge.on('exit', function(code, signal) {
    27 |    console.log('fxa-content-server killed, existing');
    28 |    if (done) done(code);
------------^
    29 |    else process.exit(code);
    30 |  });

If statement without curly braces at scripts/run_locally.js :
    33 |// only start the server if the file is called directly, otherwise wait until
    34 |// module.exports is called.
    35 |if (process.argv[1] === __filename) module.exports();
--------^
    36 |
    37 |

Keyword `else` should not be placed on new line at scripts/run_locally.js :
    27 |    console.log('fxa-content-server killed, existing');
    28 |    if (done) done(code);
    29 |    else process.exit(code);
------------^
    30 |  });
    31 |};

4 code style errors found.

Note that this config file won't work with the grunt-jscs-checker package until they update the jscs dependency from 1.1.0 to 1.2.0 (I have a pending PR). Once that gets merged I can submit a PR to wire up the jscs Grunt task.

Also note that when/if we make changes to this config file, it should be copied to the other 2-3 fxa-* repos to keep things consistent.

@shane-tomlinson
Copy link

I suggest we add a run of whichever linter we decide on to the travis build to catch errors like #219.

@pdehaan
Copy link
Contributor

pdehaan commented Jan 14, 2014

Initial commit for this in PR #226

@pdehaan
Copy link
Contributor

pdehaan commented Mar 24, 2014

I think this was fixed a long time ago. Closing.

@pdehaan pdehaan closed this as completed Mar 24, 2014
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants