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

Docs: clarify defaultAssignment option, fix no-unneeded-ternary examples #10874

Merged
merged 3 commits into from Sep 21, 2018
Merged

Docs: clarify defaultAssignment option, fix no-unneeded-ternary examples #10874

merged 3 commits into from Sep 21, 2018

Conversation

CoffeeTableEspresso
Copy link
Contributor

@CoffeeTableEspresso CoffeeTableEspresso commented Sep 19, 2018

The documentation for the no-unneeded-ternary rule seems to have a mistake. (#10872).

At one point, it mentions that a || should be used when the middle operand of the ternary is the same as the first:

// Bad
var foo = bar ? bar : 1;

// Good
var foo = bar || 1;

Later, however, it lists something seemingly identical to this as an example of correct code:

var a = x ? x : 1;

In my project, I was given a no-unneeded-ternary error for using this type of code, so the documentation seems to have an error. Either way, the documentation seems to list this sort of construct as both correct and incorrect, so one of those is probably in error.

Fixes #10872.

@eslint-deprecated eslint-deprecated bot added the triage An ESLint team member will look at this issue soon label Sep 19, 2018
@jsf-clabot
Copy link

jsf-clabot commented Sep 19, 2018

CLA assistant check
All committers have signed the CLA.

@platinumazure
Copy link
Member

Hi @CoffeeTableEspresso, thanks for the PR.

The difference is whether the assignment is a self-assignment/-update or not. The defaultAssignment option covers self-assignments, and by default self-assignments can use ternaries.

/* eslint no-unneeded-ternary: "error" */

/**** Not a self-assignment: ****/

// Bad
var foo = bar ? bar : 1;

// Good
var foo = bar || 1;

/**** Self-assignment: ****/

// Good
var a = x ? x : 1;
// This will be allowed by default, or if the { defaultAssignment: true } option is explicitly specified

If defaultAssignment is explicitly disabled:

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

// Bad
var foo = bar ? bar : 1;

// Good
var foo = bar || 1;

/**** Self-assignment: ****/

// Bad
var a = x ? x : 1;
// This will not be allowed

I definitely think the documentation could be improved to make this more clear. If you would like to tweak this PR or submit a new one, we would greatly appreciate it. Thanks!

@CoffeeTableEspresso
Copy link
Contributor Author

Ok, seems like I overlooked the defaultAssignment option, thanks for pointing that out.

Maybe I'm missing something still, but I don't see any difference between var foo = bar ? bar : 1; and var a = x ? x : 1;, besides the variable names, so I'm not why one would be allowed and one not with the defaults for no-unneeded-ternary set, as in your first example.

@platinumazure
Copy link
Member

platinumazure commented Sep 19, 2018 via email

@CoffeeTableEspresso
Copy link
Contributor Author

Yes, that was where my confusion came from.

Perhaps something like

// Bad
f(bar ? bar : 1);

// Good
f(bar || 1);

For the first example, and an extra comment explaining that the second example is only allowed because it in on the right on an assignment.

var a = x ? x : 1;   // Note that this is only allowed on the right of assignments by default. (See defaultAssignment option below.)

To clarify, are all ternaries allowed on the right of an assignment by default, or just a few specific uses?

@platinumazure
Copy link
Member

platinumazure commented Sep 19, 2018 via email

@CoffeeTableEspresso
Copy link
Contributor Author

Ok, I'll hold off for now then, since I think the comment should make it clear which types of ternary expressions are allowed in assignments. If you link the relevant source-code I could also have a look (but I'm not sure exactly how eslint is laid out so I don't know where to start looking).

@platinumazure
Copy link
Member

platinumazure commented Sep 19, 2018 via email

@platinumazure platinumazure added rule Relates to ESLint's core rules documentation Relates to ESLint's documentation accepted There is consensus among the team that this change meets the criteria for inclusion and removed triage An ESLint team member will look at this issue soon labels Sep 20, 2018
@platinumazure
Copy link
Member

Hi again @CoffeeTableEspresso, I've dived into the rule source.

A "default assignment" is defined in the rule as follows: The ConditionalExpression test node is an Identifier, the consequent node is an Identifier, and the identifiers have the same name. So, in other words, x ? x : y (for any identifier x and any expression y). Hope this helps!

@CoffeeTableEspresso
Copy link
Contributor Author

Ok, I have updated the file to reflect this and the changes discussed yesterday. Thanks for looking at the source code for me!

Lemme know if there's any more issues with my PR.

Copy link
Member

@platinumazure platinumazure left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, I think this has come together nicely. Thanks for contributing!

I'm going to leave this open for another day or so in case any other team member would like to review.

@CoffeeTableEspresso
Copy link
Contributor Author

Sure, no rush. Thanks for taking the time to look into this, it's been very pleasant and easy contributing to eslint.

@platinumazure
Copy link
Member

@CoffeeTableEspresso Regarding the "commit-message" status check: Could you please update the PR title to start with "Docs:" and to overall be less than 72 characters? (And I encourage you to phrase it in terms of what you're accomplishing with this PR, because then you look more awesome in our changelog when we do our next release. 😄) Thanks!

@CoffeeTableEspresso CoffeeTableEspresso changed the title Contradictions in no-unneeded-ternary documentation Docs: clarification in no-needed-ternary docs Sep 20, 2018
@CoffeeTableEspresso CoffeeTableEspresso changed the title Docs: clarification in no-needed-ternary docs Docs: clarified defaultAssignment rule in no-needed-ternary docs Sep 20, 2018
@CoffeeTableEspresso CoffeeTableEspresso changed the title Docs: clarified defaultAssignment rule in no-needed-ternary docs Docs: clarified defaultAssignment option, updated no-unneeded-ternary examples Sep 20, 2018
@CoffeeTableEspresso CoffeeTableEspresso changed the title Docs: clarified defaultAssignment option, updated no-unneeded-ternary examples Docs: clarify defaultAssignment option, fix no-unneeded-ternary examples Sep 20, 2018
Copy link
Member

@platinumazure platinumazure left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM (again, after title change). Thanks!

@platinumazure
Copy link
Member

Merging. Thanks for contributing @CoffeeTableEspresso!

@platinumazure platinumazure merged commit a6ebfd3 into eslint:master Sep 21, 2018
ch1ller0 pushed a commit to ch1ller0/eslint that referenced this pull request Sep 21, 2018
…les (eslint#10874)

* Contradictions in no-unneeded-ternary documentation

See eslint#10872

* Updated examples to more clearly reflect eslint behaviour.

* Update no-unneeded-ternary.md
ch1ller0 pushed a commit to ch1ller0/eslint that referenced this pull request Sep 21, 2018
…les (eslint#10874)

* Contradictions in no-unneeded-ternary documentation

See eslint#10872

* Updated examples to more clearly reflect eslint behaviour.

* Update no-unneeded-ternary.md
@Mouvedia
Copy link

related: #8211

@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Mar 21, 2019
@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 Mar 21, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion documentation Relates to ESLint's documentation rule Relates to ESLint's core rules
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants