Skip to content
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

Auto fix `no-unneeded-ternary` changes code behavior #8507

feross opened this issue Apr 25, 2017 · 1 comment


None yet
3 participants
Copy link

commented Apr 25, 2017

Tell us about your environment

  • ESLint Version: 3.19.0
  • Node Version: 7.9.0
  • npm Version: 4.5.0

What parser (default, Babel-ESLint, etc.) are you using? default

Please show your full configuration:

    "no-unneeded-ternary": ["error", { "defaultAssignment": false }],

What did you do? Please include the actual source code causing the issue.

The following code (line 3) is considered an error by the no-unneeded-ternary rule.

var value = 'a'
var canSet = true
var result = value ? value : canSet ? 'unset' : 'can not set'
// NOTE: `result` is 'a'

When --fix is used, it is corrected to code that has different behavior. See:

var value = 'a'
var canSet = true
var result = value || canSet ? 'unset' : 'can not set'
// NOTE: `result` is 'unset'

What did you expect to happen?

Parens should probably be added in this situation. This code would have maintained the same behavior:

var result = value || (canSet ? 'unset' : 'can not set')

Related issue: standard/standard#866


This comment has been minimized.

Copy link

commented Apr 26, 2017

Thanks for reporting, I can reproduce this bug.

@not-an-aardvark not-an-aardvark added accepted bug rule and removed triage labels Apr 26, 2017

@eslint eslint bot locked and limited conversation to collaborators Feb 6, 2018

@eslint eslint bot added the archived due to age label Feb 6, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.