Skip to content

oxc/branches-sharing-code Nursery

What it does

Checks if the if and else blocks contain shared code that can be moved out of the blocks.

Why is this bad?

Duplicate code is less maintainable. Extracting common code from branches makes the code more DRY (Don't Repeat Yourself) and easier to maintain.

Examples

Examples of incorrect code for this rule:

javascript
let foo = if (condition) {
    console.log("Hello");
    13
} else {
    console.log("Hello");
    42
};

if (condition) {
    doSomething();
    cleanup();
} else {
    doSomethingElse();
    cleanup();
}

Examples of correct code for this rule:

javascript
console.log("Hello");
let foo = if (condition) {
    13
} else {
    42
};

if (condition) {
    doSomething();
} else {
    doSomethingElse();
}
cleanup();

How to use

To enable this rule in the CLI or using the config file, you can use:

bash
oxlint --deny oxc/branches-sharing-code
json
{
  "rules": {
    "oxc/branches-sharing-code": "error"
  }
}

References

Released under the MIT License.