Give every lint or type-checker suppression a reason
In cart Checkout
When to apply
Before adding, changing, or reviewing a comment that disables a linter or type-checker check, such as //nolint, // eslint-disable-next-line, @ts-expect-error, # noqa, or # type: ignore.
Every comment that disables a lint or type-checker check names the specific check it disables and says why the code is right despite it.
Implementation
- Name the one check, not a whole category:
//nolint:errcheck, not//nolint. - Put the reason on the same comment, in a sentence a reviewer can verify, such as the upstream bug or the invariant that makes the code safe.
- Suppress the narrowest scope: one line rather than a block, and a block rather than a file.
- Enforce it with the tool where you can: Go's
nolintlintwithrequire-explanationandrequire-specific, typescript-eslint'sban-ts-commentwithallow-with-description, andeslint-comments/require-description.
Examples
Application: A Go linter suppression
Incorrect (counterexample):
defer f.Close() //nolint
The comment disables every linter on the line and gives no reason.
Correct:
defer f.Close() //nolint:errcheck // Read-only file; a close error can't lose data.
Application: A TypeScript type error
Incorrect (counterexample):
// @ts-expect-error
chart.render(options);
Correct:
// @ts-expect-error The chart library's types omit `animate`; fixed upstream in v5 (https://github.com/example/chart/issues/412).
chart.render(options);
Validation
Search changed files for suppression comments and check that each names one check and gives a reason a reviewer can verify.