[hir] Refactor Babel expression visitor to separate function

We'll need to recursively call it in the future.
This commit is contained in:
Sathya Gunasekaran
2023-03-03 13:35:03 +00:00
parent 99eb48db07
commit 482ae7aa7c
+31 -26
View File
@@ -7,6 +7,7 @@
import { NodePath, Scope } from "@babel/traverse";
import * as t from "@babel/types";
import { Expression } from "@babel/types";
import invariant from "invariant";
import { CompilerError, ErrorSeverity } from "../CompilerError";
import { Err, Ok, Result } from "../Utils/Result";
@@ -2158,34 +2159,38 @@ function gatherCapturedDeps(
to: componentScope,
});
function visit(path: NodePath<Expression>): void {
let obj = path;
while (obj.isMemberExpression()) {
obj = obj.get("object");
}
if (!obj.isIdentifier()) {
return;
}
const binding = obj.scope.getBinding(obj.node.name);
if (binding === undefined || !pureScopes.has(binding.scope)) {
return;
}
if (path.isMemberExpression()) {
// For CallExpression, we need to depend on the receiver, not the
// function itself.
if (path.parent.type === "CallExpression") {
path = path.get("object");
}
path.skip();
}
capturedIds.add(binding.identifier);
capturedRefs.add(lowerExpressionToTemporary(builder, path));
}
fn.get("body").traverse({
Expression(path) {
let obj = path;
while (obj.isMemberExpression()) {
obj = obj.get("object");
}
if (!obj.isIdentifier()) {
return;
}
const binding = obj.scope.getBinding(obj.node.name);
if (binding === undefined || !pureScopes.has(binding.scope)) {
return;
}
if (path.isMemberExpression()) {
// For CallExpression, we need to depend on the receiver, not the
// function itself.
if (path.parent.type === "CallExpression") {
path = path.get("object");
}
path.skip();
}
capturedIds.add(binding.identifier);
capturedRefs.add(lowerExpressionToTemporary(builder, path));
visit(path);
},
});