360b195e798e3afb58ef331f4ea0d8f73cc71065.svn-base
6.29 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
[Grunt homepage](https://github.com/gruntjs/grunt) | [Documentation table of contents](toc.md)
# lint (built-in task)
Validate files with [JSHint][jshint].
[jshint]: http://www.jshint.com/
## About
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.
_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)._
## A Very Important Note
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.
```javascript
module.exports = function(grunt) {
// Your grunt code goes in here.
};
```
## Project configuration
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.
```javascript
// Project configuration.
grunt.initConfig({
// Lists of files to be linted with JSHint.
lint: {}
});
```
## Usage examples
### Wildcards
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`.
```javascript
// Project configuration.
grunt.initConfig({
lint: {
files: ['grunt.js', 'lib/*.js', 'test/*.js']
}
});
```
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.
```javascript
// Project configuration.
grunt.initConfig({
lint: {
files: ['grunt.js', 'lib/**/*.js', 'test/**/*.js']
}
});
```
### Linting before and after concat
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)!
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`.
```javascript
// Project configuration.
grunt.initConfig({
concat: {
dist: {
src: ['src/foo.js', 'src/bar.js'],
dest: 'dist/output.js'
}
},
lint: {
beforeconcat: ['src/foo.js', 'src/bar.js'],
afterconcat: ['dist/output.js']
}
});
// Default task.
grunt.registerTask('default', 'lint:beforeconcat concat lint:afterconcat');
```
_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._
### Dynamic filenames
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.
```javascript
// Project configuration.
grunt.initConfig({
pkg: '<json:package.json>',
concat: {
dist: {
src: ['src/foo.js', 'src/bar.js'],
dest: 'dist/<%= pkg.name %>-<%= pkg.version %>.js'
}
},
lint: {
beforeconcat: ['src/foo.js', 'src/bar.js'],
afterconcat: ['<config:concat.dist.dest>']
}
});
```
### Specifying JSHint options and globals
In this example, taken from the [Sample jQuery plugin gruntfile](https://github.com/gruntjs/grunt-init-jquery-sample/blob/master/grunt.js), custom JSHint `options` and `globals` are specified. These options are explained in the [JSHint documentation](http://www.jshint.com/options/).
_Note: config `jshint.options` and `jshint.globals` apply to the entire project, but can be overridden with per-file comments like `/*global exports:false*/`._
```javascript
// Project configuration.
grunt.initConfig({
lint: {
files: ['grunt.js', 'src/**/*.js', 'test/**/*.js']
},
jshint: {
options: {
curly: true,
eqeqeq: true,
immed: true,
latedef: true,
newcap: true,
noarg: true,
sub: true,
undef: true,
eqnull: true,
browser: true
},
globals: {
jQuery: true
}
},
});
```
#### Per-target JSHint options and globals
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.
In this example, there are default JSHint settings, as well as per-target overrides:
```javascript
// Project configuration.
grunt.initConfig({
lint: {
src: 'src/*.js',
grunt: 'grunt.js',
tests: 'tests/unit/**/*.js'
},
jshint: {
// Defaults.
options: {curly: true},
globals: {},
// Just for the lint:grunt target.
grunt: {
options: {node: true},
globals: {task: true, config: true, file: true, log: true, template: true}
},
// Just for the lint:src target.
src: {
options: {browser: true},
globals: {jQuery: true}
},
// Just for the lint:tests target.
tests: {
options: {jquery: true},
globals: {module: true, test: true, ok: true, equal: true, deepEqual: true, QUnit: true}
}
}
});
```
## Helpers
A generic `lint` helper is available for use in any other task where file linting might be useful. For example:
```javascript
var filename = 'example.js';
var src = grunt.file.read(filename);
grunt.helper('lint', src, {browser: true}, {jQuery: true}, filename);
```
See the [lint task source](../tasks/lint.js) for more information.