Sub topic 1
Linting versus formatting: what each check catches
Prerequisites
This chapter assumes you have:
- Used Git and opened a GitHub pull request.
- Run commands in a terminal.
- Used a linter and a formatter in one language, such as ESLint and Prettier, or Ruff.
- Read basic YAML.
The examples use JavaScript tools. The ideas apply to any language.
Two checks, two questions
A linter and a formatter both read your source files and report problems. They answer different questions.
- A linter analyzes code for likely bugs and risky patterns. Examples are an unused variable, a loose
==comparison, or an unreachable branch. It asks, "Is this code probably wrong or unsafe?" - A formatter rewrites code into one consistent layout. It handles indentation, quote style, line length, and trailing commas. It asks, "Does this code look the way the team agreed?"
A formatter never changes what the program does. A linter can point at behavior.
What each one catches
Consider this file:
const a = 1;
const b = "1";
const unused = 42;
if (a == b) {
console.log( 'equal' )
}ESLint, with a typical rule set, reports problems with the logic:
unusedis assigned but never used (no-unused-vars).==allows type coercion, so1 == "1"is true (eqeqeq).
Prettier ignores both. It reports style differences:
- Spaces inside
console.log( ... ). - Single quotes, if the project uses double quotes.
- A missing semicolon after the
console.logcall.
Neither tool covers the other's job, so a project needs both.
| Aspect | Linter | Formatter |
|---|---|---|
| Main concern | Correctness and risky patterns | Layout and style |
| Typical findings | Unused variables, loose equality | Spacing, quotes, line wrapping |
| Can change behavior if auto-fixed | Sometimes | No |
| Common tools | ESLint, Ruff (`check`) | Prettier, Ruff (`format`) |
Check-only mode in CI
Both kinds of tool can usually rewrite files or just report problems. These are two modes of the same tool:
- Fix mode edits files in place. It suits your editor and your local machine.
- Check mode reads files, prints problems, and exits with a non-zero status if it finds any. It leaves the files untouched.
CI should use check mode. A CI runner works on a temporary copy of the repository. If a tool rewrote files there, the fixes would be thrown away when the job ended. The job could also pass even though the code in the pull request is still wrong. Check mode turns a problem into a failed job, and the author fixes it and pushes again.
Prettier's check mode is prettier --check .. Ruff's is ruff format --check .. ESLint reports problems by default and only edits files when you pass --fix.
Why run them as separate checks
Lint failures and format failures have different causes and fixes. A lint failure usually needs you to think about the code. A format failure is fixed by running the formatter. If both run as one command, a reviewer cannot tell at a glance whether the pull request has a real defect or only a whitespace issue. Later chapters turn each check into its own script and its own CI job.
Exercise
A teammate's pull request adds a variable that is never read, and a line indented with tabs when the project uses two spaces. Which check flags each problem, and why should CI run both in check mode?
Show answer
The linter flags the unused variable, because it is a likely mistake. The formatter flags the tab indentation, because it is a layout difference. CI runs both in check mode so they only report problems and exit non-zero, which fails the job. Fix mode would edit files on a temporary runner copy that is discarded, and the pull request would still contain the problems.