@samitouri / QOS-React-1 / commits / 35fb6e05e1

[hir] Refactor function lowering from lowerFunctionExpression

Split out the HIR generation bits from the lowering of the actual function. The lowering will get reused when we lower object methods.

Sathya Gunasekaran committed Sep 15, 2023 at 17:16 UTC 35fb6e05e133f261392997de5ec89dba44f99c2e
1 file changed +26 -11
compiler/packages/babel-plugin-react-forget/src/HIR/BuildHIR.ts
+26 -11
@@ -32,6 +32,7 @@ import {
32 InstructionKind,
33 InstructionValue,
34 JsxAttribute,
35 + LoweredFunction,
36 ObjectPattern,
37 ObjectProperty,
38 ObjectPropertyKey,
@@ -2708,6 +2709,25 @@ function lowerFunctionExpression(
2709 if (expr.isFunctionExpression()) {
2710 name = expr.get("id")?.node?.name ?? null;
2711 }
2712 + const loweredFunc = lowerFunction(builder, expr);
2713 + if (!loweredFunc) {
2714 + return { kind: "UnsupportedNode", node: exprNode, loc: exprLoc };
2715 + }
2716 + return {
2717 + kind: "FunctionExpression",
2718 + name,
2719 + expr: expr.node,
2720 + loc: exprLoc,
2721 + loweredFunc,
2722 + };
2723 +}
2724 +
2725 +function lowerFunction(
2726 + builder: HIRBuilder,
2727 + expr: NodePath<
2728 + t.FunctionExpression | t.ArrowFunctionExpression | t.ObjectMethod
2729 + >
2730 +): LoweredFunction | null {
2731 const componentScope: Scope = builder.parentFunction.scope;
2732 const captured = gatherCapturedDeps(builder, expr, componentScope);
2733
@@ -2729,19 +2749,12 @@ function lowerFunctionExpression(
2749 lowering
2750 .unwrapErr()
2751 .details.forEach((detail) => builder.errors.pushErrorDetail(detail));
2732 - return {
2733 - kind: "UnsupportedNode",
2734 - node: exprNode,
2735 - loc: exprLoc,
2736 - };
2752 + return null;
2753 }
2754 loweredFunc = lowering.unwrap();
2755 return {
2740 - kind: "FunctionExpression",
2741 - name,
2742 - loweredFunc: { func: loweredFunc, dependencies: captured.refs },
2743 - expr: expr.node,
2744 - loc: exprLoc,
2756 + func: loweredFunc,
2757 + dependencies: captured.refs,
2758 };
2759 }
2760
@@ -3270,7 +3283,9 @@ function captureScopes({ from, to }: { from: Scope; to: Scope }): Set<Scope> {
3283
3284 function gatherCapturedDeps(
3285 builder: HIRBuilder,
3273 - fn: NodePath<t.FunctionExpression | t.ArrowFunctionExpression>,
3286 + fn: NodePath<
3287 + t.FunctionExpression | t.ArrowFunctionExpression | t.ObjectMethod
3288 + >,
3289 componentScope: Scope
3290 ): { identifiers: t.Identifier[]; refs: Place[] } {
3291 const capturedIds: Map<t.Identifier, number> = new Map();