UNPKG

5.39 kBMarkdownView Raw
1[![Build Status](https://travis-ci.org/spalger/gulp-jshint.svg?branch=master)](https://travis-ci.org/spalger/gulp-jshint)
2
3## Information
4
5<table>
6<tr>
7<td>Package</td><td>gulp-jshint</td>
8</tr>
9<tr>
10<td>Description</td>
11<td>JSHint plugin for gulp</td>
12</tr>
13<tr>
14<td>Node Version</td>
15<td>>= 0.4</td>
16</tr>
17</table>
18
19## Install
20
21```sh
22npm install jshint gulp-jshint --save-dev
23```
24
25***NOTE:*** as of 2.0 jshint must be installed with gulp-jshint.
26
27## Usage
28
29```js
30const jshint = require('gulp-jshint');
31const gulp = require('gulp');
32
33gulp.task('lint', function() {
34 return gulp.src('./lib/*.js')
35 .pipe(jshint())
36 .pipe(jshint.reporter('YOUR_REPORTER_HERE'));
37});
38```
39
40## Options
41
42Plugin options:
43
44- `lookup`
45 - Default is `true`
46 - When `false` do not lookup `.jshintrc` files. See the [JSHint docs](http://www.jshint.com/docs/) for more info.
47
48- `linter`
49 - Default is `"jshint"`
50 - Either the name of a module to use for linting the code or a linting function itself. This enables using an alternate (but jshint compatible) linter like `"jsxhint"`.
51 - Here's an example of passing in a module name:
52
53 ```js
54 gulp.task('lint', function() {
55 return gulp.src('./lib/*.js')
56 .pipe(jshint({ linter: 'some-jshint-module' }))
57 .pipe(/*...*/);
58 });
59 ```
60
61 - Here's an example of passing in a linting function:
62
63 ```js
64 gulp.task('lint', function() {
65 return gulp.src('./lib/*.js')
66 // This is available for modules like jshint-jsx, which
67 // expose the normal jshint function as JSHINT and the
68 // jsxhint function as JSXHINT
69 .pipe(jshint({ linter: require('jshint-jsx').JSXHINT }))
70 .pipe(/*...*/);
71 });
72 ```
73
74You can pass in any other options and it passes them straight to JSHint. Look at their README for more info. You can also pass in the location of your jshintrc file as a string and it will load options from it.
75
76For example, to load your configuration from your `package.json` exclusively and avoid lookup overhead you can do:
77
78```js
79const pkg = require('./package');
80const jshintConfig = pkg.jshintConfig;
81
82jshintConfig.lookup = false;
83
84gulp.src('yo').pipe(jshint(jshintConfig));
85```
86
87## Results
88
89Adds the following properties to the file object:
90
91```js
92file.jshint.success = true; // or false
93file.jshint.errorCount = 0; // number of errors returned by JSHint
94file.jshint.results = []; // JSHint errors, see [http://jshint.com/docs/reporters/](http://jshint.com/docs/reporters/)
95file.jshint.data = []; // JSHint returns details about implied globals, cyclomatic complexity, etc
96file.jshint.opt = {}; // The options you passed to JSHint
97```
98
99## Reporters
100
101### JSHint reporters
102
103#### Built-in
104
105You can choose any [JSHint reporter](https://github.com/jshint/jshint/tree/master/src/reporters)
106when you call
107
108```js
109stuff
110 .pipe(jshint())
111 .pipe(jshint.reporter('default'))
112```
113
114#### External
115
116Let's use [jshint-stylish](https://github.com/sindresorhus/jshint-stylish) as an example
117
118```js
119const stylish = require('jshint-stylish');
120
121stuff
122 .pipe(jshint())
123 .pipe(jshint.reporter(stylish))
124```
125
126- OR -
127
128```js
129stuff
130 .pipe(jshint())
131 .pipe(jshint.reporter('jshint-stylish'))
132```
133
134JSHint plugins have no good module format so I tried to support all of them I saw in the wild. Hopefully it worked, but if a JSHint plugin isn't working with this library feel free to open an issue.
135
136### Fail Reporter
137
138Do you want the task to fail when a JSHint error happens? gulp-jshint includes a simple utility for this.
139
140This example will log the errors using the stylish reporter, then fail if JSHint was not a success.
141
142```js
143stuff
144 .pipe(jshint())
145 .pipe(jshint.reporter('jshint-stylish'))
146 .pipe(jshint.reporter('fail'))
147```
148
149### Custom Reporters
150
151Custom reporters don't interact with this module at all. jshint will add some attributes to the file object and you can add a custom reporter downstream.
152
153```js
154const jshint = require('gulp-jshint');
155const map = require('map-stream');
156
157const myReporter = map(function (file, cb) {
158 if (file.jshint.success) {
159 return cb(null, file);
160 }
161
162 console.log('JSHINT fail in', file.path);
163 file.jshint.results.forEach(function (result) {
164 if (!result.error) {
165 return;
166 }
167
168 const err = result.error
169 console.log(` line ${err.line}, col ${err.character}, code ${err.code}, ${err.reason}`);
170 });
171
172 cb(null, file);
173});
174
175gulp.task('lint', function() {
176 return gulp.src('./lib/*.js')
177 .pipe(jshint())
178 .pipe(myReporter);
179});
180```
181
182### Reporter Configuration
183
184Some reporters have options which you can pass to `jshint.reporter()`. Here is an example of using verbose mode with the default JSHint reporter.
185
186```js
187gulp.task('lint', function() {
188 return gulp.src('./lib/*.js')
189 .pipe(jshint())
190 .pipe(jshint.reporter('default', { verbose: true }));
191});
192```
193
194## Extract
195
196Tells JSHint to extract JavaScript from HTML files before linting (see [JSHint CLI flags](http://www.jshint.com/docs/cli/)). Keep in mind that it doesn't override the file's content after extraction. This is your tool of choice to lint web components!
197
198```js
199gulp.task('lintHTML', function() {
200 return gulp.src('./src/*.html')
201 // if flag is not defined default value is 'auto'
202 .pipe(jshint.extract('auto|always|never'))
203 .pipe(jshint())
204 .pipe(jshint.reporter('default'));
205});
206```
207
208## LICENSE
209
210[MIT](LICENSE)