@samitouri / QOS-React-2 / commits / 05f48e2aac

[hir] Refactor FunctionExpression lowering into separate function

This will allow us to reuse all the lowering for FunctionDeclaration.

Sathya Gunasekaran committed Mar 30, 2023 at 15:48 UTC 05f48e2aacb6d1ce2676a13643ab49d46d174d6a
1 file changed +49 -40
compiler/forget/src/HIR/BuildHIR.ts
+49 -40
@@ -1446,46 +1446,7 @@ function lowerExpression(
1446 const expr = exprPath as NodePath<
1447 t.FunctionExpression | t.ArrowFunctionExpression
1448 >;
1449 - let name: string | null = null;
1450 - if (expr.isFunctionExpression()) {
1451 - name = expr.get("id")?.node?.name ?? null;
1452 - }
1453 - const componentScope: Scope = expr.scope.parent.getFunctionParent()!;
1454 - const captured = gatherCapturedDeps(builder, expr, componentScope);
1455 -
1456 - // TODO(gsn): In the future, we could only pass in the context identifiers
1457 - // that are actually used by this function and it's nested functions, rather
1458 - // than all context identifiers.
1459 - //
1460 - // This isn't a problem in practice because use Babel's scope analysis to
1461 - // identify the correct references.
1462 - const lowering = lower(
1463 - expr,
1464 - builder.environment.options,
1465 - [...builder.context, ...captured.identifiers],
1466 - builder.parentFunction,
1467 - builder.environment
1468 - );
1469 - let loweredFunc: HIRFunction;
1470 - if (lowering.isErr()) {
1471 - lowering
1472 - .unwrapErr()
1473 - .details.forEach((detail) => builder.errors.pushErrorDetail(detail));
1474 - return {
1475 - kind: "UnsupportedNode",
1476 - node: exprNode,
1477 - loc: exprLoc,
1478 - };
1479 - }
1480 - loweredFunc = lowering.unwrap();
1481 - return {
1482 - kind: "FunctionExpression",
1483 - name,
1484 - loweredFunc,
1485 - dependencies: captured.refs,
1486 - expr: expr.node,
1487 - loc: exprLoc,
1488 - };
1449 + return lowerFunctionExpression(builder, expr);
1450 }
1451 case "TaggedTemplateExpression": {
1452 const expr = exprPath as NodePath<t.TaggedTemplateExpression>;
@@ -1996,6 +1957,54 @@ function lowerJsxElement(
1957 }
1958 }
1959
1960 +function lowerFunctionExpression(
1961 + builder: HIRBuilder,
1962 + expr: NodePath<t.FunctionExpression | t.ArrowFunctionExpression>
1963 +): InstructionValue {
1964 + const exprNode = expr.node;
1965 + const exprLoc = exprNode.loc ?? GeneratedSource;
1966 + let name: string | null = null;
1967 + if (expr.isFunctionExpression()) {
1968 + name = expr.get("id")?.node?.name ?? null;
1969 + }
1970 + const componentScope: Scope = expr.scope.parent.getFunctionParent()!;
1971 + const captured = gatherCapturedDeps(builder, expr, componentScope);
1972 +
1973 + // TODO(gsn): In the future, we could only pass in the context identifiers
1974 + // that are actually used by this function and it's nested functions, rather
1975 + // than all context identifiers.
1976 + //
1977 + // This isn't a problem in practice because use Babel's scope analysis to
1978 + // identify the correct references.
1979 + const lowering = lower(
1980 + expr,
1981 + builder.environment.options,
1982 + [...builder.context, ...captured.identifiers],
1983 + builder.parentFunction,
1984 + builder.environment
1985 + );
1986 + let loweredFunc: HIRFunction;
1987 + if (lowering.isErr()) {
1988 + lowering
1989 + .unwrapErr()
1990 + .details.forEach((detail) => builder.errors.pushErrorDetail(detail));
1991 + return {
1992 + kind: "UnsupportedNode",
1993 + node: exprNode,
1994 + loc: exprLoc,
1995 + };
1996 + }
1997 + loweredFunc = lowering.unwrap();
1998 + return {
1999 + kind: "FunctionExpression",
2000 + name,
2001 + loweredFunc,
2002 + dependencies: captured.refs,
2003 + expr: expr.node,
2004 + loc: exprLoc,
2005 + };
2006 +}
2007 +
2008 function lowerExpressionToTemporary(
2009 builder: HIRBuilder,
2010 exprPath: NodePath<t.Expression>