noDuplicateElseIf
Este conteúdo não está disponível em sua língua ainda.
Diagnostic Category: lint/nursery/noDuplicateElseIf
Since: v1.6.2
Sources:
- Same as:
no-dupe-else-if
Description
Section titled DescriptionDisallow duplicate conditions in if-else-if chains
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.
Please note that this rule does not compare conditions from the chain with conditions inside statements
Examples
Section titled ExamplesInvalid
Section titled Invalidif (a) { foo();} else if (b) { bar();} else if (b) { baz();}
code-block.js:5:12 lint/nursery/noDuplicateElseIf ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ This branch can never execute. Its condition is a duplicate or covered by previous conditions in the if-else-if chain.
3 │ } else if (b) {
4 │ bar();
> 5 │ } else if (b) {
│ ^
6 │ baz();
7 │ }
Valid
Section titled Validif (a) { foo();} else if (b) { bar();} else if (c) { baz();}
How to configure
Section titled How to configure{ "linter": { "rules": { "nursery": { "noDuplicateElseIf": "error" } } }}