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 parameter expression get binding #10912
Fix parameter expression get binding #10912
Conversation
@@ -18,5 +19,9 @@ export default function isScope(node: Object, parent: Object): boolean { | |||
return false; | |||
} | |||
|
|||
if (isPattern(node) && isFunction(parent)) { |
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.
If a Pattern
is a child of Function
, it must be one of the params
key because both body
and typeAnnotation
can not be a Pattern
.
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.
If you need to add a comment to explain your PR, then why don't add it directly in the code? 😉
|
||
do { | ||
const binding = scope.getOwnBinding(name); | ||
if (binding) return binding; | ||
if (binding) { | ||
// When a pattern is a Scope, it is a part of parameter expressions. |
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 case is one (likely the only one?) of the exception that although the Pattern
is descended from Function
, the scope of Pattern
is not descended from the one of Function
. This breaks the assumption that a scope is always initialized from the scope of its nearest Scopable ancestry, which becomes scope.parent
later.
export function setScope() { |
I feel like it is more cumbersome to fix that on scope.parent
accessor, so I implement the fix on getBinding
.
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! This PR is definitely better than the other. I'm impressed by how much you have learned in the last six months.
@@ -18,5 +19,9 @@ export default function isScope(node: Object, parent: Object): boolean { | |||
return false; | |||
} | |||
|
|||
if (isPattern(node) && isFunction(parent)) { |
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.
If you need to add a comment to explain your PR, then why don't add it directly in the code? 😉
31e013e
to
63dd253
Compare
@JLHwung wrong button? |
@nicolo-ribaudo Oops. |
getBinding
will return declaration inside the function body for patterns in function parameters.This PR is a rework of #10055. In this PR I extend the definition of
Scope
to include all thePattern
nodes when it immediately descends from theparams
of a Function node. By doing so we can intercept the ancestry queries inscope.getBinding
and skip the binding if it is not aparam
-kind, because the spec (9.2.10, Step 28) requires that any closure defined in the parameter expressions should not access the declarations inside the function body.An example can be found in the
scope
unit test:babel/packages/babel-traverse/test/scope.js
Lines 77 to 92 in fb4125a
Personal sidenote:
This PR is dedicated to @nicolo-ribaudo, who gave insightful comments to #10055, one of my first works when I spent days to merely learn what is scope, and who inspires me and encourages me to work on Babel. Thank you and Merry Christmas @nicolo-ribaudo!