Control flow rules
max-switch-cases
Section titled “max-switch-cases”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.
What to do
Section titled “What to do”Split the cases by responsibility, or use a lookup when every case performs the same operation.
Example
Section titled “Example”With a limit of 30, a switch with 31 nonempty case clauses reports one finding.
Options
Section titled “Options”Option 1: Whole number. Default: 30.
no-compound-case
Section titled “no-compound-case”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.
What to do
Section titled “What to do”Write separate case labels for each intended match. Keep intentional boolean cases in switch (true).
Example
Section titled “Example”Use case "open": case "ready": instead of case "open" || "ready":.
no-duplicated-branches
Section titled “no-duplicated-branches”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.
What to do
Section titled “What to do”Change the branch that should differ. When the branches really do the same, merge their conditions, or remove the condition.
Example
Section titled “Example”if (isAdmin) { grant(user); } else { grant(user); } reports all. Write grant(user); without the condition.
no-equal-loop-condition
Section titled “no-equal-loop-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.
What to do
Section titled “What to do”Compare the counter with <, <=, >, or >=.
Example
Section titled “Example”for (let index = 0; index !== limit; index += 2) reports equality. Write index < limit instead.
no-ignored-catch
Section titled “no-ignored-catch”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.
What to do
Section titled “What to do”Handle or rethrow the exception, narrow the try body, or document why an empty catch intentionally ignores it.
Example
Section titled “Example”catch (error) { report(error); } handles the caught exception.
no-misplaced-loop-counter
Section titled “no-misplaced-loop-counter”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.
What to do
Section titled “What to do”Update the value that the condition tests, or test the value that the update changes.
Example
Section titled “Example”for (let row = 0; row < rows; column++) reports misplaced. Update row instead.
no-nested-switch
Section titled “no-nested-switch”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.
What to do
Section titled “What to do”Move the inner decision into a separate function declared outside the outer switch.
Example
Section titled “Example”switch (kind) { case "file": switch (format) { case "text": readText(); } } reports the inner switch. Call a function that selects the reader for the format instead.
no-small-switch
Section titled “no-small-switch”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.
What to do
Section titled “What to do”Replace the switch with an if statement, or with if and else.
Example
Section titled “Example”switch (mode) { case "dark": paint(); } reports small. Write if (mode === "dark") paint();.
no-switch-case-labels
Section titled “no-switch-case-labels”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.
What to do
Section titled “What to do”Remove the label, or write a separate case clause when it represents another switch value.
Example
Section titled “Example”switch (value) { case 1: retry: while (ready) run(); } reports the retry label.
no-unthrown-error
Section titled “no-unthrown-error”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.
What to do
Section titled “What to do”Throw the error, return it, or store it for the caller that handles the failure.
Example
Section titled “Example”new TypeError("Invalid value"); reports a finding. Use throw new TypeError("Invalid value");.
no-updated-loop-counter
Section titled “no-updated-loop-counter”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.
What to do
Section titled “What to do”Change the counter only in the update clause, or use a while loop when the body decides how far to move.
Example
Section titled “Example”for (let index = 0; index < size; index++) { index = next(index); } reports counter.