@samitouri / QOS-React / commits / 482ae7aa7c

[hir] Refactor Babel expression visitor to separate function

We'll need to recursively call it in the future.

Sathya Gunasekaran committed Mar 3, 2023 at 13:35 UTC 482ae7aa7cf43dd102a08980704c2cb876c5a555
1 file changed +27 -22
compiler/forget/src/HIR/BuildHIR.ts
+27 -22
@@ -7,6 +7,7 @@
7
8 import { NodePath, Scope } from "@babel/traverse";
9 import * as t from "@babel/types";
10 +import { Expression } from "@babel/types";
11 import invariant from "invariant";
12 import { CompilerError, ErrorSeverity } from "../CompilerError";
13 import { Err, Ok, Result } from "../Utils/Result";
@@ -2158,34 +2159,38 @@ function gatherCapturedDeps(
2159 to: componentScope,
2160 });
2161
2161 - fn.get("body").traverse({
2162 - Expression(path) {
2163 - let obj = path;
2164 - while (obj.isMemberExpression()) {
2165 - obj = obj.get("object");
2166 - }
2162 + function visit(path: NodePath<Expression>): void {
2163 + let obj = path;
2164 + while (obj.isMemberExpression()) {
2165 + obj = obj.get("object");
2166 + }
2167
2168 - if (!obj.isIdentifier()) {
2169 - return;
2170 - }
2168 + if (!obj.isIdentifier()) {
2169 + return;
2170 + }
2171
2172 - const binding = obj.scope.getBinding(obj.node.name);
2173 - if (binding === undefined || !pureScopes.has(binding.scope)) {
2174 - return;
2172 + const binding = obj.scope.getBinding(obj.node.name);
2173 + if (binding === undefined || !pureScopes.has(binding.scope)) {
2174 + return;
2175 + }
2176 +
2177 + if (path.isMemberExpression()) {
2178 + // For CallExpression, we need to depend on the receiver, not the
2179 + // function itself.
2180 + if (path.parent.type === "CallExpression") {
2181 + path = path.get("object");
2182 }
2183
2177 - if (path.isMemberExpression()) {
2178 - // For CallExpression, we need to depend on the receiver, not the
2179 - // function itself.
2180 - if (path.parent.type === "CallExpression") {
2181 - path = path.get("object");
2182 - }
2184 + path.skip();
2185 + }
2186
2184 - path.skip();
2185 - }
2187 + capturedIds.add(binding.identifier);
2188 + capturedRefs.add(lowerExpressionToTemporary(builder, path));
2189 + }
2190
2187 - capturedIds.add(binding.identifier);
2188 - capturedRefs.add(lowerExpressionToTemporary(builder, path));
2191 + fn.get("body").traverse({
2192 + Expression(path) {
2193 + visit(path);
2194 },
2195 });
2196