source: Dev/trunk/node_modules/grunt-contrib-jshint/docs/jshint-examples.md @ 484

Last change on this file since 484 was 484, checked in by hendrikvanantwerpen, 11 years ago

Commit node_modules, to make checkouts and builds more deterministic.

File size: 2.9 KB
Line 
1# Usage examples
2
3## Wildcards
4In this example, running `grunt jshint:all` (or `grunt jshint` because `jshint` is a [multi task](http://gruntjs.com/configuring-tasks#task-configuration-and-targets)) will lint the project's Gruntfile as well as all JavaScript files in the `lib` and `test` directories and their subdirectores, using the default JSHint options.
5
6```js
7// Project configuration.
8grunt.initConfig({
9  jshint: {
10    all: ['Gruntfile.js', 'lib/**/*.js', 'test/**/*.js']
11  }
12});
13```
14
15## Linting before and after concatenating
16In this example, running `grunt jshint` will lint both the "beforeconcat" set and "afterconcat" sets of files. This is not ideal, because `dist/output.js` may get linted before it gets created via the [grunt-contrib-concat plugin](https://github.com/gruntjs/grunt-contrib-concat) `concat` task.
17
18In this case, you should lint the "beforeconcat" files first, then concat, then lint the "afterconcat" files, by running `grunt jshint:beforeconcat concat jshint:afterconcat`.
19
20```js
21// Project configuration.
22grunt.initConfig({
23  concat: {
24    dist: {
25      src: ['src/foo.js', 'src/bar.js'],
26      dest: 'dist/output.js'
27    }
28  },
29  jshint: {
30    beforeconcat: ['src/foo.js', 'src/bar.js'],
31    afterconcat: ['dist/output.js']
32  }
33});
34```
35
36## Specifying JSHint options and globals
37
38In this example, custom JSHint options are specified. Note that when `grunt jshint:uses_defaults` is run, those files are linted using the default options, but when `grunt jshint:with_overrides` is run, those files are linted using _merged_ task/target options.
39
40```js
41// Project configuration.
42grunt.initConfig({
43  jshint: {
44    options: {
45      curly: true,
46      eqeqeq: true,
47      eqnull: true,
48      browser: true,
49      globals: {
50        jQuery: true
51      },
52    },
53    uses_defaults: ['dir1/**/*.js', 'dir2/**/*.js'],
54    with_overrides: {
55      options: {
56        curly: false,
57        undef: true,
58      },
59      files: {
60        src: ['dir3/**/*.js', 'dir4/**/*.js']
61      },
62    }
63  },
64});
65```
66
67## Ignoring specific warnings
68
69If you would like to ignore a specific warning:
70
71```shell
72[L24:C9] W015: Expected '}' to have an indentation at 11 instead at 9.
73```
74
75You can toggle it by prepending `-` to the warning id as an option:
76
77```js
78grunt.initConfig({
79  jshint: {
80    ignore_warning: {
81      options: {
82        '-W015': true,
83      },
84      src: ['**/*.js'],
85    },
86  },
87});
88```
89
90## Ignoring specific files
91
92Occasionally application files and third party libraries share the same directory.  To exclude third party code, but include all current and future application files, use a glob for `files` and specifically exclude libraries using `ignores`.  In this example, the jQuery file is matched by the glob but subsequently ignored when JSHint does its analysis.
93
94```js
95grunt.initConfig({
96    jshint: {
97        files: ['js/*.js'],
98        options: {
99            ignores: ['js/jquery.js']
100        }
101    }
102});
103```
Note: See TracBrowser for help on using the repository browser.