Skip to content

Conversation

cjihrig
Copy link
Contributor

@cjihrig cjihrig commented Aug 3, 2015

Add ability to check for x ? x : y pattern, which can be rewritten as x || y. Adds noDefaultAssignment option to no-unneeded-ternary rule. Defaults to off for backwards compatibility. Closes #3232.

@kentor
Copy link

kentor commented Aug 3, 2015

Will this warn for constructs like var foo = obj.foo ? obj.foo : obj.bar;?

But then again that isn't always the same as obj.foo || obj.bar if obj.foo has side effects...

@cjihrig
Copy link
Contributor Author

cjihrig commented Aug 3, 2015

It only handles Identifier AST nodes. Like you pointed out, getters make things hairy.

@nzakas
Copy link
Member

nzakas commented Aug 4, 2015

I think we need to handle more than identifiers for this option. Really, anything to the left of ? that is also on the right behaves the same way.

@cjihrig
Copy link
Contributor Author

cjihrig commented Aug 4, 2015

Really, anything to the left of ? that is also on the right behaves the same way.

That's not necessarily true if the thing in question has side effects. I think it might be safe to add a few other expression types (this, array expressions, and object expressions with no getters or setters). Alternatively, we could blindly flag all matches, as it's probably not good programming practice in any scenario.

@michaelficarra
Copy link
Member

@cjihrig Even bare identifiers can have side effects if the references cannot be statically resolved. We have to draw the line somewhere. I think identifiers and static member access is enough.

@mysticatea
Copy link
Member

FYI: If we would flag all matches, this can be used.

@nzakas
Copy link
Member

nzakas commented Aug 26, 2015

We kind of left this hanging. Thoughts on how to resolve?

Also, I think the option noDefaultAssignment is a bit ambiguous, probably need to rethink that.

@cjihrig
Copy link
Contributor Author

cjihrig commented Aug 28, 2015

I picked noDefaultAssignment based on identifying x = x || y, but to be honest I'm not crazy about the name and am open to suggestions.

@nzakas
Copy link
Member

nzakas commented Aug 31, 2015

I don't have any better ideas, so maybe we do defaultAssignment with a default value of true, and you can set it to false to flag it? (Trying to avoid double negatives.)

@cjihrig
Copy link
Contributor Author

cjihrig commented Sep 3, 2015

Got rid of the double negative.

ilyavolodin added a commit that referenced this pull request Sep 4, 2015
Update: Check for default assignment in no-unneeded-ternary (fixes #3232)
@ilyavolodin ilyavolodin merged commit 71acf23 into eslint:master Sep 4, 2015
@cjihrig cjihrig deleted the 3232 branch September 4, 2015 16:29
@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Feb 7, 2018
@eslint-deprecated eslint-deprecated bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Feb 7, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
archived due to age This issue has been archived; please open a new issue for any further discussion
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants