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
clean up traverse scope #12797
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b01fcfc
test: add tests on import declaration/export
JLHwung a3e1bf1
Fix flow->ts warnings
JLHwung daea2f4
labeled statement does not have name, do not register undefined to gl…
JLHwung e004ac6
test: add more test cases to scope traverse
JLHwung 16decb9
cleanup: remove duplicated function declaration handler
JLHwung 35b8fe0
fix: visit root node when crawl is not initiated from program scope
JLHwung 1aa4adc
review comments
JLHwung File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -203,10 +203,7 @@ const collectorVisitor: Visitor<CollectVisitorState> = { | |
if (path.isBlockScoped()) return; | ||
|
||
// this will be hit again once we traverse into it after this iteration | ||
// @ts-expect-error todo(flow->ts): might be not correct for export all declaration | ||
if (path.isExportDeclaration() && path.get("declaration").isDeclaration()) { | ||
return; | ||
} | ||
if (path.isExportDeclaration()) return; | ||
|
||
// we've ran into a declaration! | ||
const parent = | ||
|
@@ -228,7 +225,8 @@ const collectorVisitor: Visitor<CollectVisitorState> = { | |
ExportDeclaration: { | ||
exit(path) { | ||
const { node, scope } = path; | ||
// @ts-expect-error todo(flow->ts) declaration is not present on ExportAllDeclaration | ||
// ExportAllDeclaration does not have `declaration` | ||
if (t.isExportAllDeclaration(node)) return; | ||
const declar = node.declaration; | ||
if (t.isClassDeclaration(declar) || t.isFunctionDeclaration(declar)) { | ||
const id = declar.id; | ||
|
@@ -248,8 +246,6 @@ const collectorVisitor: Visitor<CollectVisitorState> = { | |
}, | ||
|
||
LabeledStatement(path) { | ||
// @ts-expect-error todo(flow->ts): possible bug - statement might not have name and so should not be added as global | ||
path.scope.getProgramParent().addGlobal(path.node); | ||
path.scope.getBlockParent().registerDeclaration(path); | ||
}, | ||
|
||
|
@@ -283,15 +279,6 @@ const collectorVisitor: Visitor<CollectVisitorState> = { | |
} | ||
}, | ||
|
||
Block(path) { | ||
const paths = path.get("body"); | ||
for (const bodyPath of paths) { | ||
if (bodyPath.isFunctionDeclaration()) { | ||
path.scope.getBlockParent().registerDeclaration(bodyPath); | ||
} | ||
} | ||
}, | ||
|
||
CatchClause(path) { | ||
path.scope.registerBinding("let", path); | ||
}, | ||
|
@@ -873,22 +860,6 @@ export default class Scope { | |
this.uids = Object.create(null); | ||
this.data = Object.create(null); | ||
|
||
// TODO: explore removing this as it should be covered by collectorVisitor | ||
if (path.isFunction()) { | ||
if ( | ||
path.isFunctionExpression() && | ||
path.has("id") && | ||
!path.get("id").node[t.NOT_LOCAL_BINDING] | ||
) { | ||
this.registerBinding("local", path.get("id"), path); | ||
} | ||
|
||
const params: Array<NodePath> = path.get("params"); | ||
for (const param of params) { | ||
this.registerBinding("param", param); | ||
} | ||
} | ||
|
||
const programParent = this.getProgramParent(); | ||
if (programParent.crawling) return; | ||
|
||
|
@@ -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) { | ||
// @ts-expect-error when collectorVisitor is exploded, `enter` always exists | ||
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); | ||
} | ||
} | ||
} | ||
Comment on lines
+877
to
+887
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
path.traverse(collectorVisitor, state); | ||
this.crawling = false; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 beforebabel.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?