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

Bug/missing references after crawl #11011

Merged
merged 7 commits into from Jan 20, 2020

Conversation

regiontog
Copy link
Contributor

Q                       A
Fixed Issues? Fixes #10896
Patch: Bug Fix? Yes
Major: Breaking Change? Uncertain
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes? No
License MIT

When traversing during a crawl() babel currently sets the class identifier inside the class to the old binding. This has caused a number of issues in parcel(parcel-bundler/parcel#3932, parcel-bundler/parcel#2513, parcel-bundler/parcel#2981, parcel-bundler/parcel#3664). This PR sets the binding to the binding in the parent. See #10896 for in depth explanation.

In addition the assumption that a Declaration visitor is visited before ClassDeclaration in crawl() was removed by merging the visitors.

Copy link
Contributor

@JLHwung JLHwung left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Added to v7.8.4 milestone because it is blocking downstream tools.

@JLHwung JLHwung added this to the v7.8.4 milestone Jan 15, 2020
@JLHwung JLHwung added pkg: traverse (scope) PR: Bug Fix 🐛 A type of pull request used for our changelog categories labels Jan 15, 2020
if (!id) return;

const name = id.name;
path.scope.bindings[name] = path.scope.getBinding(name);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't the following change all that's necessary?

-    path.scope.bindings[name] = path.scope.getBinding(name);
+    path.scope.bindings[name] = path.scope.parent.getBinding(name);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. parent is the nearest function scope, but classes are block-scoped:

let A;
function fn() {
  {
    class A {}
  }
  A; // This references the let declaration, not the path.
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change

-    path.scope.bindings[name] = path.scope.getBinding(name);
+    path.scope.bindings[name] = path.scope.parent.getBinding(name);

implicitly depends on the fact that the Declaration visitor runs before ClassDeclaration visitor, so that path.scope.parent.getBinding always points to the new binding which may be initialized in Declaration visitor or BlockScoped visitor.

See #10896 (comment)

Besides that, I agree we should do path.scope.parent instead of parent.

Copy link
Contributor Author

@regiontog regiontog Jan 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

implicitly depends on the fact that the Declaration visitor runs before ClassDeclaration visitor, so that path.scope.parent.getBinding always points to the new binding which may be initialized in Declaration visitor or BlockScoped visitor.

If the class binding may be(always is?) initialized in BlockScoped won't this stop the identifier from being visible inside the class scope if the visitors are merged?

Declaration(path) {
// delegate block scope handling to the `BlockScoped` method
if (path.isBlockScoped()) return;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@regiontog Ah you are right! The ClassDeclaration binding is registered in BlockScoped and BlockScoped runs before ClassDeclaration. I am curious why the test can even get passed.

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!

Copy link
Contributor

@JLHwung JLHwung left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Apr 21, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 21, 2020
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.

Missing referencePaths after crawl
5 participants