Sharing JSHint Settings in Grunt

Comments

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
{
  "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:

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

Comments