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 within a function expression (fixes #6908) #6909
Fix: no-cond-assign within a function expression (fixes #6908) #6909
Conversation
@pmcelhaney, thanks for your PR! By analyzing the annotation information on this pull request, we identified @mysticatea, @vitorbal and @spmurrayzzz to be potential reviewers |
Thanks for the pull request, @pmcelhaney! I took a look to make sure it's ready for merging and found some changes are needed:
Can you please update the pull request to address these? (More information can be found in our pull request guide.) |
@@ -36,7 +36,8 @@ ruleTester.run("no-cond-assign", rule, { | |||
"while (someNode || (someNode = parentNode)) { }", | |||
"do { } while (someNode || (someNode = parentNode));", | |||
"for (;someNode || (someNode = parentNode););", | |||
"if ((function(node) { return (node = parentNode); })(someNode)) { }", | |||
{ code: "if ((function(node) { return (node = parentNode); })(someNode)) { }", options: ["except-parens"] }, | |||
{ code: "if ((function(node) { return (node = parentNode); })(someNode)) { }", options: ["always"] }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved the rule from invalid to valid in the "always" config, and updated the "except-parens" config to be more explicit, so you can see that the code is now considered valid in either case.
8a313d7
to
8e018cb
Compare
Thanks for the pull request, @pmcelhaney! I took a look to make sure it's ready for merging and found some changes are needed:
Can you please update the pull request to address these? (More information can be found in our pull request guide.) |
8e018cb
to
45484eb
Compare
Thanks for the pull request, @pmcelhaney! I took a look to make sure it's ready for merging and found some changes are needed:
Can you please update the pull request to address these? (More information can be found in our pull request guide.) |
45484eb
to
3b83cc3
Compare
LGTM |
{ code: "if ((function(node) { return (node = parentNode); })(someNode)) { }", options: ["except-parens"] }, | ||
{ code: "if ((function(node) { return (node = parentNode); })(someNode)) { }", options: ["always"] }, | ||
{ code: "if ((node => node = parentNode)(someNode)) { }", options: ["except-parens"], parserOptions: { ecmaVersion: 6 } }, | ||
{ code: "if ((node => node = parentNode)(someNode)) { }", options: ["always"], parserOptions: { ecmaVersion: 6 } }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd love to see a test just for the function expression itself (not immediately invoked). Yes, that's a useless/constant condition, but it'd be good to make sure that case is covered anyway.
LGTM except for this:
|
3b83cc3
to
d86baa5
Compare
LGTM |
Okay, I've added a couple more test cases to cover the function expression without invocation. |
@@ -59,7 +59,7 @@ module.exports = { | |||
if (isConditionalTestExpression(currentAncestor)) { | |||
return currentAncestor.parent; | |||
} | |||
} while ((currentAncestor = currentAncestor.parent)); | |||
} while ((currentAncestor = currentAncestor.parent) && currentAncestor.type !== "FunctionExpression" && currentAncestor.type !== "ArrowFunctionExpression"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would ast-utils' isFunction
method work here?
Thanks @pmcelhaney! I've added one more comment. LGTM, except:
If that's not feasible or desired, then this can be merged (as far as I'm concerned). Thanks! |
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"
d86baa5
to
d4ad00f
Compare
LGTM |
Added the astUtils function. Good idea. Thanks! |
Oh man, I can't believe I let this sit. Sorry! LGTM. |
What issue does this pull request address?
#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"
What changes did you make? (Give an overview)
I noticed that the behavior depends on whether the configuration is set to "always" or "except-parens". If configured to "except-parens" the behavior was as expected in the bug report. "always" is more strict.
Is there anything you'd like reviewers to focus on?
This might be a breaking change. I actually moved one of the unit tests from invalid to valid. But we should probably discuss that in the issue.