1 | [Grunt homepage](https://github.com/cowboy/grunt) | [Documentation table of contents](toc.md)
|
2 |
|
3 | # lint (built-in task)
|
4 | Validate files with [JSHint][jshint].
|
5 |
|
6 | [jshint]: http://www.jshint.com/
|
7 |
|
8 | ## About
|
9 |
|
10 | This task is a [multi task](types_of_tasks.md), meaning that grunt will automatically iterate over all `lint` targets if a target is not specified.
|
11 |
|
12 | _Need some help getting started with grunt? Visit the [getting started](getting_started.md) page. And if you're creating your own tasks or helpers, be sure to check out the [types of tasks](types_of_tasks.md) page as well as the [API documentation](api.md)._
|
13 |
|
14 | ## A Very Important Note
|
15 | Your `grunt.js` gruntfile **must** contain this code, once and **only** once. If it doesn't, grunt won't work. For the sake of brevity, this "wrapper" code has been omitted from all examples on this page, but it needs to be there.
|
16 |
|
17 | ```javascript
|
18 | module.exports = function(grunt) {
|
19 | // Your grunt code goes in here.
|
20 | };
|
21 | ```
|
22 |
|
23 | ## Project configuration
|
24 |
|
25 | This example shows a brief overview of the [config](api_config.md) properties used by the `lint` task. For a more in-depth explanation, see the usage examples.
|
26 |
|
27 | ```javascript
|
28 | // Project configuration.
|
29 | grunt.initConfig({
|
30 | // Lists of files to be linted with JSHint.
|
31 | lint: {}
|
32 | });
|
33 | ```
|
34 |
|
35 | ## Usage examples
|
36 |
|
37 | ### Wildcards
|
38 |
|
39 | In this example, running `grunt lint` will lint the project's gruntfile as well as all JavaScript files in the `lib` and `test` directories, using the default JSHint `options` and `globals`.
|
40 |
|
41 | ```javascript
|
42 | // Project configuration.
|
43 | grunt.initConfig({
|
44 | lint: {
|
45 | files: ['grunt.js', 'lib/*.js', 'test/*.js']
|
46 | }
|
47 | });
|
48 | ```
|
49 |
|
50 | With a slight modification, running `grunt lint` will also lint all JavaScript files in the `lib` and `test` directories _and all subdirectories_. See the [minimatch](https://github.com/isaacs/minimatch) module documentation for more details on wildcard patterns.
|
51 |
|
52 | ```javascript
|
53 | // Project configuration.
|
54 | grunt.initConfig({
|
55 | lint: {
|
56 | files: ['grunt.js', 'lib/**/*.js', 'test/**/*.js']
|
57 | }
|
58 | });
|
59 | ```
|
60 |
|
61 | ### Linting before and after concat
|
62 |
|
63 | In this example, running `grunt lint` will lint two separate sets of files using the default JSHint `options` and `globals`: one "beforeconcat" set, and one "afterconcat" set. Running `grunt lint` will lint both sets of files all at once, because lint is a [multi task](types_of_tasks.md). This is not ideal, because `dist/output.js` may get linted before it gets created via the [concat task](task_concat.md)!
|
64 |
|
65 | In this case, you should lint the "beforeconcat" set first, then concat, then lint the "afterconcat" set, by running `grunt lint:beforeconcat concat lint:afterconcat`.
|
66 |
|
67 | ```javascript
|
68 | // Project configuration.
|
69 | grunt.initConfig({
|
70 | concat: {
|
71 | dist: {
|
72 | src: ['src/foo.js', 'src/bar.js'],
|
73 | dest: 'dist/output.js'
|
74 | }
|
75 | },
|
76 | lint: {
|
77 | beforeconcat: ['src/foo.js', 'src/bar.js'],
|
78 | afterconcat: ['dist/output.js']
|
79 | }
|
80 | });
|
81 |
|
82 | // Default task.
|
83 | grunt.registerTask('default', 'lint:beforeconcat concat lint:afterconcat');
|
84 | ```
|
85 |
|
86 | _Note: in the above example, a default [alias task](types_of_tasks.md) was created that runs the 'lint:beforeconcat', 'concat' and 'lint:afterconcat' tasks. If you didn't want this to be the default grunt task, you could give it a different name._
|
87 |
|
88 | ### Dynamic filenames
|
89 |
|
90 | Building on the previous example, if you want to avoid duplication, you can use a [directive](helpers_directives.md) like `'<config:concat.dist.dest>'` in place of `'dist/output.js'` in the `afterconcat` lint target. This allows you to generate the output filename dynamically. In this example, the `concat:dist` destination filename is generated from the `name` and `version` properties of the referenced `package.json` file through the `pkg` config property.
|
91 |
|
92 | ```javascript
|
93 | // Project configuration.
|
94 | grunt.initConfig({
|
95 | pkg: '<json:package.json>',
|
96 | concat: {
|
97 | dist: {
|
98 | src: ['src/foo.js', 'src/bar.js'],
|
99 | dest: 'dist/<%= pkg.name %>-<%= pkg.version %>.js'
|
100 | }
|
101 | },
|
102 | lint: {
|
103 | beforeconcat: ['src/foo.js', 'src/bar.js'],
|
104 | afterconcat: ['<config:concat.dist.dest>']
|
105 | }
|
106 | });
|
107 | ```
|
108 |
|
109 | ### Specifying JSHint options and globals
|
110 |
|
111 | In this example, taken from the [Sample jQuery plugin gruntfile](https://github.com/cowboy/grunt-jquery-example/blob/master/grunt.js), custom JSHint `options` and `globals` are specified. These options are explained in the [JSHint documentation](http://www.jshint.com/options/).
|
112 |
|
113 | _Note: config `jshint.options` and `jshint.globals` apply to the entire project, but can be overridden with per-file comments like `/*global exports:false*/`._
|
114 |
|
115 | ```javascript
|
116 | // Project configuration.
|
117 | grunt.initConfig({
|
118 | lint: {
|
119 | files: ['grunt.js', 'src/**/*.js', 'test/**/*.js']
|
120 | },
|
121 | jshint: {
|
122 | options: {
|
123 | curly: true,
|
124 | eqeqeq: true,
|
125 | immed: true,
|
126 | latedef: true,
|
127 | newcap: true,
|
128 | noarg: true,
|
129 | sub: true,
|
130 | undef: true,
|
131 | eqnull: true,
|
132 | browser: true
|
133 | },
|
134 | globals: {
|
135 | jQuery: true
|
136 | }
|
137 | },
|
138 | });
|
139 | ```
|
140 |
|
141 | #### Per-target JSHint options and globals
|
142 |
|
143 | For each `lint` target, grunt looks for a target-named object underneath the `jshint` config object. If this object is found, its `options` and `globals` sub-objects will be used instead of the global ones. this allows per-lint-target JSHint options/globals overrides.
|
144 |
|
145 | In this example, there are default JSHint settings, as well as per-target overrides:
|
146 |
|
147 | ```javascript
|
148 | // Project configuration.
|
149 | grunt.initConfig({
|
150 | lint: {
|
151 | src: 'src/*.js',
|
152 | grunt: 'grunt.js',
|
153 | tests: 'tests/unit/**/*.js'
|
154 | },
|
155 | jshint: {
|
156 | // Defaults.
|
157 | options: {curly: true},
|
158 | globals: {},
|
159 | // Just for the lint:grunt target.
|
160 | grunt: {
|
161 | options: {node: true},
|
162 | globals: {task: true, config: true, file: true, log: true, template: true}
|
163 | },
|
164 | // Just for the lint:src target.
|
165 | src: {
|
166 | options: {browser: true},
|
167 | globals: {jQuery: true}
|
168 | },
|
169 | // Just for the lint:tests target.
|
170 | tests: {
|
171 | options: {jquery: true},
|
172 | globals: {module: true, test: true, ok: true, equal: true, deepEqual: true, QUnit: true}
|
173 | }
|
174 | }
|
175 | });
|
176 | ```
|
177 |
|
178 | ## Helpers
|
179 |
|
180 | A generic `lint` helper is available for use in any other task where file linting might be useful. For example:
|
181 |
|
182 | ```javascript
|
183 | var filename = 'example.js';
|
184 | var src = grunt.file.read(filename);
|
185 | grunt.helper('lint', src, {browser: true}, {jQuery: true}, filename);
|
186 | ```
|
187 |
|
188 | See the [lint task source](../tasks/lint.js) for more information.
|