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

clean up traverse scope #12797

Merged
merged 7 commits into from Feb 16, 2021
Merged

Conversation

JLHwung
Copy link
Contributor

@JLHwung JLHwung commented Feb 12, 2021

Q                       A
Fixed Issues? Class Id is not available in class scope after classPath.scope.crawl()
Patch: Bug Fix? Y
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

This PR generally cleans up internals of traverse scope, some notable changes:

  • When scope.crawl() is initiated from non-program scope, it will now collect the binding info from the scope path. Before this PR only function declaration will be collected, the scope path is ignored from collection. Only descendants of scope path is collected.
  • Remove duplicated Block handler in collectorVisitor, it is already handled in the BlockScoped handler because FunctionDeclaration is always a Block.

@JLHwung JLHwung added PR: Bug Fix 🐛 A type of pull request used for our changelog categories pkg: traverse (scope) labels Feb 12, 2021
@@ -899,6 +870,21 @@ export default class Scope {
};

this.crawling = true;
// traverse does not visit the root node, here we explicitly collect
// root node binding info when the root is not a Program.
if (programParent.path !== path && collectorVisitor._exploded) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

programParent.path !== path is meant to to avoid visiting logic when the path is a program. The program crawl is always performed before babel.transform so it is very popular path.

Copy link
Member

Choose a reason for hiding this comment

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

Can we use path.type !== "Program" instead?

@babel-bot
Copy link
Collaborator

babel-bot commented Feb 12, 2021

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/40528/

@codesandbox-ci
Copy link

codesandbox-ci bot commented Feb 12, 2021

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 1aa4adc:

Sandbox Source
babel-repl-custom-plugin Configuration
babel-plugin-multi-config Configuration

Comment on lines +877 to +887
for (const visit of collectorVisitor.enter) {
visit(path, state);
}
const typeVisitors = collectorVisitor[path.type];
if (typeVisitors) {
// @ts-expect-error when collectorVisitor is exploded, `enter` always exists
for (const visit of typeVisitors.enter) {
visit(path, state);
}
}
}
Copy link
Member

Choose a reason for hiding this comment

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

In the future we might want to extract this as a generic utility to visit a single specific node.

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

4 participants