Skip to content

New rule proposal: no-dupe-else-if #12469

@mdjermanovic

Description

@mdjermanovic

Please describe what the rule should do:

Disallows duplicate test conditions in if-else-if chains.

What category of rule is this? (place an "X" next to just one item)

[X] Warns about a potential error (problem)

Provide 2-3 code examples that this rule will warn about:

if (a > 1) {
    foo();
} else if (a > 1) {
    bar();
}

if (a === 1) {
    foo();
} else if (a === 2) {
    bar();
} else if (a === 3) { 
    baz();
} else if (a === 2) {
    quux();
} else if (a === 5) {
    quuux();
}

Why should this rule be included in ESLint (instead of a plugin)?

It's a possible error. A statement with a duplicate condition can never execute (unless it's an unusual use case with side effects).

This rule is similar to the existing no-duplicate-case. The error could be due to a copy & paste and a long chain.

The test nodes would be compared by their tokens.

Are you willing to submit a pull request to implement this rule?

Yes.

Metadata

Metadata

Assignees

Labels

acceptedThere is consensus among the team that this change meets the criteria for inclusionarchived due to ageThis issue has been archived; please open a new issue for any further discussionfeatureThis change adds a new feature to ESLintruleRelates to ESLint's core rules

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions