Skip to content

eslint/no-dupe-else-if Correctness

This rule is turned on by default.

What it does

Disallow duplicate conditions in if-else-if chains

Why is this bad?

if-else-if chains are commonly used when there is a need to execute only one branch (or at most one branch) out of several possible branches, based on certain conditions. Two identical test conditions in the same chain are almost always a mistake in the code. Unless there are side effects in the expressions, a duplicate will evaluate to the same true or false value as the identical expression earlier in the chain, meaning that its branch can never execute.

Example

javascript
if (a) {
  foo();
} else if (b) {
  bar();
} else if (b) {
  baz();
}

How to use

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

bash
oxlint --deny no-dupe-else-if
json
{
  "rules": {
    "no-dupe-else-if": "error"
  }
}

References

Released under the MIT License.