noDuplicateIfBranches (JavaScript)
Language JavaScript (and super languages)
Summary
Section titled “Summary”- Diagnostic Category:
lint/nursery/noDuplicateIfBranches - This rule doesn’t have a fix.
- The default severity of this rule is warning.
- Sources:
- Inspired from
unicorn/no-duplicate-if-branches
- Inspired from
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noDuplicateIfBranches": "error" } } }}Description
Section titled “Description”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.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”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();}See Also
Section titled “See Also”- If you want to disallow the same condition appearing twice in an
ifstatement, seenoDuplicateElseIf. - If you want to disallow the same code at the start or end of every branch of an
ifstatement, seenoSharedBranchCode.
Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.