[484] | 1 | # grunt-contrib-jshint v0.6.5 [](https://travis-ci.org/gruntjs/grunt-contrib-jshint) |
---|
| 2 | |
---|
| 3 | > Validate files with JSHint. |
---|
| 4 | |
---|
| 5 | |
---|
| 6 | |
---|
| 7 | ## Getting Started |
---|
| 8 | This plugin requires Grunt `~0.4.0` |
---|
| 9 | |
---|
| 10 | If 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 |
---|
| 13 | npm install grunt-contrib-jshint --save-dev |
---|
| 14 | ``` |
---|
| 15 | |
---|
| 16 | Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript: |
---|
| 17 | |
---|
| 18 | ```js |
---|
| 19 | grunt.loadNpmTasks('grunt-contrib-jshint'); |
---|
| 20 | ``` |
---|
| 21 | |
---|
| 22 | |
---|
| 23 | |
---|
| 24 | |
---|
| 25 | ## Jshint task |
---|
| 26 | _Run this task with the `grunt jshint` command._ |
---|
| 27 | |
---|
| 28 | Task targets, files and options may be specified according to the grunt [Configuring tasks](http://gruntjs.com/configuring-tasks) guide. |
---|
| 29 | |
---|
| 30 | For more explanations of the lint errors JSHint will throw at you please visit [jslinterrors.com](http://jslinterrors.com/). |
---|
| 31 | |
---|
| 32 | ### Options |
---|
| 33 | |
---|
| 34 | Any 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 | |
---|
| 39 | A few additional options are supported: |
---|
| 40 | |
---|
| 41 | #### globals |
---|
| 42 | Type: `Object` |
---|
| 43 | Default value: `null` |
---|
| 44 | |
---|
| 45 | A 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. |
---|
| 46 | |
---|
| 47 | #### jshintrc |
---|
| 48 | Type: `String` |
---|
| 49 | Default value: `null` |
---|
| 50 | |
---|
| 51 | If this filename is specified, options and globals defined therein will be used. The `jshintrc` file must be valid JSON and looks something like this: |
---|
| 52 | |
---|
| 53 | ```json |
---|
| 54 | { |
---|
| 55 | "curly": true, |
---|
| 56 | "eqnull": true, |
---|
| 57 | "eqeqeq": true, |
---|
| 58 | "undef": true, |
---|
| 59 | "globals": { |
---|
| 60 | "jQuery": true |
---|
| 61 | } |
---|
| 62 | } |
---|
| 63 | ``` |
---|
| 64 | |
---|
| 65 | *Be aware that `jshintrc` settings are not merged with your Grunt options.* |
---|
| 66 | |
---|
| 67 | #### extensions |
---|
| 68 | Type: `String` |
---|
| 69 | Default value: `''` |
---|
| 70 | |
---|
| 71 | A list of non-dot-js extensions to check. |
---|
| 72 | |
---|
| 73 | #### ignores |
---|
| 74 | Type: `Array` |
---|
| 75 | Default value: `null` |
---|
| 76 | |
---|
| 77 | A list of files and dirs to ignore. This will override your `.jshintignore` file if set and does not merge. |
---|
| 78 | |
---|
| 79 | #### force |
---|
| 80 | Type: `Boolean` |
---|
| 81 | Default value: `false` |
---|
| 82 | |
---|
| 83 | Set `force` to `true` to report JSHint errors but not fail the task. |
---|
| 84 | |
---|
| 85 | #### reporter |
---|
| 86 | Type: `String` |
---|
| 87 | Default value: `null` |
---|
| 88 | |
---|
| 89 | Allows 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`. |
---|
| 90 | |
---|
| 91 | See also: [Writing your own JSHint reporter.](http://jshint.com/docs/reporters/) |
---|
| 92 | |
---|
| 93 | #### reporterOutput |
---|
| 94 | Type: `String` |
---|
| 95 | Default value: `null` |
---|
| 96 | |
---|
| 97 | Specify 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. |
---|
| 98 | |
---|
| 99 | ### Usage examples |
---|
| 100 | |
---|
| 101 | #### Wildcards |
---|
| 102 | In 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. |
---|
| 103 | |
---|
| 104 | ```js |
---|
| 105 | // Project configuration. |
---|
| 106 | grunt.initConfig({ |
---|
| 107 | jshint: { |
---|
| 108 | all: ['Gruntfile.js', 'lib/**/*.js', 'test/**/*.js'] |
---|
| 109 | } |
---|
| 110 | }); |
---|
| 111 | ``` |
---|
| 112 | |
---|
| 113 | #### Linting before and after concatenating |
---|
| 114 | In 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. |
---|
| 115 | |
---|
| 116 | In this case, you should lint the "beforeconcat" files first, then concat, then lint the "afterconcat" files, by running `grunt jshint:beforeconcat concat jshint:afterconcat`. |
---|
| 117 | |
---|
| 118 | ```js |
---|
| 119 | // Project configuration. |
---|
| 120 | grunt.initConfig({ |
---|
| 121 | concat: { |
---|
| 122 | dist: { |
---|
| 123 | src: ['src/foo.js', 'src/bar.js'], |
---|
| 124 | dest: 'dist/output.js' |
---|
| 125 | } |
---|
| 126 | }, |
---|
| 127 | jshint: { |
---|
| 128 | beforeconcat: ['src/foo.js', 'src/bar.js'], |
---|
| 129 | afterconcat: ['dist/output.js'] |
---|
| 130 | } |
---|
| 131 | }); |
---|
| 132 | ``` |
---|
| 133 | |
---|
| 134 | #### Specifying JSHint options and globals |
---|
| 135 | |
---|
| 136 | In 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. |
---|
| 137 | |
---|
| 138 | ```js |
---|
| 139 | // Project configuration. |
---|
| 140 | grunt.initConfig({ |
---|
| 141 | jshint: { |
---|
| 142 | options: { |
---|
| 143 | curly: true, |
---|
| 144 | eqeqeq: true, |
---|
| 145 | eqnull: true, |
---|
| 146 | browser: true, |
---|
| 147 | globals: { |
---|
| 148 | jQuery: true |
---|
| 149 | }, |
---|
| 150 | }, |
---|
| 151 | uses_defaults: ['dir1/**/*.js', 'dir2/**/*.js'], |
---|
| 152 | with_overrides: { |
---|
| 153 | options: { |
---|
| 154 | curly: false, |
---|
| 155 | undef: true, |
---|
| 156 | }, |
---|
| 157 | files: { |
---|
| 158 | src: ['dir3/**/*.js', 'dir4/**/*.js'] |
---|
| 159 | }, |
---|
| 160 | } |
---|
| 161 | }, |
---|
| 162 | }); |
---|
| 163 | ``` |
---|
| 164 | |
---|
| 165 | #### Ignoring specific warnings |
---|
| 166 | |
---|
| 167 | If you would like to ignore a specific warning: |
---|
| 168 | |
---|
| 169 | ```shell |
---|
| 170 | [L24:C9] W015: Expected '}' to have an indentation at 11 instead at 9. |
---|
| 171 | ``` |
---|
| 172 | |
---|
| 173 | You can toggle it by prepending `-` to the warning id as an option: |
---|
| 174 | |
---|
| 175 | ```js |
---|
| 176 | grunt.initConfig({ |
---|
| 177 | jshint: { |
---|
| 178 | ignore_warning: { |
---|
| 179 | options: { |
---|
| 180 | '-W015': true, |
---|
| 181 | }, |
---|
| 182 | src: ['**/*.js'], |
---|
| 183 | }, |
---|
| 184 | }, |
---|
| 185 | }); |
---|
| 186 | ``` |
---|
| 187 | |
---|
| 188 | #### Ignoring specific files |
---|
| 189 | |
---|
| 190 | Occasionally 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. |
---|
| 191 | |
---|
| 192 | ```js |
---|
| 193 | grunt.initConfig({ |
---|
| 194 | jshint: { |
---|
| 195 | files: ['js/*.js'], |
---|
| 196 | options: { |
---|
| 197 | ignores: ['js/jquery.js'] |
---|
| 198 | } |
---|
| 199 | } |
---|
| 200 | }); |
---|
| 201 | ``` |
---|
| 202 | |
---|
| 203 | |
---|
| 204 | ## Release History |
---|
| 205 | |
---|
| 206 | * 2013-10-23âââv0.6.5âââFix output when maxerr is low. |
---|
| 207 | * 2013-08-29âââv0.6.4âââjshintrc now loaded by jshint allowing comments. |
---|
| 208 | * 2013-08-15âââv0.6.3âââFix module location for jshint 2.1.10. |
---|
| 209 | * 2013-07-29âââv0.6.2âââUpdate to jshint 2.1.7. |
---|
| 210 | * 2013-07-27âââv0.6.1âââPeg jshint to 2.1.4 until breaking changes in 2.1.5 are fixed. |
---|
| 211 | * 2013-06-02âââv0.6.0âââDont always succeed the task when using a custom reporter. Bump jshint to 2.1.3. |
---|
| 212 | * 2013-05-22âââv0.5.4âââFix default reporter to show offending file. |
---|
| 213 | * 2013-05-19âââv0.5.3âââPerformance: Execute the reporter once rather than per file. |
---|
| 214 | * 2013-05-18âââv0.5.2âââFix printing too many erroneous ignored file errors. |
---|
| 215 | * 2013-05-17âââv0.5.1âââFix for when only 1 file is lint free. |
---|
| 216 | * 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. |
---|
| 217 | * 2013-04-08âââv0.4.3âââFix evaluation of predef option when it's an object. |
---|
| 218 | * 2013-04-08âââv0.4.2âââAvoid wiping force option when jshintrc is used. |
---|
| 219 | * 2013-04-06âââv0.4.1âââFix to allow object type for deprecated predef. |
---|
| 220 | * 2013-04-04âââv0.4.0âââRevert task level options to override jshintrc files. |
---|
| 221 | * 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. |
---|
| 222 | * 2013-02-26âââv0.2.0âââBump to JSHint 1.0 |
---|
| 223 | * 2013-02-15âââv0.1.1âââFirst official release for Grunt 0.4.0. |
---|
| 224 | * 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. |
---|
| 225 | * 2013-01-09âââv0.1.1rc5âââUpdating to work with grunt v0.4.0rc5. Switching to this.filesSrc api. |
---|
| 226 | * 2012-10-18âââv0.1.0âââWork in progress, not yet officially released. |
---|
| 227 | |
---|
| 228 | --- |
---|
| 229 | |
---|
| 230 | Task submitted by ["Cowboy" Ben Alman](http://benalman.com/) |
---|
| 231 | |
---|
| 232 | *This file was generated on Wed Oct 23 2013 20:33:11.* |
---|