-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
Update: Let no-restricted-properties
check destructuring (fixes #7147)
#7151
Conversation
@not-an-aardvark, thanks for your PR! By analyzing the annotation information on this pull request, we identified @TheSavior and @alecharmon to be potential reviewers |
LGTM |
const message = matchedObjectProperty.message ? " " + matchedObjectProperty.message : ""; | ||
checkPropertyAccess(node, objectName, propertyName); | ||
}, | ||
VariableDeclarator(node) { |
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.
And AssignmentExpression
/AssignmentPattern
can access properties by destructuring.
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 updated the PR to check for an AssignmentExpression
, but what is an example of an AssignmentPattern
?
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.
const obj = {a: 1}
function foo({a} = obj) { // obj.a
}
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.
Updated to check for AssignmentPattern
s as well.
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.
@mysticatea does this rule flag this?
function foo(obj) {
return obj.a;
}
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.
@nzakas: Yes; it reports any reference to the a
property of anything named obj
(assuming the config is [2, {"object": "obj", "property": "a"}]
).
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.
Cool, just wanted to double-check. 👍
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.
Yes. It's checked as a MemberExpression
.
d62d3c9
to
81ae3e7
Compare
LGTM |
81ae3e7
to
a11943c
Compare
LGTM |
On second thought, I realized maybe we shouldn't consider /* eslint no-restricted-properties: [2, {"object": "foo", "property": "0"}] */
var foo = new Set();
var [bar] = foo; // reports an error with this PR, but it probably shouldn't. So we should only consider edit: Updated this PR to only account for |
a11943c
to
f8a4abb
Compare
LGTM |
@not-an-aardvark We can consider supporting array destructure later-- I think supporting object destructure is the most important thing here. |
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 except above comment.
}); | ||
if (node.id.type === "ObjectPattern") { | ||
node.id.properties.forEach(property => { | ||
checkPropertyAccess(node.id, objectName, property.key.name || property.key.value); |
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.
This should ignore the property if the property has dynamic name.
const name = astUtils.getStaticPropertyName(property);
if (name) {
checkPropertyAccess(node.id, objectName, name);
}
There is the same code in checkDestructuringAssignment
function.
f8a4abb
to
311e933
Compare
LGTM |
} | ||
}, | ||
additionalProperties: false, | ||
required: ["object"] |
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.
By the way, I'm wondering if property
should be omittable because this rule is no-restricted-properties
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.
Ah, sorry, never mind...
LGTM, thank you! |
311e933
to
f4390b5
Compare
LGTM |
What is the purpose of this pull request? (put an "X" next to item)
See #7147 for the template answers.
Please check each item to ensure your pull request is ready:
What changes did you make? (Give an overview)
This allows
no-restricted-properties
to report errors from destructuring patterns.Is there anything you'd like reviewers to focus on?
Nothing in particular.
This PR is built on top of the commit from #7139, since it includes some test cases that correspond to the options in #7139. It shouldn't be merged until after #7139 is merged.(edit: #7139 is merged now.)In the meantime, see here for the diff that only includes changes from this PR.