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

Nested ternaries are rewritten to mean something else than the original code #1262

Closed
anddoutoi opened this issue Mar 25, 2019 · 5 comments

Comments

@anddoutoi
Copy link

commented Mar 25, 2019

This is due to not taking into account Operator Precedence when rewriting the code.

Run the following code in the console:

const foos = ['fubar']
const foo = undefined
const defaultFoos = ['snafu']
const fubar = foos ? foos : foo ? [foo] : defaultFoos
console.log(fubar)

Output is:

["fubar"]

Go to https://standardjs.com/demo.html, paste the code (or use this demo github gist link) and click Correct errors using --fix .

The code is rewritten to:

const foos = ['fubar']
const foo = undefined
const defaultFoos = ['snafu']
const fubar = foos || foo ? [foo] : defaultFoos
console.log(fubar)

Running this in a console outputs:

[undefined]

Standard has changed the actual meaning of the code.

@LinusU

This comment has been minimized.

Copy link
Member

commented Apr 2, 2019

This is not good, would you mind filing a bug upstream with eslint, and then linking that issue here? Thanks

@anddoutoi

This comment has been minimized.

Copy link
Author

commented Apr 2, 2019

Pasting the same code:

/* eslint-disable no-console */
/* eslint-env browser */
/*eslint no-unneeded-ternary: ["error", { "defaultAssignment": false }]*/
var foos = ['fubar']
var foo = undefined
var defaultFoos = ['snafu']
var fubar = foos ? foos : foo ? [foo] : defaultFoos
console.log(fubar)

in ESLints demo page produces the expected result. Maybe Standard is running an older version of ESLInt?

Maybe related:

@LinusU

This comment has been minimized.

Copy link
Member

commented Apr 2, 2019

Ah, that might be it. There is an open PR here to fix it: #1267

ping @feross

@stale

This comment has been minimized.

Copy link

commented Jul 1, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

@stale stale bot added the stale label Jul 1, 2019

@stale stale bot closed this Jul 8, 2019

@LinusU

This comment has been minimized.

Copy link
Member

commented Jul 9, 2019

This is fixed upstream and should be fixed in Standard 13, install with npm install standard@next

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