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: prefer-const when using destructuring assign (fixes #8308) #10924
Conversation
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.
LGTM so far, but was unsure about a few possible typos/unintentional changes.
@mysticatea if you have time, can you take a look at this pull request? |
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.
Thanks! I noticed one potential bug, but this generally looks like a good approach.
Good call. Let me check that.
…On Wed, Oct 17, 2018 at 10:49 AM Teddy Katz ***@***.***> wrote:
***@***.**** commented on this pull request.
Thanks! I noticed one potential bug, but this generally looks like a good
approach.
------------------------------
In lib/rules/prefer-const.js
<#10924 (comment)>:
> @@ -96,6 +97,47 @@ function getDestructuringHost(reference) {
return node;
}
+/**
+ * Determines if a destructuring assignment node contains
+ * any MemberExpression nodes. This is used to determine if a
+ * variable that is only written once using destructuring can be
+ * safely converted into a const declaration.
+ * @param {ASTNode} node The ObjectPattern or ArrayPattern node to check.
+ * @returns {boolean} True if the destructuring pattern contains
+ * a MemberExpression, false if not.
+ */
+function hasMemberExpressionAssignment(node) {
+ switch (node.type) {
+ case "ObjectPattern":
+ return node.properties.some(prop => {
+ if (prop) {
Will this work correctly if node contains an object rest pattern?
var { ...foo } = bar;
It seems like prop.value would be undefined in that case, which would
cause the recursive call to crash.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#10924 (review)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AACWkllcqfvplh4HWnsFLhs_QD3wKrNrks5ul22ngaJpZM4XKqWZ>
.
--
______________________________
Nicholas C. Zakas
@SlickNet
Author, Principles of Object-Oriented JavaScript <http://amzn.to/29Pmfrm>
Author, Understanding ECMAScript 6 <http://amzn.to/29K1mIy>
|
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.
LGTM, thanks!
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.
LGTM. Thanks for working on this!
What is the purpose of this pull request? (put an "X" next to item)
[ ] Documentation update
[x] Bug fix (template)
[ ] New rule (template)
[ ] Changes an existing rule (template)
[ ] Add autofixing to a rule
[ ] Add a CLI option
[ ] Add something to the core
[ ] Other, please explain:
What changes did you make? (Give an overview)
Changed
prefer-const
so that it considers destructuring assignment with an object property to disqualify usingconst
for variables used in the same destructuring assignment.Is there anything you'd like reviewers to focus on?
Is this the correct approach? It seems like the key to fixing this bug was to consider object properties in destructuring assignment to be the key factor in determining if a variable should be declared with
const
. I'm not 100% this is the correct assumption, so please double-check that.