In ESLint, how far do eslint-disable, eslint-disable-line and eslint-disable-next-line comments reach, and why name the rule and add a -- reason?
answer
- block, same line, next line
- block comments only for the region form
- no rule list means every rule
- eslint-enable without names re-enables all
- two or more hyphens, then the why
basics
~20 s/* eslint-disable / silences rules until / eslint-enable */ or the end of the file, eslint-disable-line covers its own line and eslint-disable-next-line the following one. Naming the rule limits the silence; a -- reason records why.
solid answer
~40 sESLint has three disable directives. `/* eslint-disable */` turns rules off from that point until a matching `/* eslint-enable */` or the end of the file, so at the top it covers the whole file; it works only as a block comment. `eslint-disable-line` covers the line it sits on and `eslint-disable-next-line` covers the line below, and both may be written as `//` or `/* */` comments. Each directive can list rule IDs, plugin rules included; without a list it silences every rule on that region, which also hides any new, unrelated problem. After the rule list you can add a description separated by two or more hyphens, `// eslint-disable-next-line no-console -- debug output for the feed`, which ESLint ignores when matching and which tells the next reader why the exception exists.
go deeper
Know what // eslint-disable-next-line no-console does and that eslint-disable-line covers its own line.
Explain the reach of each directive, why the region form must be a block comment, what an unnamed directive hides, and the -- description syntax.
Audit unexplained disable comments in a legacy codebase: spot directives that silence everything or do nothing, and replace them with named, described ones.
Set a team rule for when an inline directive is acceptable versus a config-file exception, and how reviewers check descriptions.
## Three directives, three reaches A **disable directive** is a comment that tells ESLint not to report violations in part of a file. ESLint still parses the whole file, so disabled code must still be syntactically valid. The three forms differ in how far they reach: | Directive | Reach | Comment forms accepted | |---|---|---| | `eslint-disable` / `eslint-enable` | From the comment to the matching enable, or to the end of the file | Block comment only: `/* ... */` | | `eslint-disable-line` | The line the comment is on | `//` or `/* */`, on one line | | `eslint-disable-next-line` | The line after the comment | `//` or `/* */`, may span lines | A `/* eslint-disable */` at the very top of a file therefore disables rules for the whole file. ## The comment-form trap Only the two line directives are recognised in `//` comments. A `// eslint-disable` line comment is not a directive at all; ESLint treats it as an ordinary comment and reports everything as usual. Two smaller rules: - An `eslint-disable-line` written as a block comment that spans several lines is itself reported as a problem: "eslint-disable-line comment should not span multiple lines." - `eslint-disable-next-line` may be a multi-line block comment, which is handy for listing several rules on separate lines. ## Naming the rule Every directive accepts a comma-separated list of rule IDs, including plugin rules with their namespace prefix (`example/rule-name`): 1. `// eslint-disable-next-line` with no list silences **every** rule on that line, including rules added to the config next year and violations nobody intended to excuse. 2. `// eslint-disable-next-line no-console` silences only `no-console`; an `eqeqeq` problem on the same line is still reported. 3. `/* eslint-enable */` with no list re-enables **all** disabled rules, while `/* eslint-enable no-alert */` re-enables only that one. Naming the rule also lets ESLint say precisely which rule a stale directive no longer suppresses when it reports unused directives. ## The `--` description After the directive and its rule list, a **description** can follow, separated by two or more consecutive `-` characters: - `// eslint-disable-next-line no-console -- prints the feed URL in the dev build` - In a multi-line block comment, the separator and the description can sit on their own lines, but not on a separator line that starts with `*`; the docs show that form failing. ESLint does not interpret the text and core ESLint does not require it; enforcing descriptions takes a plugin rule. Its value is for people: ESLint's guidance is to use disable comments with caution, document the reason after `--`, and treat a temporary disable as a follow-up task. ## The programme-guide files nobody can explain In the broadcaster's programme-guide app, a few files start with `/* eslint-disable */` and some lines carry a bare `// eslint-disable-next-line`. None names a rule and none has a reason, so: - nobody can tell which rule was being excused; - every rule, including `no-unused-vars` and `eqeqeq` once they become errors, is silenced in those places; - a reviewer cannot judge whether the exception is still needed. Rewriting each as a named, described directive, or removing it, is the cleanup. Two related controls exist for when a team wants more than review: `linterOptions.noInlineConfig` (or `--no-inline-config`) makes ESLint ignore every inline directive, and ESLint can report directives that no longer suppress anything. ## How directives interact with severities - A disable directive hides violations of the rules it names whatever their severity, warnings as well as errors. - If the named rule is `"off"` for that file, the directive has nothing to hide, and ESLint can report it as unused. - An inline rule comment such as `/* eslint no-alert: "off" */` is different from a disable directive: it changes the rule's configuration for the whole file, and the docs use it to make sure a rule is never applied regardless of later enable or disable comments. - `/* eslint-enable */` cannot turn on a rule that the configuration has set to `"off"`; it only ends a disabled region. ## Directive or config file? ESLint's own advice is to prefer the configuration file for anything that applies to a group of files, such as test files, because it keeps exceptions in one visible place. Disable comments are for a single, explained exception at a specific line.
- A teammate writes `// eslint-disable-next-line -- no-console debug output`. What does it actually disable?Every rule on the next line. Everything after the two hyphens is the description, so `no-console` there is just text, and the directive carries no rule list. The intended form is `// eslint-disable-next-line no-console -- debug output`, with the rule before the separator.
- How can a team stop all inline directives from having any effect in one build?Set `linterOptions.noInlineConfig: true` in a config object, or pass `--no-inline-config` on the command line. ESLint then ignores `eslint-disable`, `eslint-enable`, the line directives and `/* eslint */` rule comments, so the run reflects the config file alone.
- Thinks a // eslint-disable line comment disables the rest of the file
- Uses bare eslint-disable-next-line comments without naming a rule
- Puts the rule name after the -- separator and expects it to be honoured
- Believes /* eslint-enable */ with no list re-enables only the last rule
- Says ESLint skips parsing disabled code, so it may contain syntax errors