Explain Codes LogoExplain Codes Logo

Turning off eslint rule for a specific line

javascript
eslint
code-quality
technical-debt
Anton ShumikhinbyAnton Shumikhin·Aug 17, 2024
TLDR

To immediately disable an ESLint rule for a line, use // eslint-disable-next-line [rule-name]:

// eslint-disable-next-line no-console console.log("I'm sneaking past ESLint!");

Replace [rule-name] with the exact rule to silence. If you leave out [rule-name], all rules will be disabled for that line.

Straight to the point: disabling rules

Disabling a rule for the next line

If you want to ignore ESLint just for the next line, use // eslint-disable-next-line [rule-name]:

// eslint-disable-next-line no-unused-vars const ninjaVar = "I'm hiding from ESLint!";

Deactivating multiple rules

If you wish to combine directives in a single comment to disable multiple rules, do this:

// eslint-disable-next-line no-alert, quotes, semi alert('Alert! Multi-rule disabling in progress.');

Ignoring a rule within a line

To disable rules within the line, employ // eslint-disable-line [rule-name]:

console.log('Indeed, eslint is disabled here.'); // eslint-disable-line no-console

Disabling rules for blocks of code

Wrap your code with /* eslint-disable */ and /* eslint-enable */ to temporarily turn off linting:

/* eslint-disable no-console */ console.log('ESLint has left the chat.'); console.log('Still no eslint!'); /* eslint-enable no-console */

This also works to target specific rules:

/* eslint-disable no-underscore-dangle */ lib._privateMethod(); // Pssst! ESLint can't see us! /* eslint-enable no-underscore-dangle */

Disabling a rule for the entire file

To pretend a rule doesn't exist in a file, use /* eslint rule-name:0 */ at the beginning:

/* eslint no-global-assign:0 */ window = "ESLint can't see me!";

Mastering rule management

Handling unique scenarios

ESLint also works with JSX. Just remember to adapt the standard line appropriately:

<MyComponent prop={value} // eslint-disable-line react/no-direct-mutation-state state="Kowabunga!" />

Document rule disabling

Always document why you’re disabling a rule. It helps the next person to read your code:

// Temporarily disabling due to an existential crisis // eslint-disable-next-line camelcase const life_42 = getAnswer();

Avoid overuse

Disabling rules frequently might be an indicator of a more significant issue. It's like eating too much candy, you'll get a stomach ache. Refactor instead.

Thoughtful overrides

Thoughtful override, when required, improves code quality. Consider custom rules, third-party libraries, or coding styles:

// eslint-disable-next-line no-underscore-dangle lib._privateMethod(); // Doing a sneaky, for the greater good.

Additional hints

Team decisions

In team coding, discuss when to disable rules. Mutually agreed standards reduce conflicts and maintain unified code quality.

Technical Debt

Use rule disabling as a temporary solution while addressing technical debt:

// TODO: Refactor after updates // eslint-disable-next-line no-use-before-define const futureFix = prepFix();

Emergencies and exceptions

A justified case for rule disabling is in emergencies:

// Hotfix: waiting on a proper auth flow // eslint-disable-next-line security/detect-non-literal-fs-filename fs.readFile(extremelyUrgentFile);