Turning off an ESLint rule for a specific line

2.1k2015 年提問 · runtimeZero ·javascriptjshinteslint 主題:Node 與工具鏈 → 在 Stack Overflow 看原文 ↗

In order to turn off a linting rule for a particular line in JSHint, we use the following rule:

/* jshint ignore:start*/
$scope.someVar = ConstructorFunction();
/* jshint ignore:end */

Is there an equivalent of the above for ESLint?

重點答案

To disable the next line:

// eslint-disable-next-line no-use-before-define
var thing = new Thing();

Or use the single line syntax:

var thing = new Thing(); // eslint-disable-line no-use-before-define

See the ESLint documentation.


Thanks to KhalilRavanna's comment:

if you don't care about specificity you can just do //eslint-disable-line and it appears to disable all rules for the given line

var thing = new Thing() // eslint-disable-line

Later versions of ESLint have now been updated with a better way disable a single line:

// eslint-disable-next-line $rulename

See goofballLogic's excellent answer.

Note though, the above is not working for jsx eg React web applications.

For older versions of ESLint

You can use the following pair of disable/ enable comments. Please note only the /*eslint ... */ syntax is working; //eslint ... is not working.

/* eslint-disable */
alert('suppress all warnings between comments')
/* eslint-enable */

/* eslint-disable eqeqeq */
alert('suppress specific warning eg eqeqeq between comments')
/* eslint-enable eqeqeq */

To disable a specific warning, e.g., eqeqeq for an entire file, you can include a comment at the top of the file:

/* eslint eqeqeq:0 */

And when using multi-rule to ignore, list them on one line, separated with a space:

/* eslint jsx-a11y/alt-text:0 react/jsx-no-duplicate-props:0 */

You can also disable a specific rule/rules (rather than all) by specifying them in the enable (open) and disable (close) blocks:

/* eslint-disable no-alert, no-console */

alert('foo');
console.log('bar');

/* eslint-enable no-alert */

Via goofballMagic's link.