Sub topic 7
Disabling Rules Inline, and When Not To
This chapter assumes you have a working eslint.config.js and know how rule severities (off, warn, error) work. Sometimes a rule is right for the whole project but wrong for one line. ESLint lets you switch rules off inside a file with special comments called inline disable directives. Use them on purpose and keep them rare. Each directive is a spot where the linter has stopped checking your code.
The Directive Forms
ESLint recognizes several comment forms. Always name the rule you are disabling. If you leave the rule name out, every rule is disabled.
| Directive | Scope |
|---|---|
| `// eslint-disable-next-line rule-name` | The following line only |
| `// eslint-disable-line rule-name` | The line the comment sits on |
| `/* eslint-disable rule-name */` ... `/* eslint-enable rule-name */` | The block between the two comments |
| `/* eslint-disable rule-name */` at the top of a file | The whole file |
Here is the most common case, a deliberate console.log in a project that forbids it:
function logUser(user) {
// eslint-disable-next-line no-console -- debug output required by the CLI spec
console.log(user);
}Text after -- is a description. ESLint ignores it, but reviewers read it. Make a habit of giving the reason every time.
Common Mistakes
- Bare directives. Writing
// eslint-disable-next-linewith no rule name hides every problem on that line, including ones that show up later. - File-wide disables to silence noise. A file-level
/* eslint-disable */usually means the rule's configuration is wrong. Fix the config, or scope the rule with afilesentry ineslint.config.js. - Stale directives. You fix the code, but the comment stays behind and goes on suppressing nothing, or something new.
- Disabling instead of understanding. Rules such as
eqeqeqandno-unused-varsoften point to real bugs. Read the rule's documentation before you silence it.
Catching Unused Directives
ESLint can report directives that no longer suppress anything. Turn this up to error so stale comments fail the lint run:
// eslint.config.js
import js from "@eslint/js";
export default [
js.configs.recommended,
{
linterOptions: {
reportUnusedDisableDirectives: "error",
},
},
];In ESLint v9 the default for this setting is "warn". You can also pass --report-unused-disable-directives on the command line.
Prettier has a comment of its own, // prettier-ignore. It controls formatting, not rule checks, and it is covered in the chapter on ignoring files and code blocks.
Exercise
A teammate adds // eslint-disable-next-line above a line that triggers no-unused-vars. Name two problems with this directive and rewrite it correctly.
Show answer
It names no rule, so every rule is disabled for that line, and it gives no reason. A better version is // eslint-disable-next-line no-unused-vars -- kept for the plugin API signature. Better still, rename the variable or remove it if it is not actually needed.
Exercise
What happens to /* eslint-disable no-console */ after you remove the last console call from a file, if reportUnusedDisableDirectives is set to "error"?
Show answer
ESLint reports the directive as unused and the lint run fails until you delete the comment.