Skip to content
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

Merged
merged 3 commits into from Dec 24, 2019

Conversation

JLHwung
Copy link
Contributor

@JLHwung JLHwung commented Dec 23, 2019

Q                       A
Fixed Issues? getBinding will return declaration inside the function body for patterns in function parameters.
Patch: Bug Fix? Yes
Tests Added + Pass? Yes
License MIT

This PR is a rework of #10055. In this PR I extend the definition of Scope to include all the Pattern nodes when it immediately descends from the params of a Function node. By doing so we can intercept the ancestry queries in scope.getBinding and skip the binding if it is not a param-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:

it("should not has visibility of declarations inside function body", () => {
expect(
getPath(
`var a = "outside"; (function foo(b = a) { let a = "inside" })`,
)
.get("body.1.expression.params.0")
.scope.getBinding("a").path.node.init.value,
).toBe("outside");
});
it("should has visibility on paramater bindings", () => {
expect(
getPath(`var a = "outside"; (function foo(b = a, a = "inside") {})`)
.get("body.1.expression.params.0")
.scope.getBinding("a").path.node.right.value,
).toBe("inside");
});

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!

@JLHwung JLHwung added PR: Bug Fix 🐛 A type of pull request used for our changelog categories pkg: traverse (scope) labels Dec 23, 2019
@@ -18,5 +19,9 @@ export default function isScope(node: Object, parent: Object): boolean {
return false;
}

if (isPattern(node) && isFunction(parent)) {
Copy link
Contributor Author

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.

Copy link
Member

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.
Copy link
Contributor Author

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.

Copy link
Member

@nicolo-ribaudo nicolo-ribaudo left a 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.

packages/babel-traverse/test/scope.js Outdated Show resolved Hide resolved
@@ -18,5 +19,9 @@ export default function isScope(node: Object, parent: Object): boolean {
return false;
}

if (isPattern(node) && isFunction(parent)) {
Copy link
Member

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? 😉

packages/babel-traverse/src/scope/lib/renamer.js Outdated Show resolved Hide resolved
@JLHwung JLHwung force-pushed the fix-parameter-expression-get-binding branch from 31e013e to 63dd253 Compare December 23, 2019 18:07
@JLHwung JLHwung closed this Dec 24, 2019
@JLHwung JLHwung deleted the fix-parameter-expression-get-binding branch December 24, 2019 17:56
@nicolo-ribaudo
Copy link
Member

@JLHwung wrong button?

@JLHwung JLHwung restored the fix-parameter-expression-get-binding branch December 24, 2019 20:18
@JLHwung
Copy link
Contributor Author

JLHwung commented Dec 24, 2019

@nicolo-ribaudo Oops.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue pkg: traverse (scope) PR: Bug Fix 🐛 A type of pull request used for our changelog categories
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants