Have jshint ignore certain files when building Twi

2019-02-03 00:42发布

问题:

I often have this problem when using Twitter Bootstrap with other 3rd-party JS libraries, such as html5.js from WordPress' "Twenty Twelve" theme, where the build fails because jshint (or jslint in previous versions of TB I think) throws an error due to the third-party JS library, e.g.

\n##################################################
Building Bootstrap...
##################################################\n
js/html5.js: line 3, col 122, Expected ')' to match '(' from line 3 and instead
  saw ','.
js/html5.js: line 3, col 140, Expected an identifier and instead saw ')'.
js/html5.js: line 4, col 101, eval is evil.
js/html5.js: line 6, col 369, Confusing use of '!'.
js/html5.js: line 6, col 422, Confusing use of '!'.
js/html5.js: line 7, col 61, 'b' is already defined.
js/theme-customizer.js: line 26, col 26, Use '===' to compare with ''.

7 errors
make: *** [build] Error 1

I'd like to avoid modifying third-party libraries or modify TB's Makefile, as that'll cause problems for upgrades in the future; is my only option to put 3rd-party JS files into a separate folder.

Is there a way to get jshint or TB's build process to ignore certain JS files (perhaps through some .jshintrc configuration I'm not aware of?)?

回答1:

So there is an option to ignore files in jshint, but it's not set within .jshintrc but rather a separate file .jshintignore, which makes sense. However, while jshint will look for .jshintrc in your project's subdirectories, .jshintignore needs to be in the project root. So with Twitter Bootstrap, .jshintrc is in /js while .jshintignore needs to be placed in /.

So to solve the problem in my question /.jshintignore needs to contain:

js/html5.js
js/theme-customizer.js

And that's it!



回答2:

As a supplement to Lèse majesté's answer, one can also write these comments in your JS and jshint will ignore the code in between:

// Code here will be linted with JSHint.
/* jshint ignore:start */
// Code here will be linted with ignored by JSHint.
/* jshint ignore:end */

OR if you just want JSHint not to check a single line:

 // jshint ignore:line

Reference: jshint docs



回答3:

The simplest solution for me is I just add // jshint ignore: start to the top of whatever file I want ignored