# no-duplicated-branches

Rule `sonarjs/no-duplicated-branches`. Two branches in a conditional structure should not have exactly the same implementation

- Source: sonarjs
- Technologies: JavaScript
- Kind: style
- Needs type information: no
- Autofix: no
- Reach: 6.2M weekly downloads of presets that enable it
- Trend: +62.1% last quarter
- Rank: #1257 by reach, #558 by growth

## Linters

| Linter | Rule name | Recommended | Autofix | Details | Docs |
| --- | --- | --- | --- | --- | --- |
| ESLint | `sonarjs/no-duplicated-branches` | yes | no | package `eslint-plugin-sonarjs` | [docs](https://rules.sonarsource.com/javascript/RSPEC-1871/) |

## Code examples

### Incorrect: the rule reports this

```js
switch (i) {
  case 1:
    doFirstThing();
    doSomething();
    break;
  case 2:
    doSomethingDifferent();
    break;
  case 3:  // Noncompliant; duplicates case 1's implementation
    doFirstThing();
    doSomething();
    break;
  default:
    doTheRest();
}

if (a >= 0 && a < 10) {
  doFirstThing();
  doTheThing();
}
else if (a >= 10 && a < 20) {
  doTheOtherThing();
}
else if (a >= 20 && a < 50) {
  doFirstThing();
  doTheThing();  // Noncompliant; duplicates first condition
}
else {
  doTheRest();
}
```

Examples from the ESLint documentation.

## Enable it

### ESLint: `eslint.config.js`

```js
export default [
  {
    rules: {
      "sonarjs/no-duplicated-branches": "error",
    },
  },
];
```

## Presets that enable it

| Preset | Linter | Package | Rules | Weekly downloads | Trend |
| --- | --- | --- | --- | --- | --- |
| [SonarJS recommended](https://lintradar.dev/presets/sonarjs-recommended.md) | ESLint | `eslint-plugin-sonarjs` | 230 | 6.2M | +62.1% |

## Related rules

- [sonarjs/no-duplicate-test-title](https://lintradar.dev/rules/sonarjs/no-duplicate-test-title.md): Test titles should be unique within the same suite
- [sonarjs/no-element-overwrite](https://lintradar.dev/rules/sonarjs/no-element-overwrite.md): Collection elements should not be replaced unconditionally
- [sonarjs/no-duplicate-parameterized-test-case](https://lintradar.dev/rules/sonarjs/no-duplicate-parameterized-test-case.md): Parameterized tests should not contain duplicate test cases
- [sonarjs/no-empty-after-reluctant](https://lintradar.dev/rules/sonarjs/no-empty-after-reluctant.md): Reluctant quantifiers in regular expressions should be followed by an expression that can't match the empty string
- [sonarjs/no-duplicate-in-composite](https://lintradar.dev/rules/sonarjs/no-duplicate-in-composite.md): Union and intersection types should not include duplicated constituents
- [sonarjs/no-empty-alternatives](https://lintradar.dev/rules/sonarjs/no-empty-alternatives.md): Alternation in regular expressions should not contain empty alternatives

---

Web page: https://lintradar.dev/rules/sonarjs/no-duplicated-branches. Data from npm and the linters' documentation, collected Dec 4, 2026. Site guide for agents: https://lintradar.dev/llms.txt
