275

I have a (single) case in my app were eval is used, and I would like to suppress JSHint warning only for this case.

Is there a way to achieve that? Configuration, magic comment, ...?

Mike Aski
  • 9,180
  • 4
  • 46
  • 63

3 Answers3

486

Yes, there is a way. Two in fact. In October 2013 jshint added a way to ignore blocks of code like this:

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

You can also ignore a single line with a trailing comment like this:

ignoreThis(); // jshint ignore:line
Thomas Leduc
  • 1,092
  • 1
  • 20
  • 44
Restore the Data Dumps
  • 38,967
  • 12
  • 96
  • 122
  • For anybody trying to get JSHint Sublime Text 2/3 plugins to work, you will need to upgrade jshint specifically in the ST package folder. (I specifically ran in to this with JSHint Gutter) – Josh Nov 15 '13 at 16:19
  • 8
    Is it possible to ignore a specific error only? eg. maxlen on that specific line yet still detect other errors. – Jürgen Paul Feb 16 '14 at 04:40
  • 4
    thank goodness for `// jshint ignore:line` -- works great with `;(function() { // jshint ignore:line` – katy lavallee May 02 '14 at 16:22
  • 4
    The "ignore" option is deprecated, and I get the error "bad option" using version 0.9.9 of JSHint Integration feature in eclipse. Is there an alternative way to do the same thing? – MidnightJava May 09 '14 at 19:31
  • 3
    I got around the `bad option: 'ignore'` by using the specific [option](http://www.jshint.com/docs/options/) for the line I want ignored. In my case I wanted to ignore `if (x == null)` because I want it to equal more than just null and used [`/* jshint eqnull:true */`](http://www.jshint.com/docs/options/#eqnull) in the same function as the `if` to silence JSHint. – Nate Jul 14 '14 at 17:48
  • Ignoring a line only seems to work on a formal standalone Javascript line. It doesn't work if you try to apply it to a line within a collection or other datatype declaration. – Cerin Feb 06 '17 at 18:59
  • That's really a poor way to do it. You should only disabled the one warning you want. You don't want to disable everything. – user959690 Aug 14 '19 at 21:32
111

The "evil" answer did not work for me. Instead, I used what was recommended on the JSHints docs page. If you know the warning that is thrown, you can turn it off for a block of code. For example, I am using some third party code that does not use camel case functions, yet my JSHint rules require it, which led to a warning. To silence it, I wrote:

/*jshint -W106 */
save_state(id);
/*jshint +W106 */
Rubens Mariuzzo
  • 28,358
  • 27
  • 121
  • 148
tollmanz
  • 3,113
  • 4
  • 29
  • 34
  • 15
    And how do I figure out my warning number? – 11684 Nov 12 '13 at 19:59
  • 3
    When you run jshint and get the error initially, the number will be reported. – tollmanz Nov 14 '13 at 16:33
  • 5
    Oh, sorry. I forgot I left a comment, so didn't post something after I checked the docs. It isn't, you have to add `--verbose` to the command to get the warning number. – 11684 Nov 14 '13 at 16:36
  • 9
    Alternatively, the answer to [this related question](http://stackoverflow.com/questions/17535130/where-can-i-find-a-list-of-jshint-numeric-error-codes) suggests checking [messages.js](https://github.com/jshint/jshint/blob/2.1.4/src/shared/messages.js). I found this quite helpful since I use WebStorm. – Michael Jess Dec 06 '13 at 09:42
  • WebStorm 8.0.3 shows JSHint warning numbers: http://confluence.jetbrains.com/display/WI/WebStorm+135.937+Release+Notes http://youtrack.jetbrains.com/issue/WEB-7597 – Victor Jul 15 '14 at 09:24
  • Good answer as you can ignore specific errors instead of blanket ignores across multiple checks. – roughcoder Feb 10 '15 at 22:54
  • However one warning number specifys a collection of errors of a similar format(See @MichaelJess answer). That could be dangerous potentially. – addlistener Sep 13 '15 at 16:28
  • 4
    All warning, error and info numbers can be found here: https://github.com/jshint/jshint/blob/master/src/messages.js – Anderson Arboleya May 24 '16 at 20:14
  • 1
    there's a way to write this as a single line comment? like `ignoreThis(); //jshint ignore:W080` – Vitim.us Nov 30 '16 at 20:21
40

As you can see in the documentation of JSHint you can change options per function or per file. In your case just place a comment in your file or even more local just in the function that uses eval:

/*jshint evil:true */

function helloEval(str) {
    /*jshint evil:true */
    eval(str);
}
Rubens Mariuzzo
  • 28,358
  • 27
  • 121
  • 148
Odi
  • 6,916
  • 3
  • 34
  • 52