Skip to content

noDuplicateIfBranches (JavaScript)

Language JavaScript (and super languages)
biome.json
{
"linter": {
"rules": {
"nursery": {
"noDuplicateIfBranches": "error"
}
}
}
}

Disallow two branches next to each other in an if statement from running the same code.

When two branches that follow each other in an if, else if, else chain contain the same code, the condition that chooses between them makes no difference. This is often a copy-paste mistake, or a leftover from an unfinished change.

Only branches next to each other are compared. Two branches with the same code that are separated by a different branch are allowed, because the branch in between can change which one runs.

if (isAdmin) {
showDashboard();
loadStats();
} else {
showDashboard();
loadStats();
}
code-block.js:4:8 lint/nursery/noDuplicateIfBranches ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ This branch runs the same code as the branch before it.

2 │ showDashboard();
3 │ loadStats();
> 4 │ } else {
│ ^
> 5 │ showDashboard();
> 6 │ loadStats();
> 7 │ }
│ ^
8 │

ℹ The branch before it is here:

> 1 │ if (isAdmin) {
│ ^
> 2 │ showDashboard();
> 3 │ loadStats();
> 4 │ } else {
│ ^
5 │ showDashboard();
6 │ loadStats();

ℹ The condition that chooses between these branches makes no difference, which is often a copy-paste mistake.

ℹ Change the code of one of the branches, or merge the branches into one.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

if (value === "a") {
doThing();
} else if (value === "b") {
doThing();
}
code-block.js:3:27 lint/nursery/noDuplicateIfBranches ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ This branch runs the same code as the branch before it.

1 │ if (value === "a") {
2 │ doThing();
> 3 │ } else if (value === "b") {
│ ^
> 4 │ doThing();
> 5 │ }
│ ^
6 │

ℹ The branch before it is here:

> 1 │ if (value === "a") {
│ ^
> 2 │ doThing();
> 3 │ } else if (value === "b") {
│ ^
4 │ doThing();
5 │ }

ℹ The condition that chooses between these branches makes no difference, which is often a copy-paste mistake.

ℹ Change the code of one of the branches, or merge the branches into one.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

if (isAdmin) {
showDashboard();
loadStats();
} else {
showLogin();
}
if (value === "a" || value === "b") {
doThing();
}
if (format === "json") {
parseJson();
} else if (format === "yaml") {
parseYaml();
} else {
parseJson();
}
  • If you want to disallow the same condition appearing twice in an if statement, see noDuplicateElseIf.
  • If you want to disallow the same code at the start or end of every branch of an if statement, see noSharedBranchCode.