skip to content

With ESLint 10, how do you turn no-unused-vars and eqeqeq on as errors in a legacy codebase without first fixing every existing violation?

level: seniorimportance: should knowfreq 8%

answer

  1. a committed file, not comments
  2. errors only, warnings are skipped
  3. fix first, then record the rest
  4. eslint-suppressions.json in the run's directory
  5. a moved file needs its flag every run

basics

~10 s

Set both rules to "error", then run eslint --fix --suppress-rule no-unused-vars --suppress-rule eqeqeq. ESLint writes the remaining violations to eslint-suppressions.json; commit it, and later runs report only new violations.

solid answer

~40 s

ESLint's bulk suppressions, added in 9.24.0, are built for this. First set `no-unused-vars` and `eqeqeq` to `"error"` in the config, because only error-level violations are suppressed; warnings are ignored. Then run `eslint --fix --suppress-rule no-unused-vars --suppress-rule eqeqeq` so fixable violations are fixed rather than recorded. ESLint writes `eslint-suppressions.json` in the directory the command runs from, holding a count per file and per rule, and you commit it so every developer and CI run use it. Later runs read that file automatically and hide the recorded violations, while new ones fail. `--suppress-all` records every error-level rule instead. If you move the file with `--suppressions-location`, every run must pass the same flag, or ESLint will not find it.

code

json · 15 lines
json
{
  "src/guide/epg-grid.js": {
    "eqeqeq": {
      "count": 4
    },
    "no-unused-vars": {
      "count": 2
    }
  },
  "src/legacy/listings.js": {
    "no-unused-vars": {
      "count": 7
    }
  }
}

go deeper

for a junior

Know that ESLint can record existing violations in a committed file so a new error rule only fails on new code.

for a middle

Walk through the flags: set the rule to error, run with --fix and --suppress-rule, commit eslint-suppressions.json.

for a senior

Land two new error rules in a legacy codebase with bulk suppressions, and avoid the traps of warn-level rules, moved files and differing working directories.

for a principal

Choose between per-rule and all-rule suppression for a large repository, and who owns regenerating and pruning the file.

## The problem bulk suppressions solve Turning a rule on as `"error"` in an existing codebase usually means hundreds of violations at once. Fixing them all before the switch can take weeks, and new violations keep arriving meanwhile. ESLint 9.24.0 added **bulk suppressions**: a way to record the violations that exist today in a committed file, so the rule is enforced for new code while the old violations are worked off gradually. ## Step by step for the programme-guide app The broadcaster's programme-guide app wants `no-unused-vars` and `eqeqeq` to block merges: 1. **Set both rules to `"error"`** in `eslint.config.js`. Only rules configured as `"error"` are suppressed; a `"warn"` rule's violations are skipped entirely. 2. **Run with `--fix` and the suppress flag together**: `npx eslint --fix --suppress-rule no-unused-vars --suppress-rule eqeqeq`. ESLint's docs recommend `--fix` so violations an autofix can remove are fixed rather than suppressed; `eqeqeq` can fix some comparisons itself, while `no-unused-vars` has no autofix. 3. **Review and commit `eslint-suppressions.json`**. It is created in the directory the command runs from, normally the repository root. 4. **Run ESLint as usual** from the same directory. When the file exists, ESLint applies it automatically; recorded violations are hidden and new ones are reported as errors. ## What the file holds The file maps each file path, relative to the working directory, to each suppressed rule and a **count** of violations: - It is sorted JSON, so regenerating it produces small diffs. - It records how many violations a file has, not where they are, so editing unrelated lines does not invalidate it. - Paths are relative to the directory ESLint runs from, so run it from the same place locally and in CI. ## The flags and their limits | Flag | What it does | |---|---| | `--suppress-rule <rule>` | Records current error-level violations of the named rule; repeat it for several rules | | `--suppress-all` | Records current violations of every rule set to `"error"` | | `--suppressions-location <path>` | Uses a file elsewhere; must be passed on **every** run that should read it | | `--prune-suppressions` | Removes entries whose violations no longer occur | | `--pass-on-unpruned-suppressions` | Stops leftover entries from affecting the exit code | A few combinations are refused: `--suppress-all` with `--suppress-rule`, either of them with `--prune-suppressions`, and any of the three with code piped in on stdin. Creating and pruning suppressions is CLI-only; the Node.js API can only apply an existing file, with `applySuppressions: true`. ## What later runs look like Once the file is committed, ordinary runs behave like this: - A new file with an `==` comparison fails, because it has no entry in the file. - `src/guide/epg-grid.js` with its four recorded `eqeqeq` violations passes as long as it still has four or fewer. - A file that gains a fifth violation of a suppressed rule reports all of that rule's violations in the file, because the file stores a count, not locations. - A file where someone fixed a suppressed violation makes ESLint report the leftover entry and exit with code 2 until `--prune-suppressions` updates the file. ## Choosing between `--suppress-rule` and `--suppress-all` - `--suppress-rule` keeps the file focused on the rules being introduced, so other error-level rules still fail on existing problems. - `--suppress-all` is useful when adopting a whole stricter config at once, but it freezes every existing error, including ones the team would have preferred to see. - Running either again updates the file with the current violations for the rules it covers. ## Traps - Leaving the rule at `"warn"` and expecting the flag to record it: nothing is written for warnings. - Moving the file with `--suppressions-location` once and forgetting the flag in CI: CI then reads no suppressions and reports every old violation. - Running ESLint from a package subdirectory while the file was generated at the root: the relative paths no longer match. - Suppressing first and fixing later, when `--fix` in the same command would have removed the fixable violations for free. What a suppressions file means as a team practice, such as how it should shrink over time, is a broader quality-gate question; the ESLint mechanics are the file, the flags and the counts.

  • Why does the CI job report every old violation after someone moved the file to `config/eslint-suppressions.json`?
    Without `--suppressions-location`, ESLint looks only for `eslint-suppressions.json` in the directory it runs from. The move needs the same `--suppressions-location config/eslint-suppressions.json` flag on every run, local and CI, not just on the command that created the file.
  • Can the suppressions be applied when ESLint runs through its Node.js API?
    Yes, by passing `applySuppressions: true` to the `ESLint` constructor, with `suppressionsLocation` if the file is not in the working directory. The API can only apply an existing file; creating and pruning suppressions are CLI-only.

  • Expects warn-level rules to be recorded by --suppress-rule
  • Adds thousands of eslint-disable comments instead of using the suppressions file
  • Moves the file with --suppressions-location once and omits the flag in CI
  • Suppresses before running --fix, freezing violations that could have been fixed
  • Keeps eslint-suppressions.json out of version control

Assumes ESLint 10; bulk suppressions (--suppress-all, --suppress-rule, eslint-suppressions.json) were introduced in ESLint 9.24.0.