liveBook · Manning

Linter rule

As explained in Secrets of the JavaScript Ninja, Third Edition

A configurable static-analysis condition that determines when a linter reports a source-code construct.

A linter rule is a configurable condition that tells a linter when to report a source-code construct. Rules can catch likely mistakes, discourage patterns that make code harder to read, or enforce a team’s style choices. section 1.3.2 section 2.2.3

For example, ESLint’s no-shadow rule reports a variable declared with the same name as one in an outer scope. Shadowing is legal JavaScript, but can make it harder to track which variable a name refers to. Rules can also target mistakes such as unreachable code, library-specific practices, or comment capitalization; some style rules can automatically fix the code. section 1.3.2 section 2.2.3

Listing 2.11 Shadowing a variable
const count = 10; #A
 
console.log("Outer count:", count);
 
for (let count = 0; count < 3; count++) { #B
  console.log("Inner count:", count); #C
}
 
console.log("Outer count still:", count);

Compare the outer and loop-local count variables to see the kind of shadowing no-shadow flags. section 2.2.3

Choosing rules involves a tradeoff: more rules can catch more errors and keep style cohesive, but opinionated rules may flag code that is valid and intentional. A project can disable a rule in its ESLint configuration, or an individual use can be exempted with an explanatory comment. Alternatively, code can be rewritten to comply. section 2.2.2

How Secrets of the JavaScript Ninja, Third Edition covers it

How deep it goes
Explains rule effects through code examples, project configuration, and inline exceptions.
Examples
Uses unreachable code, shadowing, non-null assertions, and comment capitalization as rule examples.
What you can do after
Configure ESLint rules at project or line level, or rewrite code to satisfy them.
How it connects
Connects ESLint rules with JavaScript shadowing, TypeScript assertions, code style, and automatic formatting.

Also explained in

Compare how 6 books explain linter rule →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Secrets of the JavaScript Ninja, Third Edition. The section links point to the parts of the book it draws on.