question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Warn against identical left and right hand ternary expressions (no-identical-ternary-expressions)

See original GitHub issue

Please 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:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:10 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
kaicataldocommented, Aug 28, 2019

I wonder if something like no-useless-comparison (no-unneeded-ternary seems to be the outlier here - it looks like most rules use useless 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!

1reaction
GrayedFoxcommented, Aug 15, 2019

Hmm… so, after poking around, I’ve reconsidered combining it with no-unneeded-ternary. Here is why, input welcome:

  • the category of the no-unneeded-ternary rule is Stylistic Issues (which makes sense) however the no-identical-ternary-expressions would fall under Possible Errors
  • if we combine it with no-unneeded-ternary, which some users might want to switch off, they would have to switch off the identical left and right hand linting too (which they might want to leave on)
  • no-unneeded-ternary rule is fixable whereas no-identical-ternary-expressions would not be
  • no-unneeded-ternary rule is not recommended however I would argue that the no-identical-ternary-expressions should be recommended (not sure what the process is to get a rule recommended)

Also, yay for having more free time to contribute haha 😄

Read more comments on GitHub >

github_iconTop Results From Across the Web

Conditional (ternary) operator - JavaScript - MDN Web Docs
The conditional (ternary) operator is the only JavaScript operator that takes three operands: a condition followed by a question mark (?)
Read more >
Operator precedence with JavaScript's ternary operator
It skips any explanation of why the left-hand expressions "grouped together" first (i.e. because + has greater precedence than the conditional/ternary operator ......
Read more >
the ternary conditional operator - Microsoft Learn
Learn about the C# ternary conditional operator, (`?:`), that returns the result of one of the two expressions based on a Boolean ...
Read more >
How to use the PHP Ternary Operator | Codementor
The ternary operator is a conditional operator that decreases the length of code while performing comparisons and conditionals.
Read more >
Rethinking the JavaScript ternary operator - James Sinclair
Lots of people treat the ternary operator with suspicion. ... expression on the left, the operator symbol, and an expression on the right....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found