noSharedBranchCode (JavaScript)
Summary
Section titled “Summary”- Rule available since:
v2.6.6 - Diagnostic Category:
lint/nursery/noSharedBranchCode - This rule doesn’t have a fix.
- The default severity of this rule is information.
- Sources:
- Inspired from
oxc/branches-sharing-code
- Inspired from
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noSharedBranchCode": "error" } } }}Description
Section titled “Description”Disallow the same code at the start or end of every branch of an if statement.
When every branch of an if statement starts or ends with the same code,
that code runs no matter which branch is taken.
Repeating it in each branch makes the code longer, and a later change to it has to be made in every copy.
Move the shared code before or after the if statement instead.
The rule only checks if statements that end with an else branch,
including chains of else if branches, where every branch is a block wrapped in curly braces.
The rule doesn’t report shared code that can’t be moved out of the branches without changing what it does:
- shared code that uses a variable declared elsewhere in the same branch;
- shared code at the start of the branches that assigns a variable, or a property of a variable, used in a condition of the
ifstatement, because moving it before theifstatement changes the result of the condition.
Moving shared code before the if statement makes it run before the conditions are checked, instead of after.
When a condition calls a function, check that the function doesn’t depend on the shared code before moving it.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”if (condition) { console.log("Hello"); doSomething();} else { console.log("Hello"); doSomethingElse();}code-block.js:2:5 lint/nursery/noSharedBranchCode ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Every branch of this if statement starts with the same code.
1 │ if (condition) {
> 2 │ console.log("Hello");
│ ^^^^^^^^^^^^^^^^^^^^^
3 │ doSomething();
4 │ } else {
ℹ This code runs no matter which branch is taken. Repeating it in each branch makes every change to it happen in several places.
ℹ Move the shared code before the if statement.
ℹ 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 (condition) { doSomething(); cleanup();} else { doSomethingElse(); cleanup();}code-block.js:3:5 lint/nursery/noSharedBranchCode ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Every branch of this if statement ends with the same code.
1 │ if (condition) {
2 │ doSomething();
> 3 │ cleanup();
│ ^^^^^^^^^^
4 │ } else {
5 │ doSomethingElse();
ℹ This code runs no matter which branch is taken. Repeating it in each branch makes every change to it happen in several places.
ℹ Move the shared code after the if statement.
ℹ 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.
console.log("Hello");if (condition) { doSomething();} else { doSomethingElse();}
if (condition) { doSomething();} else { doSomethingElse();}cleanup();
if (condition) { const message = "yes"; console.log(message);} else { const message = "no"; console.log(message);}Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.