liveBook · Manning

ESLint

As explained in Secrets of the JavaScript Ninja, Third Edition

An open-source linter for JavaScript and TypeScript with configurable rules and automatic fixes.

JavaScript can allow a mistake to survive until code runs. ESLint checks code without running it, applying configurable rules that catch likely errors or enforce a project’s coding conventions. It complements TypeScript’s type checking and tests rather than replacing either. section 2.0 section 2.2

Rules range from no-unreachable, which flags code after a return, to library-specific checks such as react/no-deprecated. Style rules can enforce team preferences; some can automatically fix code instead of merely reporting an error. Prettier has a different primary job: formatting details such as line breaks and indentation. section 1.3.2 section 2.4

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);

The loop’s count shadows the outer count. JavaScript permits this, but readers must track which variable each reference means. ESLint’s no-shadow rule can flag the pattern; it must be enabled separately because it is not in the recommended rule set. section 2.2.3

More rules can catch more problems and keep a team’s style cohesive, but opinionated rules can also reject valid code. For example, the typescript-eslint strict rules forbid non-null assertions even when a preceding check establishes that a value exists. You can rewrite the code, turn off the rule in the project configuration, or make a local exception. section 2.2.2

/* eslint-disable-next-line @typescript-eslint/no-non-null-assertion --
 * The previous condition ensured that dojoStudents.get(dojoName) exists.
 */
dojoStudents.set(dojoName, dojoStudents.get(dojoName)! + 1);

A local eslint-disable-next-line comment makes an exception for one non-null assertion and records why the preceding condition makes it safe here. section 2.2.2

Choose rules for the mistakes and conventions that matter to your project. Type-aware rules from typescript-eslint expand what ESLint can check, though they increase linting time. section 2.2.3 section 2.4

How Secrets of the JavaScript Ninja, Third Edition covers it

How deep it goes
Explains rule examples, configuration choices, local exceptions, and the tradeoff between broader checks and unwanted warnings; does not detail ESLint internals.
Examples
Uses shadowed variables, unreachable code, deprecated React methods, and a non-null assertion rejected by a strict rule.
What it assumes
Assumes a Node.js environment with node, npm, and npx available, then introduces ESLint setup in a TypeScript project.
What you can do after
Operate/configure it to select rules, run a lint check, and justify either project-wide or local exceptions.
How it connects
Connects ESLint to static analysis, TypeScript, typescript-eslint, tests, Prettier, and variable shadowing.

Also explained in

Compare how 3 books explain ESLint →

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.