Disable an ESLint rule
ESLint is great at spotting errors in JavaScript code, but sometimes you need to disable it for certain conditions.
For instance when debugging you’ll probably want to write something to the console, but that violates the no-console
rule. Depending on your setup you might get a warning, or your code may straight-up refuse to compile.
This line
Use eslint-disable-line
to disable ESLint for the current line of code. Optionally you can disable a specific rule or rules.
In the third example, notice the double quotes. ESLint allows you to enforce single or double quotes for strings with the quotes rule, I use single quotes so this would usually throw a linting error.
console.log('lorem'); // eslint-disable-line
console.log('lorem'); // eslint-disable-line no-console
console.log("lorem"); // eslint-disable-line no-console, quotes
The next line
eslint-disable-next-line
will disable ESLint for the next line of code. This is the one I tend to use as it keeps the comment separate from the code, I just find it more readable.
Once again you can disable a specific rule or rules.
// eslint-disable-next-line
console.log('lorem');
// eslint-disable-next-line no-console
console.log('lorem');
// eslint-disable-next-line no-console, quotes
console.log("lorem");
The whole file
You can disable ESLint for an entire file with eslint-disable
, put it at the top of your file.
/* eslint-disable */
// elsewhere in the file
console.log('lorem');
As before you can customise the rules to disable.
/* eslint-disable no-console, quotes */
// elsewhere in the file
console.log('lorem');
A block of code
Identical to the one above, but this time you re-enable it after your code block: eslint-enable
/* eslint-disable */
console.log('lorem');
console.log('ipsum');
console.log('dolor');
/* eslint-enable */