Sub topic 7
Disabling Rules Inline, and When Not To
This chapter assumes you already have a working eslint.config.js flat config, understand rule severities (off, warn, error), and can run eslint . from the command line. Prettier's ignore comments get their own chapter later. Here they appear only so you can compare them with ESLint's directives.
Sometimes a rule is right for the codebase but wrong for a specific line. ESLint lets you suppress it in place with a comment called a directive. Directives are useful and easy to misuse. A codebase full of eslint-disable comments has a lint config that no longer describes the rules the team actually follows.
The Directive Forms
ESLint recognizes several comment shapes. Each one covers a different scope.
| Directive | Scope | Comment style |
|---|---|---|
| `eslint-disable-next-line rule` | The line after the comment | `//` or `/* */` |
| `eslint-disable-line rule` | The line the comment sits on | `//` or `/* */` |
| `/* eslint-disable rule */` ... `/* eslint-enable rule */` | Everything between the two comments | Block only |
| `/* eslint-disable rule */` at the top, never re-enabled | The rest of the file | Block only |
| `/* eslint rule: "error" */` | Changes a rule's config for the whole file | Block only |
| `/* global name */` | Declares a global variable for the file | Block only |
Here is the basic case, alongside its Prettier counterpart:
function logUser(user) {
// eslint-disable-next-line no-console
console.log(user);
}
// prettier-ignore
const matrix = [
1, 0, 0,
0, 1, 0,
0, 0, 1,
];The two comments look alike but do different jobs. eslint-disable-next-line tells ESLint not to report a problem. prettier-ignore tells Prettier not to rewrite the next node. Neither tool reads the other's comments.
Range and file-level disables
A range disable needs a matching enable. If you leave out the enable, the disable runs to the end of the file:
/* eslint-disable no-bitwise */
export function hash(str) {
let h = 0;
for (let i = 0; i < str.length; i++) {
h = (h << 5) - h + str.charCodeAt(i);
h |= 0;
}
return h;
}
/* eslint-enable no-bitwise */Always name the rule
All of these forms work without a rule name. // eslint-disable-next-line on its own turns off every rule for that line. Do not write it that way. A bare disable hides the problem you meant to hide, and it also hides any future problem on that line, including real bugs that rules like no-undef or no-unreachable would catch. To disable more than one rule, separate the names with commas:
// eslint-disable-next-line no-console, no-alert
console.log(alert("debug"));Add a reason
After the rule list, put two or more dashes surrounded by spaces, then a description. ESLint ignores everything after the dashes:
// eslint-disable-next-line no-console -- CLI entry point; stdout is the interface
console.log(result);The reason costs nothing to write. It tells a reviewer whether the suppression is still justified, and it gives the next developer enough context to delete it safely.
Common Errors
The directive suppresses nothing
The example logUser function above has a subtle problem. no-console is not part of js.configs.recommended. If that is the only config you use, the directive disables a rule that was never on. Since ESLint v9, linterOptions.reportUnusedDisableDirectives defaults to "warn", so you get:
2:3 warning Unused eslint-disable directive (no problems were reported from 'no-console')Treat this warning as useful. Unused directives pile up after refactors, and each one is a future bare spot where real problems will go unreported. Raise it to an error in your config:
// eslint.config.js
import js from "@eslint/js";
export default [
js.configs.recommended,
{
linterOptions: {
reportUnusedDisableDirectives: "error",
},
},
];eslint --fix deletes unused directives for you, so the stricter setting adds no manual work.
Wrong comment style
/* eslint-disable */, /* eslint ... */, and /* global */ only work as block comments. Written as // eslint-disable no-console, they are not guaranteed to be recognized across ESLint versions, so the suppression may silently do nothing. Use // only for the -line and -next-line forms.
JSX
Inside JSX children, a plain comment is text, not a comment. Wrap the directive in braces:
export function Avatar({ url }) {
return (
<div>
{/* eslint-disable-next-line jsx-a11y/alt-text */}
<img src={url} />
</div>
);
}Plugin rules need their prefix
A rule from a plugin must be named with its plugin namespace, for example @typescript-eslint/no-explicit-any rather than no-explicit-any. The unprefixed name refers to a different rule, which may not exist at all. Since ESLint v9, a directive naming an unknown rule is reported as an error.
When Not to Disable Inline
A directive is the narrowest tool available. Use it only when the exception really is limited to one spot. Ask these questions first:
The middle option is the one people skip. If every script in scripts/ prints to the console, writing a directive in each file spreads one decision across many places. Put it in the config once:
// eslint.config.js
import js from "@eslint/js";
export default [
js.configs.recommended,
{
rules: { "no-console": "error" },
},
{
files: ["scripts/**/*.js", "bin/**/*.js"],
rules: { "no-console": "off" },
},
];Config objects later in the array override earlier ones for the files they match. So no-console is an error in the application code and off in the tooling scripts. The policy is visible in one file and is reviewed like any other code change.
Signs that you are overusing inline disables:
- The same rule is disabled in many files. The rule or its options probably need changing.
- A file opens with
/* eslint-disable */and no rule name. Either the file should be inignores(generated code, vendored libraries) or it needs real cleanup. - Disables appear on code that was just written. A rule that fires on fresh code is often pointing at a real issue.
/* eslint rule: ... */comments change rule config per file. This scatters config across the codebase, and most teams should ban it.
Enforcing Directive Hygiene
You can lint the directives themselves. The @eslint-community/eslint-plugin-eslint-comments plugin adds rules about directive comments:
npm install --save-dev @eslint-community/eslint-plugin-eslint-comments// eslint.config.js
import js from "@eslint/js";
import comments from "@eslint-community/eslint-plugin-eslint-comments/configs";
export default [
js.configs.recommended,
comments.recommended,
{
linterOptions: { reportUnusedDisableDirectives: "error" },
rules: {
"@eslint-community/eslint-comments/require-description": "error",
"@eslint-community/eslint-comments/no-unlimited-disable": "error",
},
},
];no-unlimited-disable rejects directives that name no rule, and require-description rejects any directive without a -- reason. Together they make the good habits from this chapter mandatory.
For the strictest setup, set linterOptions.noInlineConfig: true, or pass --no-inline-config on the command line. ESLint then ignores every directive and warns wherever one appears. Some teams use this only in CI to audit how many suppressions exist. It is too strict for daily use in most projects, because sometimes a one-line exception is the honest answer.
Exercise
A teammate adds // eslint-disable-next-line (with no rule name) above a line that triggered no-unused-vars. Your config uses only js.configs.recommended. Name two problems with this directive and write a better version.
Show answer
First, it names no rule, so it suppresses every rule on that line, including future real errors such as no-undef. Second, it has no reason, so nobody can tell later whether it is still needed. A better version is // eslint-disable-next-line no-unused-vars -- kept to match the callback signature. Better still, if the variable is an unused argument, rename it with a leading underscore and set argsIgnorePattern: "^_" in the rule options, which removes the need for a directive at all.
Exercise
Twelve files under scripts/ each start with /* eslint-disable no-console */. What change would you make, and why?
Show answer
Delete the directives and add a config object with files: ["scripts/**/*.js"] and rules: { "no-console": "off" } in eslint.config.js. The exception then lives in one reviewed place, new scripts get it automatically, and application code keeps the rule. With reportUnusedDisableDirectives enabled, any leftover directive will be flagged as unused.
Exercise
You add /* eslint-disable no-bitwise */ above a function but forget the matching eslint-enable. What is the scope of the disable, and how could a plugin rule catch this?
Show answer
The disable lasts until the end of the file, so any bitwise code added further down is silently allowed. The @eslint-community/eslint-comments/disable-enable-pair rule, which is included in its recommended config, reports a eslint-disable block comment that has no matching eslint-enable.