Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 189 lines (151 sloc) 6.436 kB
2e8e45f @cowboy Adding lint task documentation
authored
1 [Grunt homepage](https://github.com/cowboy/grunt) | [Documentation table of contents](toc.md)
2
3 # lint (built-in task)
03814fb @cowboy More docs.
authored
4 Validate files with [JSHint][jshint].
5
6 [jshint]: http://www.jshint.com/
2e8e45f @cowboy Adding lint task documentation
authored
7
8 ## About
9
8662392 @cowboy More docs.
authored
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.
2e8e45f @cowboy Adding lint task documentation
authored
11
09f2412 @cowboy More docs.
authored
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)._
2e8e45f @cowboy Adding lint task documentation
authored
13
e5fc6c6 @cowboy More docs.
authored
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.
73ca1d8 @cowboy More docs.
authored
16
17 ```javascript
48cec61 @cowboy Changed back to module.exports in tasks files/gruntfiles vs exports.c…
authored
18 module.exports = function(grunt) {
e5fc6c6 @cowboy More docs.
authored
19 // Your grunt code goes in here.
20 };
21 ```
22
23 ## Project configuration
73ca1d8 @cowboy More docs.
authored
24
e5fc6c6 @cowboy More docs.
authored
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.
73ca1d8 @cowboy More docs.
authored
26
e5fc6c6 @cowboy More docs.
authored
27 ```javascript
28 // Project configuration.
29 grunt.initConfig({
30 // Lists of files to be linted with JSHint.
31 lint: {}
32 });
73ca1d8 @cowboy More docs.
authored
33 ```
34
2e8e45f @cowboy Adding lint task documentation
authored
35 ## Usage examples
36
b188687 @cowboy More lint docs tweaks.
authored
37 ### Wildcards
38
03814fb @cowboy More docs.
authored
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`.
2e8e45f @cowboy Adding lint task documentation
authored
40
41 ```javascript
e5fc6c6 @cowboy More docs.
authored
42 // Project configuration.
43 grunt.initConfig({
44 lint: {
45 files: ['grunt.js', 'lib/*.js', 'test/*.js']
46 }
47 });
2e8e45f @cowboy Adding lint task documentation
authored
48 ```
49
8806f98 @cowboy More docs.
authored
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.
2e8e45f @cowboy Adding lint task documentation
authored
51
52 ```javascript
e5fc6c6 @cowboy More docs.
authored
53 // Project configuration.
54 grunt.initConfig({
55 lint: {
56 files: ['grunt.js', 'lib/**/*.js', 'test/**/*.js']
57 }
58 });
2e8e45f @cowboy Adding lint task documentation
authored
59 ```
60
b188687 @cowboy More lint docs tweaks.
authored
61 ### Linting before and after concat
62
03814fb @cowboy More docs.
authored
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)!
2e8e45f @cowboy Adding lint task documentation
authored
64
8806f98 @cowboy More docs.
authored
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`.
2e8e45f @cowboy Adding lint task documentation
authored
66
67 ```javascript
e5fc6c6 @cowboy More docs.
authored
68 // Project configuration.
69 grunt.initConfig({
70 concat: {
71 dist: {
72 src: ['src/foo.js', 'src/bar.js'],
73 dest: 'dist/output.js'
2ada036 @cowboy Adding some edited docs. Note that many changes in these disc apply t…
authored
74 }
e5fc6c6 @cowboy More docs.
authored
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');
2e8e45f @cowboy Adding lint task documentation
authored
84 ```
85
8662392 @cowboy More docs.
authored
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._
b188687 @cowboy More lint docs tweaks.
authored
87
7943e19 @cowboy More 0.3.0 docs updates.
authored
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.
2ada036 @cowboy Adding some edited docs. Note that many changes in these disc apply t…
authored
91
92 ```javascript
e5fc6c6 @cowboy More docs.
authored
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'
2ada036 @cowboy Adding some edited docs. Note that many changes in these disc apply t…
authored
100 }
e5fc6c6 @cowboy More docs.
authored
101 },
102 lint: {
103 beforeconcat: ['src/foo.js', 'src/bar.js'],
104 afterconcat: ['<config:concat.dist.dest>']
105 }
106 });
2ada036 @cowboy Adding some edited docs. Note that many changes in these disc apply t…
authored
107 ```
108
7943e19 @cowboy More 0.3.0 docs updates.
authored
109 ### Specifying JSHint options and globals
b188687 @cowboy More lint docs tweaks.
authored
110
6a57ed2 @cowboy Updating lint docs.
authored
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/).
2e8e45f @cowboy Adding lint task documentation
authored
112
20bfdaa @cowboy More docs.
authored
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*/`._
2e8e45f @cowboy Adding lint task documentation
authored
114
115 ```javascript
e5fc6c6 @cowboy More docs.
authored
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
8806f98 @cowboy More docs.
authored
133 },
e5fc6c6 @cowboy More docs.
authored
134 globals: {
135 jQuery: true
136 }
137 },
138 });
2e8e45f @cowboy Adding lint task documentation
authored
139 ```
140
19d2b64 @cowboy Tweaking @jzaefferer's lint docs to make them a little more compact.
authored
141 #### Per-target JSHint options and globals
7943e19 @cowboy More 0.3.0 docs updates.
authored
142
19d2b64 @cowboy Tweaking @jzaefferer's lint docs to make them a little more compact.
authored
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:
b3b35a2 @jzaefferer Extend lint docs with an example of combining multiple lint contexts.…
jzaefferer authored
146
147 ```javascript
e5fc6c6 @cowboy More docs.
authored
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}
b3b35a2 @jzaefferer Extend lint docs with an example of combining multiple lint contexts.…
jzaefferer authored
168 },
e5fc6c6 @cowboy More docs.
authored
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}
b3b35a2 @jzaefferer Extend lint docs with an example of combining multiple lint contexts.…
jzaefferer authored
173 }
e5fc6c6 @cowboy More docs.
authored
174 }
175 });
b3b35a2 @jzaefferer Extend lint docs with an example of combining multiple lint contexts.…
jzaefferer authored
176 ```
177
2e8e45f @cowboy Adding lint task documentation
authored
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';
20bfdaa @cowboy More docs.
authored
184 var src = grunt.file.read(filename);
185 grunt.helper('lint', src, {browser: true}, {jQuery: true}, filename);
2e8e45f @cowboy Adding lint task documentation
authored
186 ```
1416d5b @cowboy Adding links to task source at the bottom of tasks docs.
authored
187
24f2be1 @cowboy More docs.
authored
188 See the [lint task source](../tasks/lint.js) for more information.
Something went wrong with that request. Please try again.