Skip to content

Control flow rules

Limits nonempty switch cases, excluding the default clause and empty fallthrough labels.

On in the all config.

A large switch mixes many decisions in one place and makes changes harder to review.

Split the cases by responsibility, or use a lookup when every case performs the same operation.

With a limit of 30, a switch with 31 nonempty case clauses reports one finding.

Option 1: Whole number. Default: 30.

Reports comma expressions and combined OR values used as one switch case.

On in the recommended and all configs.

A case matches one expression result. Commas and OR operators do not list alternative matches.

Write separate case labels for each intended match. Keep intentional boolean cases in switch (true).

Use case "open": case "ready": instead of case "open" || "ready":.

Reports a branch of an if chain or switch that repeats an earlier branch, a chain whose branches all do the same, and a conditional expression whose two results are the same.

On in the recommended and all configs.

Two branches with the same code usually mean that one branch was copied and not changed, or that the condition does nothing.

Change the branch that should differ. When the branches really do the same, merge their conditions, or remove the condition.

if (isAdmin) { grant(user); } else { grant(user); } reports all. Write grant(user); without the condition.

Reports a for loop that steps its counter with ++, --, +=, or -= and stops on an equality test of that counter.

On in the recommended and all configs.

A counter that steps past its end value never equals it, so the loop does not stop.

Compare the counter with <, <=, >, or >=.

for (let index = 0; index !== limit; index += 2) reports equality. Write index < limit instead.

Reports ignored or omitted catch parameters around multiple statements, blocks, loops, or switches.

On in the recommended and all configs.

Discarding exceptions from a larger operation can hide which step failed and leave incomplete work.

Handle or rethrow the exception, narrow the try body, or document why an empty catch intentionally ignores it.

catch (error) { report(error); } handles the caught exception.

Reports a for loop whose update and body change none of the values that its stop condition reads.

On in the recommended and all configs.

A loop that updates one value and tests another usually updates the wrong counter, and may never stop.

Update the value that the condition tests, or test the value that the update changes.

for (let row = 0; row < rows; column++) reports misplaced. Update row instead.

Reports a switch inside another switch, including nested function declarations.

On in the all config.

Nested switch statements make a reader follow two sets of cases at once.

Move the inner decision into a separate function declared outside the outer switch.

switch (kind) { case "file": switch (format) { case "text": readText(); } } reports the inner switch. Call a function that selects the reader for the format instead.

Reports a switch with fewer than two cases, or with one case and a default. A default that only proves the switch exhaustive, with a never variable or a call that takes never, does not count.

On in the all config.

A switch of one decision reads as a list of choices that does not exist.

Replace the switch with an if statement, or with if and else.

switch (mode) { case "dark": paint(); } reports small. Write if (mode === "dark") paint();.

Reports statement labels inside switch cases, up to an ordinary function boundary.

On in the recommended and all configs.

A statement label can look like another case and hide which statements the switch executes.

Remove the label, or write a separate case clause when it represents another switch value.

switch (value) { case 1: retry: while (ready) run(); } reports the retry label.

Reports discarded constructor calls whose names end with Error or Exception.

On in the recommended and all configs.

Constructing an error without throwing or retaining it does not stop the failing operation.

Throw the error, return it, or store it for the caller that handles the failure.

new TypeError("Invalid value"); reports a finding. Use throw new TypeError("Invalid value");.

Reports a plain assignment to a for loop counter inside the loop body, and an update of the counter in the update clause of a nested loop.

On in the recommended and all configs.

Setting the counter inside the body hides how the loop advances, and often makes it skip items or never stop.

Change the counter only in the update clause, or use a while loop when the body decides how far to move.

for (let index = 0; index < size; index++) { index = next(index); } reports counter.