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
no-cond-assign analyzes too deep into conditionals #6908
Comments
I agree, this seems like a bug. Are you willing/able to write a fix? (The answer can be no- we just like to make sure folks have a chance to contribute!) |
No, sorry, I'm afraid I wouldn't know where to start, not a clue. I detected it because the code that worked fine with ESLint 2 started giving me this error when upgrading. Since I was using the airbnb config I reported it there (airbnb/javascript#1007) since I tried the code sample above with the standard and eslint:recommended configs and had no problem with any of those. The last comment on that issue pointed me to the the eslintrc config shown above. |
I can have a look. Give me a couple days. |
@Satyam Thanks very much, had to ask. @pmcelhaney Thanks a lot, you rock. If you don't end up having time, please post back here so someone else knows they can start looking. (No pressure whatsoever, we just want to keep things moving.) |
@platinumazure Sure, I understand, no problem, thanks for asking. If I were to try a fix, I would simply bog things down. I can't beat @pmcelhaney even if it takes twice as much. Thanks. |
…slint#6908) Stops checking to see if an assignment is within a condition if the assignment is wrapped in a call expression, making the behavior consistent between "excpet-parens" and "always"
Turns out it depends on how you have the rule configured. This config considers your code invalid. "no-cond-assign": [2, "always"] These configs consider your code valid ("except-parens" is the default). "no-cond-assign": [2, "except-parens"],
"no-cond-assign": 2 The existing unit tests actually enforce this difference, which I think is a mistake. The documentation doesn't say anything about what should happen if the condition is an inline function, and being inside a function is different from being wrapped in parentheses. |
I agree that we should always allow assignments inside the body of a @pmcelhaney Are you saying that our parentheses check is also too loose? I Simple test case might be something like this: if (doSomething(foo = 5)) {} If those parentheses are misbehaving, let's open a separate issue for that. On Aug 15, 2016 10:37 AM, "Patrick McElhaney" notifications@github.com
|
The two options are implemented with different algorithms. That's why this bug shows up in one case but not the other. I just put |
No, I think that might be a separate issue. To me the spirit of the It also masked the original poster's test case (that is, OP's test case Sorry if I've confused the issue. Does this help at all? On Aug 15, 2016 10:53 AM, "Patrick McElhaney" notifications@github.com
|
@pmcelhaney That rule is exactly the one that the airbnb config has set: https://github.com/airbnb/javascript/blob/master/packages/eslint-config-airbnb-base/rules/errors.js#L7 The code |
@platinumazure I agree, the rule should accept clearly redundant sets of parenthesis, not others that might reasonably be there. |
…slint#6908) Stops checking to see if an assignment is within a condition if the assignment is wrapped in a call expression, making the behavior consistent between "except-parens" and "always"
I went ahead and added tests to clarify When the rule looks at As @Satyam said, the code |
…slint#6908) Stops checking to see if an assignment is within a condition if the assignment is wrapped in a call expression, making the behavior consistent between "except-parens" and "always"
Apologies, I definitely introduced confusion. I don't believe we should fix I would prefer to just check that
Should not be flagged, simply because the assignment is in a different scope. Can we keep this issue and any related PRs scoped to that approach? Please feel free to create a separate issue for any other problem that we've discussed in here. Thanks for your understanding. Again, I apologize for my part in allowing the discussion to diverge. |
Stops checking to see if an assignment is within a condition if the assignment is wrapped in a function expression, making the behavior consistent between "except-parens" and "always"
No worries, I've updated the PR so that it only addresses the issue at hand. |
Stops checking to see if an assignment is within a condition if the assignment is wrapped in a function expression, making the behavior consistent between "except-parens" and "always"
@platinumazure you didn't mark this as accepted, have you verified this as a bug? |
Apologies @nzakas, I hadn't had time to verify until just now. Here's a snippet that repros in the demo: /* eslint no-cond-assign: ["error", "always"] */
if (arr.some(function (item) { item = 5; })) {
doSomething();
} As discussed earlier in the issue, there is a separate issue (parentheses being too zealously considered) that prevents this from showing up as a bug with the default "except-parens" configuration. But I believe we can accept this bug, unless you disagree. |
Stops checking to see if an assignment is within a condition if the assignment is wrapped in a function expression, making the behavior consistent between "except-parens" and "always"
What version are you using?
3.2.2
What did you do?
What happened?
With this eslintrc:
It signaled:
Unexpected assignment within an 'if' statement no-cond-assign
What did you expect to happen?
It should have ceased to analyze once in the callback of
Array.prototype.some
and should not care at all about any assignments within. The purpose of the rule is to avoid mistakenly doing an assignment instead of a comparison within a conditional, not to prevent assignments at all.The text was updated successfully, but these errors were encountered: