39

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?)?

Lèse majesté
  • 7,923
  • 2
  • 33
  • 44

3 Answers3

99

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!

Lèse majesté
  • 7,923
  • 2
  • 33
  • 44
  • 5
    Apparently some wild card type magic also works, for example `js/vendor/*` - if you wanted everything under the vendor folder ignored. – demaniak Jul 09 '14 at 07:46
  • 2
    Thanks for posting this -- I was surprised that [`.jshintignore` is not currently documented](https://github.com/jshint/jshint/issues/1745). – Holistic Developer Oct 30 '14 at 14:31
  • @HolisticDeveloper Indeed, it seems like it'd be a pretty widely used feature in real-world applications. – Lèse majesté Oct 31 '14 at 06:29
  • This mostly worked for me, but I actually had to put my .jshintignore file in the project root right alongside .jshintrc – c1phr Dec 11 '14 at 16:41
  • 1
    There is an example file in the github project: https://github.com/jshint/jshint/blob/master/examples/.jshintignore – ThorSummoner Feb 23 '15 at 23:00
  • trying to ignore a whole project (eslint is being used instead) but ```*```, ```./*``` and ```**/*``` have not worked – Mild Fuzz Apr 29 '16 at 16:12
  • @MildFuzz You put those inside of `.eslintignore` in your project root? According to the [docs](http://eslint.org/docs/user-guide/configuring#ignoring-files-and-directories) that should work. – Lèse majesté Sep 23 '16 at 15:47
35

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

Community
  • 1
  • 1
Justin
  • 2,224
  • 2
  • 22
  • 28
15

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

Ronnie
  • 11,138
  • 21
  • 78
  • 140