Skip to content
← Back to rules

oxc/branches-sharing-code Pedantic

💡 A suggestion is available for this rule.

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
if (condition) {
  console.log("Hello");
  return 13;
} else {
  console.log("Hello");
  return 42;
}

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

Examples of correct code for this rule:

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

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

How to use ​

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

json
{
  "rules": {
    "oxc/branches-sharing-code": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "oxc/branches-sharing-code": "error",
  },
});
bash
oxlint --deny oxc/branches-sharing-code

Version ​

This rule was added in v1.22.0.

References ​