Sharing JSHint settings in Grunt

grunt
This post is too old to be of any use. It has been archived.

JSHint settings can get quite long and clog a Gruntfile. Luckily it is fairly simple to move them to their own file.

Create a JSON file with JSHint settings

Just copy your standard .jshintrc file, or create one (a good .jshintrc file to use as a starting point can be found on GitHub). Wrap the options in an object, and save the file somewhere sensible.

I save the file as JSON, which strictly speaking, means it has no comments (although grunt doesn't complaing if it does). Here's my standard .jshintrc

{
  "options": {
    "bitwise": true,
    "browser": true,
    "camelcase": true,
    "curly": true,
    "eqeqeq": true,
    "forin": true,
    "immed": true,
    "indent": 2,
    "latedef": true,
    "newcap": true,
    "noarg": true,
    "noempty": true,
    "nonew": true,
    "plusplus": true,
    "quotmark": "double",
    "undef": true,
    "unused": "false",
    "strict": true,
    "trailing": true,
    "maxparams": 4,
    "maxdepth": 4,
    "maxerr": 500,
    "maxlen": 80,
    "asi": false,
    "boss": false,
    "debug": false,
    "eqnull": true,
    "evil": false,
    "expr": true,
    "funcscope": false,
    "globalstrict": false,
    "iterator": false,
    "lastsemic": false,
    "laxbreak": false,
    "laxcomma": true,
    "loopfunc": false,
    "multistr": true,
    "proto": false,
    "scripturl": false,
    "smarttabs": false,
    "shadow": false,
    "sub": false,
    "supernew": true,
    "nomen": true,
    "onevar": true,
    "white": false,
    "globals": {
      "$": true,
      "afterEach": true,
      "beforeEach": true,
      "describe": true,
      "expect": true,
      "getJSONFixture": true,
      "it": true,
      "jasmine": true,
      "jQuery": true,
      "sinon": true
    }
  }
}

Use require to include the settings into the Gruntfile

The rest is easy - as grunt is node.js, just use require to import the settings:

jshint: {
  options: require( "./jshint.json" ).options,
  all: ['Gruntfile.js', 'lib/**/*.js', 'test/**/*.js']
}