Warn against identical left and right hand ternary expressions (no-identical-ternary-expressions)
See original GitHub issuePlease describe what the rule should do:
This rule would detect identical left and right hand statements within a ternary operator.
The rule would only be configurable in a “warning” or “error” sense (like all other rules) or could be switched off, although to be honest I can’t imagine why anyone would ever need to turn it off!
What category of rule is this? (place an “X” next to just one item)
- Warns about a potential (very likely) error (problem)
Provide 2-3 code examples that this rule will warn about:
function getFee(isMember) {
return (isMember ? "$2.00" : "$2.00");
// "Error: Identical left and right hand expressions"
}
thing === otherThing
? getFee(isMember)
: getFee(isMember)
// "Error: Identical left and right hand expressions"
var foo = bar > baz ? value1 : value1;
// "Error: Identical left and right hand expressions"
return condition1 ? value1 : condition1 ? value2 : value3
// "Error: Identical test conditions encountered"
// the first and second tests are identical (condition1 used twice, value2 is unreachable code)
return condition1 ? value1 : condition2 ? value2 : condition1 ? value3 : value4
// "Error: Identical test conditions encountered"
// the first and third tests are identical (condition1 used twice, value3 is unreachable code)
function example(…) {
return condition1 ? value1
: condition2 ? value2
: condition3 ? value1
}
// No error here, even though this code could be shortened to something like this:
function example(…) {
return (condition1 || condition3) ? value1
: condition2 ? value2
}
// Rule will not try and enforce a specific ternary style
Why should this rule be included in ESLint (instead of a plugin)? Because it catches something that is, I would argue, an error 100% of the time since any conditional ternary with identical left and right expressions could be simplified to:
statement
Also, duplicate test statements within ternary operators necessarily result in unreachable code.
Linters should not only enforce style but also catch logical/coding errors. It is more than likely that the programmer, in this case, accidentally put identical left and right hand expressions and meant to do something else.
Are you willing to submit a pull request to implement this rule? Absolutely 👍
Issue Analytics
- State:
- Created 4 years ago
- Reactions:2
- Comments:10 (9 by maintainers)
I wonder if something like
no-useless-comparison
(no-unneeded-ternary
seems to be the outlier here - it looks like most rules useuseless
instead, and maybe we should use that for consistency) would be clearer?Either way, I think a rule like this could be a worthwhile addition to the core rules. Let’s see what others on the team have to say, though. Our bar for new rules is quite high because we already have so many, and updating an existing rule is a lot less overhead!
Hmm… so, after poking around, I’ve reconsidered combining it with no-unneeded-ternary. Here is why, input welcome:
Also, yay for having more free time to contribute haha 😄