Is there a way to suppress JSHint warning for one given line?

Jshint

Jshint Problem Overview


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, ...?

Jshint Solutions


Solution 1 - Jshint

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

Solution 2 - Jshint

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 */

Solution 3 - Jshint

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);
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMike AskiView Question on Stackoverflow
Solution 1 - JshintJason PunyonView Answer on Stackoverflow
Solution 2 - JshinttollmanzView Answer on Stackoverflow
Solution 3 - JshintOdiView Answer on Stackoverflow