Sub topic 4
Choosing Rules and Severity Levels
This chapter assumes you already have ESLint installed and an eslint.config.js file that exports an array of config objects, as covered in the previous chapters. Here you will learn how to turn individual rules on and off, pass options to them, and pick a severity level for each one that matches how your team works.
How a rule entry is written
Each key in a config object's rules property is a rule name, and its value is either a severity or an array. When it is an array, the severity comes first and the rule's options follow it.
| Form | Example | Meaning |
|---|---|---|
| Severity only | `"prefer-const": "error"` | Turn the rule on with its default options |
| Severity plus options | `eqeqeq: ["error", "always"]` | Turn it on and configure its behavior |
| Numeric severity | `"no-debugger": 2` | Same as `"error"`; `0` is off, `1` is warn |
| Off | `"no-console": "off"` | Disable a rule a shared config enabled |
There are three severity levels:
"off"(or0): the rule does not run."warn"(or1): ESLint reports the problem but still exits with code 0."error"(or2): ESLint reports the problem and exits with code 1.
The exit code matters more than the label printed in your terminal. Scripts, Git hooks, and CI pipelines decide pass or fail from the exit code. So a "warn" rule never blocks anything unless you tell ESLint to treat warnings as failures.
A worked configuration
The config below layers a few chosen rules on top of the recommended set. Each rule uses its options to fit a common real-world preference.
// eslint.config.js
import js from "@eslint/js";
import globals from "globals";
export default [
js.configs.recommended,
{
files: ["**/*.js"],
languageOptions: {
ecmaVersion: "latest",
sourceType: "module",
globals: { ...globals.node },
},
rules: {
// Bugs: fail the run
eqeqeq: ["error", "always", { null: "ignore" }],
"no-unused-vars": ["error", { args: "after-used", argsIgnorePattern: "^_" }],
"prefer-const": "error",
// Habits we are phasing out: report, don't fail
"no-console": ["warn", { allow: ["warn", "error"] }],
// Recommended, but not useful for this codebase
"no-empty": "off",
},
},
];Here is what the options do:
eqeqeqwith{ null: "ignore" }requires===everywhere exceptx == null, a common idiom that checks for bothnullandundefined.no-unused-varswithargsIgnorePattern: "^_"lets you write(_req, res) => ...in callbacks whose signature you can't change.no-consolewithallowflagsconsole.logbut permitsconsole.warnandconsole.error.
Run it against this file:
// src/app.js
export function handler(_event, context) {
let count = 0;
const unused = 42;
if (context.id == null) {
console.error("missing id");
}
console.log(count == 0);
return count;
}npx eslint src/app.jsThe output looks like this:
src/app.js
3:7 error 'count' is never reassigned. Use 'const' instead prefer-const
4:9 error 'unused' is assigned a value but never used no-unused-vars
8:3 warning Unexpected console statement no-console
8:21 error Expected '===' and instead saw '==' eqeqeq
✖ 4 problems (3 errors, 1 warning)_event is not reported because of the ignore pattern. context.id == null passes because of null: "ignore", and console.error passes because of allow.
How later config objects override earlier ones
When several config objects match a file, ESLint merges them in array order and later entries win. That is how the "no-empty": "off" line above beats the "error" setting from js.configs.recommended.
There is one detail worth knowing. If a later entry gives only a severity, ESLint keeps the options set by an earlier entry. If it gives an array with options, those options replace the earlier ones completely. ESLint does not merge the two option objects.
export default [
{ rules: { eqeqeq: ["error", "always", { null: "ignore" }] } },
// Keeps "always" and { null: "ignore" }, changes only severity
{ files: ["scripts/**"], rules: { eqeqeq: "warn" } },
// Replaces options entirely: null comparisons are now flagged again
{ files: ["legacy/**"], rules: { eqeqeq: ["warn", "always"] } },
];To see what ESLint actually resolved for a file, print the computed config:
npx eslint --print-config src/app.jsThe output is JSON. Its rules section shows each rule with its numeric severity and final options, which is the fastest way to settle "why is this rule still on?"
Deciding between warn and error
A useful default is to make a rule an error if a violation is almost always a bug or a real maintenance problem. Use warn only for a limited period while you clean up existing violations.
Teams often run into these problems:
- Warnings pile up. A codebase with 400 warnings teaches everyone to ignore the output, and new warnings go unnoticed among the old ones. Either fix them and upgrade the rule to error, or turn it off.
- Too many error rules too soon. Turning on a strict rule across a large codebase produces hundreds of failures at once. Start the rule as warn, fix the violations, then promote it.
- Formatting rules. Rules about spacing, semicolons, or quotes belong to Prettier. ESLint deprecated its core formatting rules in v8.53.0. A later chapter covers turning off any that remain.
- Typos in rule names. A misspelled rule such as
"no-unsued-vars"makes ESLint throw a "Could not find" error. Invalid options fail the rule's schema check in the same way. Both stop the run before any files are linted.
Making warnings count when you need them to
Two CLI flags let you treat warnings differently depending on where ESLint runs:
# Fail if there are any warnings at all (useful in CI)
npx eslint . --max-warnings 0
# Show only errors, hiding warnings (useful while cleaning up)
npx eslint . --quiet--max-warnings lets you ratchet the count down. Set it to your current number of warnings, and lower it as the team fixes them.
To try a rule before you commit to it, use --rule on the command line without editing the config:
npx eslint . --rule 'no-console: ["error", { "allow": ["warn", "error"] }]'Exercise
Your config has js.configs.recommended first and a later object with rules: { "no-unused-vars": "warn" }. A teammate then adds a third object with rules: { "no-unused-vars": ["error", { argsIgnorePattern: "^_" }] } and no files key. What is the final setting for src/index.js, and what happens to the "warn"?
Show answer
The final setting is ["error", { argsIgnorePattern: "^_" }]. All three objects match the file. The third one comes last and includes options, so it replaces both the severity and the options, and the earlier "warn" has no effect.
Exercise
A CI job runs npx eslint ., and the output shows 12 warnings and 0 errors. Does the job pass? How would you make it fail on these warnings without changing any rule severities?
Show answer
It passes, because warnings alone leave ESLint's exit code at 0. Run npx eslint . --max-warnings 0 instead. The run then exits with code 1 when there is at least one warning.
Exercise
You want no-console to flag console.log but allow console.error. You also want it to report without blocking commits while the team removes old log calls. Write the rule entry.
Show answer
"no-console": ["warn", { allow: ["error"] }]. Once the existing console.log calls are removed, change "warn" to "error".