Reading and fixing lint errors
A report from ESLint has one line per problem, and every line answers four questions: where, which rule, what is wrong and how bad it is. Reading two of those lines is enough to fix a file, and running the same command again is the proof that the fix worked.
The lesson starts from a file with four problems and a config with three rules added to the recommended set. --fix repairs two of the four in place and says so, the other two need a decision that a machine cannot make, and the last section reads the exit status of each state.
Reading the report
The config of the previous lesson gains a rules object beside languageOptions. eqeqeq demands ===, prefer-const demands const for a binding that never changes, and semi demands the semicolon. The three are not in the recommended set, which is why a project names them.
The file eslint.config.js:
vi eslint.config.js
const js = require('@eslint/js');
module.exports = [
js.configs.recommended,
{
languageOptions: {
ecmaVersion: 'latest',
sourceType: 'commonjs',
globals: {
console: 'readonly',
process: 'readonly',
},
},
rules: {
eqeqeq: 'error',
'prefer-const': 'error',
semi: ['error', 'always'],
},
},
];
The file under inspection has four problems of three kinds: a binding that could be const, a loose comparison, a missing semicolon and a name that nothing defines. It keeps the last two after the fixes of this page, on purpose.
The file src/report.js:
function sameCount(a, b) {
let result = a == b
return result;
}
function check(values) {
const count = values.length;
return sameCount(count, missing);
}
module.exports = { check };
The report comes through the same program as before, because the JSON formatter is the one a lesson can print without the absolute path of a folder. Each line is line:column rule - message, and the count above it is the summary of the file.
npx eslint --format json . | node summarise.js
eslint.config.js: 0 error(s), 0 warning(s)
clean.js: 0 error(s), 0 warning(s)
report.js: 4 error(s), 0 warning(s)
2:7 prefer-const - 'result' is never reassigned. Use 'const' instead.
2:18 eqeqeq - Expected '===' and instead saw '=='.
2:22 semi - Missing semicolon.
8:27 no-undef - 'missing' is not defined.
summarise.js: 0 error(s), 0 warning(s)
Three problems sit on one line, at three columns, which is normal for a short line. The column is where the rule wants to look, not always where the fix goes: the semicolon is missing at the end of the line, and the column points after the last token. The rule id is the name to search for, and the message is written by the rule itself.
eslint --fix
--fix applies every repair that the rules are sure about, then reports what is left. The file on disk changes, and the report of the same run describes the state after the fix, so one command is both the edit and the confirmation.
npx eslint --fix --format json . | node summarise.js
eslint.config.js: 0 error(s), 0 warning(s)
clean.js: 0 error(s), 0 warning(s)
report.js: 2 error(s), 0 warning(s)
2:20 eqeqeq - Expected '===' and instead saw '=='.
8:27 no-undef - 'missing' is not defined.
summarise.js: 0 error(s), 0 warning(s)
Two problems are gone: let became const and the semicolon appeared. The comparison stayed where it was, and so did the undefined name.
cat src/report.js
function sameCount(a, b) {
const result = a == b;
return result;
}
function check(values) {
const count = values.length;
return sameCount(count, missing);
}
module.exports = { check };
A second --fix changes nothing and reports the same two lines, which is the test of a fix that has nothing left to do. The exit status of that run is 1, because problems remain; a run that fixes everything exits with 0, and a run with nothing to fix never writes.
What a fix cannot do
A rule has a fix only when the repair is mechanical and does not change what the program does. prefer-const and semi pass that test, and so does a long list of formatting rules. The two that remain fail it for different reasons.
eqeqeq could rewrite == into === in one edit, and ESLint does not, because the two operators differ for null: value == null is true for both null and undefined, and the strict form is false for undefined. A rewrite would change a program that works, so the rule reports and the person decides.
no-undef is further from a repair: the name missing has no declaration, and the linter cannot know whether the author meant to import it, to define it in the file or to delete the call. A tool that invented a declaration would hide the bug that the rule exists to find. The same holds for no-unused-vars: deleting a variable that is never read is safe in the example and wrong in a file whose side effect is the point.
So the two errors stay, and the file keeps them as the example of the distinction. A project that has to silence a rule for a line says so in the code, with a comment that names the rule and the reason:
// eslint-disable-next-line no-console -- the CLI prints its result here
console.log(result);
A comment without a reason is the usual way a rule quietly stops working, which is why some teams turn --report-unused-disable-directives on and remove the comments that no longer suppress anything.
The exit status
The status is the part of the report a script reads, and it has one meaning: 0 when there are no errors, 1 when there is at least one, and 2 for a problem of ESLint itself, such as a config that does not parse. A warning alone leaves the status at 0, which is how a team can adopt a new rule without breaking the build on the day it is added.
npx eslint src/clean.js
echo "a clean file exits with: $?"
npx eslint .
echo "the folder with two errors exits with: $?"
a clean file exits with: 0
the folder with two errors exits with: 1
The clean file of the folder is the same function without the mistakes, and it is there to show the other half of the pair. npx eslint . printed its report to stderr and the block keeps only the two statuses, which is what a pre-commit hook or a pipeline would see.
Exercises
- Turn no-unused-vars off and run the linter on src/compare.js of the previous lesson. Which other rule would now report the same name, and why does the answer depend on the config?
- Fix missing by defining it in the file, then by importing it, and compare the two reports. Which one leaves a new problem?
- Run npx eslint --fix src/report.js and then cat the file. Which byte of the file changed, and which one did not?
- Change semi to the value 'never' and run --fix. What happens to every semicolon in the folder, and why is that a warning about running a formatter as a linter?
- Run the linter with --max-warnings 0 on a folder whose only problem is a warning. Which status comes back, and which flag of the script would a pipeline need?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does the position in an ESLint report name?
Answer
The line and the column of the problem
The pair is written as line:column before the rule id and the message.
-
What does this program print?
const message = "Expected === and instead saw =="; console.log(message.split(" ").slice(0, 2).join(" "));Answer
Expected ===
The first two words of the message are kept and joined with one space.
-
Which problems does
eslint --fixrepair?Answer
The ones whose repair cannot change what the program does
A fix has to be mechanical and safe, so a rule with a semantic choice reports and waits.
-
What does this program print?
const results = [{ errorCount: 2, warningCount: 0 }]; const total = results.reduce((sum, result) => sum + result.errorCount + result.warningCount, 0); console.log(total);Answer
2
The reduce adds the two counts of the only entry to the initial zero.
-
Why does ESLint not rewrite
==into===by itself?Answer
Because the two operators differ for null and undefined
value == null is true for two values, and the strict form is true for one, so the edit can change behaviour.
-
What is the exit status of a run with one error and one warning?
Answer
1
The status is a flag and not a count: any error makes it 1.
-
What does this program print?
const problems = [{ line: 2, ruleId: "semi" }, { line: 8, ruleId: "no-undef" }]; console.log(problems.map((problem) => problem.ruleId).join(" "));Answer
semi no-undef
map keeps the order of the array and the join puts one space between the ids.
-
What happens to the report of a
--fixrun?Answer
It describes the state after the fixes
The fixes are applied first, and the run reports what could not be repaired.
-
What does this program print?
const before = 4; const after = 2; console.log(before - after, before > after);Answer
2 true
The difference of the two counts is two, and the comparison is true.
-
What does an
eslint-disable-next-linecomment do without a reason?Answer
It silences the rule on the next line, and nothing records why
The comment is a local exception, and a reason is what keeps it from becoming permanent silence.
The programs and commands on this page were run with Node.js 24.22.1 and ESLint 10.14.0 on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev