New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New: Add no-dupe-else-if rule (fixes #12469) #12504
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
# Disallow duplicate conditions in `if-else-if` chains (no-dupe-else-if) | ||
|
||
`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. | ||
|
||
```js | ||
if (a) { | ||
foo(); | ||
} else if (b) { | ||
bar(); | ||
} else if (c) { | ||
baz(); | ||
} | ||
``` | ||
|
||
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. | ||
|
||
```js | ||
if (a) { | ||
foo(); | ||
} else if (b) { | ||
bar(); | ||
} else if (b) { | ||
baz(); | ||
} | ||
``` | ||
|
||
In the above example, `baz()` can never execute. Obviously, `baz()` could be executed only when `b` evaluates to `true`, but in that case `bar()` would be executed instead, since it's earlier in the chain. | ||
|
||
## Rule Details | ||
|
||
This rule disallows duplicate conditions in the same `if-else-if` chain. | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```js | ||
/*eslint no-dupe-else-if: "error"*/ | ||
|
||
if (isSomething(x)) { | ||
foo(); | ||
} else if (isSomething(x)) { | ||
bar(); | ||
} | ||
|
||
if (a) { | ||
foo(); | ||
} else if (b) { | ||
bar(); | ||
} else if (c && d) { | ||
baz(); | ||
} else if (c && d) { | ||
quux(); | ||
} else { | ||
quuux(); | ||
} | ||
|
||
if (n === 1) { | ||
foo(); | ||
} else if (n === 2) { | ||
bar(); | ||
} else if (n === 3) { | ||
baz(); | ||
} else if (n === 2) { | ||
quux(); | ||
} else if (n === 5) { | ||
quuux(); | ||
} | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```js | ||
/*eslint no-dupe-else-if: "error"*/ | ||
|
||
if (isSomething(x)) { | ||
foo(); | ||
} else if (isSomethingElse(x)) { | ||
bar(); | ||
} | ||
|
||
if (a) { | ||
foo(); | ||
} else if (b) { | ||
bar(); | ||
} else if (c && d) { | ||
baz(); | ||
} else if (c && e) { | ||
quux(); | ||
} else { | ||
quuux(); | ||
} | ||
|
||
if (n === 1) { | ||
foo(); | ||
} else if (n === 2) { | ||
bar(); | ||
} else if (n === 3) { | ||
baz(); | ||
} else if (n === 4) { | ||
quux(); | ||
} else if (n === 5) { | ||
quuux(); | ||
} | ||
``` | ||
|
||
Please note that this rule does not compare conditions from the chain with conditions inside statements, and will not warn in the cases such as follows: | ||
|
||
```js | ||
if (a) { | ||
if (a) { | ||
foo(); | ||
} | ||
} | ||
|
||
if (a) { | ||
foo(); | ||
} else { | ||
if (a) { | ||
bar(); | ||
} | ||
} | ||
``` | ||
|
||
## When Not To Use It | ||
|
||
In rare cases where you really need identical test conditions in the same chain, which necessarily means that the expressions in the chain are causing and relying on side effects, you will have to turn this rule off. | ||
|
||
## Related Rules | ||
|
||
* [no-duplicate-case](no-duplicate-case.md) | ||
* [no-lonely-if](no-lonely-if.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/** | ||
* @fileoverview Rule to disallow duplicate conditions in if-else-if chains | ||
* @author Milos Djermanovic | ||
*/ | ||
|
||
"use strict"; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Requirements | ||
//------------------------------------------------------------------------------ | ||
|
||
const astUtils = require("./utils/ast-utils"); | ||
|
||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
type: "problem", | ||
|
||
docs: { | ||
description: "disallow duplicate conditions in if-else-if chains", | ||
category: "Possible Errors", | ||
recommended: false, | ||
url: "https://eslint.org/docs/rules/no-dupe-else-if" | ||
}, | ||
|
||
schema: [], | ||
|
||
messages: { | ||
unexpected: "Duplicate condition in if-else-if chain." | ||
} | ||
}, | ||
|
||
create(context) { | ||
const sourceCode = context.getSourceCode(); | ||
|
||
return { | ||
IfStatement(node) { | ||
let current = node; | ||
|
||
while (current.parent && current.parent.type === "IfStatement" && current.parent.alternate === current) { | ||
current = current.parent; | ||
|
||
if (astUtils.equalTokens(node.test, current.test, sourceCode)) { | ||
context.report({ node: node.test, messageId: "unexpected" }); | ||
break; | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
/** | ||
* @fileoverview Tests for the no-dupe-else-if rule | ||
* @author Milos Djermanovic | ||
*/ | ||
|
||
"use strict"; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Requirements | ||
//------------------------------------------------------------------------------ | ||
|
||
const rule = require("../../../lib/rules/no-dupe-else-if"); | ||
const { RuleTester } = require("../../../lib/rule-tester"); | ||
|
||
//------------------------------------------------------------------------------ | ||
// Tests | ||
//------------------------------------------------------------------------------ | ||
|
||
const ruleTester = new RuleTester(); | ||
|
||
ruleTester.run("no-dupe-else-if", rule, { | ||
valid: [ | ||
|
||
// different test conditions | ||
"if (a) {} else if (b) {}", | ||
"if (a); else if (b); else if (c);", | ||
"if (true) {} else if (false) {} else {}", | ||
"if (1) {} else if (2) {}", | ||
"if (f) {} else if (f()) {}", | ||
"if (f(a)) {} else if (g(a)) {}", | ||
"if (f(a)) {} else if (f(b)) {}", | ||
"if (a === 1) {} else if (a === 2) {}", | ||
"if (a === 1) {} else if (b === 1) {}", | ||
|
||
// not an if-else-if chain | ||
"if (a) {}", | ||
"if (a);", | ||
"if (a) {} else {}", | ||
"if (a) if (a) {}", | ||
"if (a) if (a);", | ||
"if (a) { if (a) {} }", | ||
"if (a) {} else { if (a) {} }", | ||
"if (a) {} if (a) {}", | ||
"if (a); if (a);", | ||
"while (a) if (a);", | ||
"if (a); else a ? a : a;", | ||
|
||
// not same conditions in the chain | ||
"if (a) { if (b) {} } else if (b) {}", | ||
"if (a) if (b); else if (a);", | ||
|
||
// not equal tokens | ||
"if (a) {} else if (!!a) {}", | ||
"if (a === 1) {} else if (a === (1)) {}" | ||
], | ||
|
||
invalid: [ | ||
|
||
// basic tests | ||
{ | ||
code: "if (a) {} else if (a) {}", | ||
errors: [{ messageId: "unexpected", type: "Identifier", column: 20 }] | ||
}, | ||
{ | ||
code: "if (a); else if (a);", | ||
errors: [{ messageId: "unexpected", type: "Identifier", column: 18 }] | ||
}, | ||
{ | ||
code: "if (a) {} else if (a) {} else {}", | ||
errors: [{ messageId: "unexpected", type: "Identifier", column: 20 }] | ||
}, | ||
{ | ||
code: "if (a) {} else if (b) {} else if (a) {} else if (c) {}", | ||
errors: [{ messageId: "unexpected", type: "Identifier", column: 35 }] | ||
}, | ||
{ | ||
code: "if (a) {} else if (b) {} else if (a) {}", | ||
errors: [{ messageId: "unexpected", type: "Identifier", column: 35 }] | ||
}, | ||
{ | ||
code: "if (a) {} else if (b) {} else if (c) {} else if (a) {}", | ||
errors: [{ messageId: "unexpected", type: "Identifier", column: 50 }] | ||
}, | ||
{ | ||
code: "if (a) {} else if (b) {} else if (b) {}", | ||
errors: [{ messageId: "unexpected", type: "Identifier", column: 35 }] | ||
}, | ||
{ | ||
code: "if (a) {} else if (b) {} else if (b) {} else {}", | ||
errors: [{ messageId: "unexpected", type: "Identifier", column: 35 }] | ||
}, | ||
{ | ||
code: "if (a) {} else if (b) {} else if (c) {} else if (b) {}", | ||
errors: [{ messageId: "unexpected", type: "Identifier", column: 50 }] | ||
}, | ||
{ | ||
code: "if (a); else if (b); else if (c); else if (b); else if (d); else;", | ||
errors: [{ messageId: "unexpected", type: "Identifier", column: 44 }] | ||
}, | ||
{ | ||
code: "if (a); else if (b); else if (c); else if (d); else if (b); else if (e);", | ||
errors: [{ messageId: "unexpected", type: "Identifier", column: 57 }] | ||
}, | ||
|
||
// multiple duplicates of the same condition | ||
{ | ||
code: "if (a) {} else if (a) {} else if (a) {}", | ||
errors: [ | ||
{ messageId: "unexpected", type: "Identifier", column: 20 }, | ||
{ messageId: "unexpected", type: "Identifier", column: 35 } | ||
] | ||
}, | ||
|
||
// multiple duplicates of different conditions | ||
{ | ||
code: "if (a) {} else if (b) {} else if (a) {} else if (b) {} else if (a) {}", | ||
errors: [ | ||
{ messageId: "unexpected", type: "Identifier", column: 35 }, | ||
{ messageId: "unexpected", type: "Identifier", column: 50 }, | ||
{ messageId: "unexpected", type: "Identifier", column: 65 } | ||
] | ||
}, | ||
|
||
// inner if statements do not affect chain | ||
{ | ||
code: "if (a) { if (b) {} } else if (a) {}", | ||
errors: [{ messageId: "unexpected", type: "Identifier", column: 31 }] | ||
}, | ||
|
||
// various kinds of test conditions | ||
{ | ||
code: "if (a === 1) {} else if (a === 1) {}", | ||
errors: [{ messageId: "unexpected", type: "BinaryExpression", column: 26 }] | ||
}, | ||
{ | ||
code: "if (1 < a) {} else if (1 < a) {}", | ||
errors: [{ messageId: "unexpected", type: "BinaryExpression", column: 24 }] | ||
}, | ||
{ | ||
code: "if (true) {} else if (true) {}", | ||
errors: [{ messageId: "unexpected", type: "Literal", column: 23 }] | ||
}, | ||
{ | ||
code: "if (a && b) {} else if (a && b) {}", | ||
errors: [{ messageId: "unexpected", type: "LogicalExpression", column: 25 }] | ||
}, | ||
{ | ||
code: "if (a && b || c) {} else if (a && b || c) {}", | ||
errors: [{ messageId: "unexpected", type: "LogicalExpression", column: 31 }] | ||
}, | ||
{ | ||
code: "if (f(a)) {} else if (f(a)) {}", | ||
errors: [{ messageId: "unexpected", type: "CallExpression", column: 23 }] | ||
}, | ||
|
||
// spaces and comments do not affect comparison | ||
{ | ||
code: "if (a === 1) {} else if (a===1) {}", | ||
errors: [{ messageId: "unexpected", type: "BinaryExpression", column: 26 }] | ||
}, | ||
{ | ||
code: "if (a === 1) {} else if (a === /* comment */ 1) {}", | ||
errors: [{ messageId: "unexpected", type: "BinaryExpression", column: 26 }] | ||
}, | ||
|
||
// extra parens around the whole test condition do not affect comparison | ||
{ | ||
code: "if (a === 1) {} else if ((a === 1)) {}", | ||
errors: [{ messageId: "unexpected", type: "BinaryExpression", column: 27 }] | ||
} | ||
] | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm guessing that to check each condition of the
||
expression individually is useful if thetest
nodes areLogicalExpression
with||
operator. Thoughts?For example:
I'm imaging code like:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's a very good idea.
We could even compare with the union of
||
conditions from all parents in the chain?For example:
That would also catch things like this:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this really necessary? This seems like it would add some complexity and go beyond the original proposal.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like there are 3 options at the moment:
I'm fine with all three 👍
Options 2. and 3. look very nice, but only 1. is accepted, so I don't how to proceed 😕
It doesn't look too complicated to do 2. or 3. Though, compared to the simple 1. it would be certainly much more code. Not sure how to weight how useful these enhancements would be.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't have any complexity concerns anymore based on the implementation shared earlier.
I don't understand why this only applies to
||
operators only, though. Shouldn't we look at&&
as well?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this addition! Agreed with @platinumazure. It does seem like it could be useful to also check for and warn on:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 to account for
&&
as well.The logic for
&&
in a chain is a bit different, so option 3 wouldn't apply well. For instance, the following is valid code:On the other hand, 'inverted' option 2 should work quite well for
&&
and is easy to implement. If the condition split by&&
is a superset of one of the previous conditions, then it's a 'duplicate'. For example, all these are invalid:In addition, it also looks easy to split the original condition by
&&
and do the||
check with each element of the resulting array, which would catch cases that might not even look like a bug at first glance:That shouldn't even add new lines, just
.map
and.some
to the existing lines in this draftThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is done now, the rule should be able to catch some relatively complex errors ('more complex errors with || and &&' section in
invalid
tests).Examples from the documentation:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tried the rule on ESLint codebase, reports only 1 bug at indent-legacy.js#L779 and it's indeed a bug because of
!parentVarNode
which is used in the previous condition with||
.