Skip to content

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

@GrayedFox

Description

@GrayedFox

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 👍

Metadata

Metadata

Assignees

No one assigned

    Labels

    archived due to ageThis issue has been archived; please open a new issue for any further discussionauto closedThe bot closed this issueevaluatingThe team will evaluate this issue to decide whether it meets the criteria for inclusionfeatureThis change adds a new feature to ESLintneeds bikesheddingMinor details about this change need to be discussedruleRelates 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