Sub topic 6
Fixing Problems Automatically with --fix
This chapter assumes you have ESLint installed, an eslint.config.js file in place, and a working idea of how rules and severity levels behave. Many rules can do more than report a problem. They can also rewrite the code that broke them. The --fix flag tells ESLint to apply those rewrites for you.
Which Problems Are Fixable
A rule is fixable when its author included a fix function. In the rules reference, these rules are marked with a wrench icon. Rules such as prefer-const, no-var, and no-extra-boolean-cast are fixable because each one has exactly one safe rewrite. Other rules, such as no-undef, only report. ESLint has no way of knowing which variable you meant to use.
Some rules offer suggestions instead of fixes. A suggestion is a rewrite that might change behavior, so --fix never applies it. Your editor shows suggestions as quick actions, and you decide whether to accept each one.
A Worked Example
Add two fixable rules to your config:
// eslint.config.js
import js from "@eslint/js";
export default [
js.configs.recommended,
{
rules: {
"no-var": "error",
"prefer-const": "error",
},
},
];Now lint this file:
// src/total.js
var rate = 0.2;
let items = [10, 20, 30];
export function total() {
return items.reduce((sum, n) => sum + n, 0) * (1 + rate);
}npx eslint src/total.js
npx eslint src/total.js --fixThe first command reports a no-var error on line 1 and a prefer-const error on line 2. The second command rewrites the file, turns both declarations into const, and exits cleanly.
ESLint applies fixes in passes. After one pass, the var becomes let. A second pass then lets prefer-const turn that let into const. ESLint repeats this up to 10 times, until no fixable problems remain.
Previewing and Narrowing Fixes
| Flag | Effect |
|---|---|
| `--fix` | Applies fixes and writes them to disk |
| `--fix-dry-run` | Computes fixes without writing them, which is useful with `--format json` |
| `--fix-type problem,suggestion` | Applies only fixes from rules of the listed types (`problem`, `suggestion`, `layout`, `directive`) |
npx eslint . --fix-dry-run --format json > fixes.json
npx eslint . --fix --fix-type problemCommon Errors
- You expect
--fixto resolve every error. It only touches fixable rules. Anything left over still needs a manual change. - You expect suggestions to be applied.
--fixskips them. Accept them in your editor. - You run
--fixin CI. CI should check the code without changing it. Run plaineslint .there so unfixed problems fail the build.
Exercise
You run npx eslint . --fix, and one no-undef error and one no-useless-escape problem are still reported. Why did --fix leave them?
Show answer
no-undef is not fixable because ESLint cannot know which identifier you intended. no-useless-escape provides suggestions rather than fixes, and --fix never applies suggestions, so you must accept that one in your editor or edit the code by hand.
Exercise
Which command shows what --fix would change in every file without modifying any of them?
Show answer
npx eslint . --fix-dry-run --format json