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

mdjermanovic commented Oct 21, 2019

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 eslint bot added the triage label Oct 21, 2019
Copy link
Member

platinumazure left a comment

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

This comment has been minimized.

Copy link
Member Author

mdjermanovic commented Oct 21, 2019

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

This comment has been minimized.

Copy link
Member Author

mdjermanovic commented Oct 21, 2019

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

This comment has been minimized.

Copy link
Member

platinumazure commented Oct 21, 2019

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

This comment has been minimized.

Copy link
Member Author

mdjermanovic commented Oct 21, 2019

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

@kaicataldo

This comment has been minimized.

Copy link
Member

kaicataldo commented Nov 1, 2019

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

Copy link
Member

kaicataldo left a comment

LGTM, thanks!

@kaicataldo kaicataldo merged commit 084a8a6 into master Nov 1, 2019
17 checks passed
17 checks passed
Verify Files
Details
Test (ubuntu-latest, 8.x)
Details
Test (ubuntu-latest, 10.x)
Details
Test (ubuntu-latest, 12.x)
Details
Test (windows-latest, 12.x)
Details
Test (macOS-latest, 12.x)
Details
Browser Test
Details
commit-message Commit message follows guidelines
Details
continuous-integration Build #20191021.2 succeeded
Details
continuous-integration (Test on Node.js 10 (Linux)) Test on Node.js 10 (Linux) succeeded
Details
continuous-integration (Test on Node.js 12 (Linux)) Test on Node.js 12 (Linux) succeeded
Details
continuous-integration (Test on Node.js 12 (Windows)) Test on Node.js 12 (Windows) succeeded
Details
continuous-integration (Test on Node.js 12 (macOS)) Test on Node.js 12 (macOS) succeeded
Details
continuous-integration (Test on Node.js 8 (Linux)) Test on Node.js 8 (Linux) succeeded
Details
licence/cla Contributor License Agreement is signed.
Details
release-monitor No patch release is pending
Details
wip This PR is no longer a work in progress
Details
@kaicataldo kaicataldo deleted the nocondassign-switchcase branch Nov 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

3 participants
You can’t perform that action at this time.