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

Fix: no-cond-assign with always option reports switch case clauses #12470

Merged
merged 1 commit into from Nov 1, 2019

Conversation

mdjermanovic
Copy link
Member

What is the purpose of this pull request? (put an "X" next to item)

[X] Bug fix

This bug fix can produce only fewer warnings.

Tell us about your environment

  • ESLint Version: 6.5.1
  • Node Version: 10.16.0
  • npm Version: 6.9.0

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

default

Please show your full configuration:

Configuration
module.exports = {
  parserOptions: {
    ecmaVersion: 2015,
  }
};

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

Online Demo Link

/* eslint no-cond-assign: ["error", "always"] */

switch (foo) {
    case a = b: 
        break;
}

What did you expect to happen?

No errors. Per the documentation, this rule doesn't target switch case clauses.

What actually happened? Please include the actual, raw output from ESLint.

 4:5  error  Unexpected assignment within SwitchCase  no-cond-assign

What changes did you make? (Give an overview)

Check parent statement's type.

Is there anything you'd like reviewers to focus on?

This test case was already valid:

{ code: "switch (foo) { case a = b: bar(); }", options: ["except-parens"] }

These test cases were invalid, that's the bug fixed by this PR:

{ code: "switch (foo) { case a = b: bar(); }", options: ["always"] },
{ code: "switch (foo) { case baz + (a = b): bar(); }", options: ["always"] }

This test case was added for regression, as there were no test cases for always with ConditionalExpression. It was already invalid:

{ code: "var x; var b = x && (y = 0) ? 1 : 0;", options: ["always"], errors: [{ messageId: "unexpected", type: "ConditionalExpression" }] },

I'll open a separate issue for conditional expressions.

@eslint-deprecated eslint-deprecated bot added the triage An ESLint team member will look at this issue soon label Oct 21, 2019
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.

This change looks good to me.

That said, why don't we just change the documentation to reflect what the rule actually enforces? Or is the problem that switch cases are not really "conditions", in your view?

@mdjermanovic
Copy link
Member Author

I'm sure this wasn't intended behavior, there are several reasons (not just the documentation).

But, yes it could become intended :-)

I think that the rule aims to prevent the mistake of using = instead of ==/=== . This isn't quite common use of the switch statement, but it could be used this way:

switch (true) {
   case x === 1: // ...
   case x === 2: // ...
}

So it might make sense to report case like other types. But then the default option should also report case (it doesn't).

I could open an enhancement issue to evaluate should the rule support case with both options?

@mdjermanovic mdjermanovic added the do not merge This pull request should not be merged yet label Oct 21, 2019
@mdjermanovic
Copy link
Member Author

Actually, a problem is that the change to start reporting case with the default "except-parens" option would produce more warnings.

We would also have to modify the conditionalAssign option in no-extra-parens.

Instead of all that, we could just document the current behavior, which is that the always option targets case, while the default except-parens option doesn't. This might be a bit confusing, though.

@platinumazure
Copy link
Member

Instead of all that, we could just document the current behavior, which is that the always option targets case, while the default except-parens option doesn't. This might be a bit confusing, though.

You've persuaded me. I think your change is the least worst approach here. Thanks!

@mdjermanovic
Copy link
Member Author

I agree, this is most likely the least confusing solution. Removing the Do Not Merge label then.

@mdjermanovic mdjermanovic added bug ESLint is working incorrectly evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion rule Relates to ESLint's core rules and removed do not merge This pull request should not be merged yet triage An ESLint team member will look at this issue soon labels Oct 21, 2019
@kaicataldo kaicataldo added accepted There is consensus among the team that this change meets the criteria for inclusion and removed evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion labels Nov 1, 2019
@kaicataldo
Copy link
Member

I agree that it makes sense to make the current behavior consistent and document it before making any enhancements.

Copy link
Member

@kaicataldo kaicataldo left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators May 1, 2020
@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 May 1, 2020
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 bug ESLint is working incorrectly rule Relates to ESLint's core rules
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants