Skip to content
public-library›Documentationpractices/documentation

Give every lint or type-checker suppression a reason

Add to cart

What would you like to add?

At checkout, you'll pick which project these go into, and you can fork a rule instead of staying in sync.

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 nolintlint with require-explanation and require-specific, typescript-eslint's ban-ts-comment with allow-with-description, and eslint-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.