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
clean up traverse scope #12797
Conversation
A functino declaration is always block scoped, it is already handled in BlockScoped handler
@@ -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) { |
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.
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.
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.
Can we use path.type !== "Program"
instead?
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/40528/ |
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:
|
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); | ||
} | ||
} | ||
} |
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.
In the future we might want to extract this as a generic utility to visit a single specific node.
classPath.scope.crawl()
This PR generally cleans up internals of traverse scope, some notable changes:
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.Block
handler incollectorVisitor
, it is already handled in theBlockScoped
handler becauseFunctionDeclaration
is always aBlock
.