UNPKG

8.88 kBMarkdownView Raw
1# grunt-contrib-jshint v0.9.0 [![Build Status](https://travis-ci.org/gruntjs/grunt-contrib-jshint.png?branch=master)](https://travis-ci.org/gruntjs/grunt-contrib-jshint)
2
3> Validate files with JSHint.
4
5
6
7## Getting Started
8This plugin requires Grunt `~0.4.0`
9
10If you haven't used [Grunt](http://gruntjs.com/) before, be sure to check out the [Getting Started](http://gruntjs.com/getting-started) guide, as it explains how to create a [Gruntfile](http://gruntjs.com/sample-gruntfile) as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:
11
12```shell
13npm install grunt-contrib-jshint --save-dev
14```
15
16Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:
17
18```js
19grunt.loadNpmTasks('grunt-contrib-jshint');
20```
21
22
23
24
25## Jshint task
26_Run this task with the `grunt jshint` command._
27
28Task targets, files and options may be specified according to the grunt [Configuring tasks](http://gruntjs.com/configuring-tasks) guide.
29
30For more explanations of the lint errors JSHint will throw at you please visit [jslinterrors.com](http://jslinterrors.com/).
31
32### Options
33
34Any specified option will be passed through directly to [JSHint][], thus you can specify any option that JSHint supports. See the [JSHint documentation][] for a list of supported options.
35
36[JSHint]: http://www.jshint.com/
37[JSHint documentation]: http://www.jshint.com/docs/
38
39A few additional options are supported:
40
41
42#### globals
43
44Type: `Object`
45Default: `null`
46
47A map of global variables, with keys as names and a boolean value to determine if they are assignable. This is not a standard JSHint option, but is passed into the `JSHINT` function as its third argument. See the [JSHint documentation][] for more information.
48
49
50#### jshintrc
51
52Type: `String` or `true`
53Default: `null`
54
55If set to `true`, no config will be sent to jshint and jshint will search for `.jshintrc` files relative to the files being linted.
56
57If a filename is specified, options and globals defined therein will be used. The `jshintrc` file must be valid JSON and looks something like this:
58
59```json
60{
61 "curly": true,
62 "eqnull": true,
63 "eqeqeq": true,
64 "undef": true,
65 "globals": {
66 "jQuery": true
67 }
68}
69```
70
71*Be aware that `jshintrc` settings are not merged with your Grunt options.*
72
73
74#### extensions
75
76Type: `String`
77Default: `''`
78
79A list of non-dot-js extensions to check.
80
81
82#### ignores
83
84Type: `Array`
85Default: `null`
86
87A list of files and dirs to ignore. This will override your `.jshintignore` file if set and does not merge.
88
89
90#### force
91
92Type: `Boolean`
93Default: `false`
94
95Set `force` to `true` to report JSHint errors but not fail the task.
96
97
98#### reporter
99
100Type: `String`
101Default: `null`
102
103Allows you to modify this plugins output. By default it will use a built-in Grunt reporter. Set the path to your own custom reporter or to one of the built-in JSHint reporters: `jslint` or `checkstyle`.
104
105See also: [Writing your own JSHint reporter.](http://jshint.com/docs/reporters/)
106
107You can also use an external reporter. For example [jshint-stylish](https://github.com/sindresorhus/jshint-stylish):
108
109```
110$ npm install --save-dev jshint-stylish
111```
112
113```js
114options: {
115 reporter: require('jshint-stylish')
116}
117```
118
119#### reporterOutput
120
121Type: `String`
122Default: `null`
123
124Specify a filepath to output the results of a reporter. If `reporterOutput` is specified then all output will be written to the given filepath instead of printed to stdout.
125
126### Usage examples
127
128#### Wildcards
129In 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.
130
131```js
132// Project configuration.
133grunt.initConfig({
134 jshint: {
135 all: ['Gruntfile.js', 'lib/**/*.js', 'test/**/*.js']
136 }
137});
138```
139
140#### Linting before and after concatenating
141In 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.
142
143In this case, you should lint the "beforeconcat" files first, then concat, then lint the "afterconcat" files, by running `grunt jshint:beforeconcat concat jshint:afterconcat`.
144
145```js
146// Project configuration.
147grunt.initConfig({
148 concat: {
149 dist: {
150 src: ['src/foo.js', 'src/bar.js'],
151 dest: 'dist/output.js'
152 }
153 },
154 jshint: {
155 beforeconcat: ['src/foo.js', 'src/bar.js'],
156 afterconcat: ['dist/output.js']
157 }
158});
159```
160
161#### Specifying JSHint options and globals
162
163In 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.
164
165```js
166// Project configuration.
167grunt.initConfig({
168 jshint: {
169 options: {
170 curly: true,
171 eqeqeq: true,
172 eqnull: true,
173 browser: true,
174 globals: {
175 jQuery: true
176 },
177 },
178 uses_defaults: ['dir1/**/*.js', 'dir2/**/*.js'],
179 with_overrides: {
180 options: {
181 curly: false,
182 undef: true,
183 },
184 files: {
185 src: ['dir3/**/*.js', 'dir4/**/*.js']
186 },
187 }
188 },
189});
190```
191
192#### Ignoring specific warnings
193
194If you would like to ignore a specific warning:
195
196```shell
197[L24:C9] W015: Expected '}' to have an indentation at 11 instead at 9.
198```
199
200You can toggle it by prepending `-` to the warning id as an option:
201
202```js
203grunt.initConfig({
204 jshint: {
205 ignore_warning: {
206 options: {
207 '-W015': true,
208 },
209 src: ['**/*.js'],
210 },
211 },
212});
213```
214
215
216## Release History
217
218 * 2014-03-12   v0.9.0   Replace deprecated grunt.util._.clone with Object.create() Replace deprecated grunt.util.hooker with hooker lib Enhancing the readability of the output. Reporter output is relative to the output file. Pass jshint options to the external reporter.
219 * 2013-12-25   v0.8.0   Update to jshint 2.4.0.
220 * 2013-11-16   v0.7.2   Only print file name once per error.
221 * 2013-10-31   v0.7.1   Ability to set jshintrc option to true to use jshint's native ability for finding .jshintrc files relative to the linted files.
222 * 2013-10-23   v0.7.0   Update to jshint 2.3.0.
223 * 2013-10-23   v0.6.5   Fix output when maxerr is low.
224 * 2013-08-29   v0.6.4   jshintrc now loaded by jshint allowing comments.
225 * 2013-08-15   v0.6.3   Fix module location for jshint 2.1.10.
226 * 2013-07-29   v0.6.2   Update to jshint 2.1.7.
227 * 2013-07-27   v0.6.1   Peg jshint to 2.1.4 until breaking changes in 2.1.5 are fixed.
228 * 2013-06-02   v0.6.0   Dont always succeed the task when using a custom reporter. Bump jshint to 2.1.3.
229 * 2013-05-22   v0.5.4   Fix default reporter to show offending file.
230 * 2013-05-19   v0.5.3   Performance: Execute the reporter once rather than per file.
231 * 2013-05-18   v0.5.2   Fix printing too many erroneous ignored file errors.
232 * 2013-05-17   v0.5.1   Fix for when only 1 file is lint free.
233 * 2013-05-17   v0.5.0   Bump to jshint 2.0. Add support for .jshintignore files and ignores option Add support for extensions option. Add support for custom reporters and output report to a file.
234 * 2013-04-08   v0.4.3   Fix evaluation of predef option when it's an object.
235 * 2013-04-08   v0.4.2   Avoid wiping force option when jshintrc is used.
236 * 2013-04-06   v0.4.1   Fix to allow object type for deprecated predef.
237 * 2013-04-04   v0.4.0   Revert task level options to override jshintrc files.
238 * 2013-03-13   v0.3.0   Bump to JSHint 1.1.0. Add force option to report JSHint errors but not fail the task. Add error/warning code to message. Allow task level options to override jshintrc file.
239 * 2013-02-26   v0.2.0   Bump to JSHint 1.0
240 * 2013-02-15   v0.1.1   First official release for Grunt 0.4.0.
241 * 2013-01-18   v0.1.1rc6   Updating grunt/gruntplugin dependencies to rc6. Changing in-development grunt/gruntplugin dependency versions from tilde version ranges to specific versions.
242 * 2013-01-09   v0.1.1rc5   Updating to work with grunt v0.4.0rc5. Switching to this.filesSrc api.
243 * 2012-10-18   v0.1.0   Work in progress, not yet officially released.
244
245---
246
247Task submitted by ["Cowboy" Ben Alman](http://benalman.com/)
248
249*This file was generated on Wed Mar 12 2014 10:07:58.*