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

Proposal: conditional operator in string concatenation #893

Closed
delvedor opened this issue May 23, 2017 · 4 comments

Comments

@delvedor
Copy link

commented May 23, 2017

Hi all! :)
I got this weird issue that was not trivial to debug until I found the cause.
If you use a conditional operator without the parenthesis inside a string concatenation the result will not be what you are expecting (at least for me!).
Example:

'hello' + true ? 'a' : 'b' + 'world' // 'a'

'hello' + (true ? 'a' :'b') + 'world' // 'helloaworld'

Standard didn't warn me about this behaviour, it is worth make a new rule?
Thanks!

@yoshuawuyts

This comment has been minimized.

Copy link
Contributor

commented May 23, 2017

@delvedor

This comment has been minimized.

Copy link
Author

commented May 23, 2017

Happens the same if I do:

var h = 'hello'
var w = 'world'
console.log(h + true ? 'a' : 'b' + w) // 'a'
console.log(h + (true ? 'a' : 'b') + w) // 'helloaworld'
@feross

This comment has been minimized.

Copy link
Member

commented Apr 21, 2018

Thanks for the suggestion. There's not an ESLint rule to catch this case, as far as I'm aware. But I'll keep my eyes out for if one gets added and enable it.

@feross feross closed this Apr 21, 2018

@delvedor

This comment has been minimized.

Copy link
Author

commented Apr 22, 2018

Great, and thank you this awesome project! :)

@lock lock bot locked as resolved and limited conversation to collaborators Jul 21, 2018

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