From 35fb6e05e133f261392997de5ec89dba44f99c2e Mon Sep 17 00:00:00 2001 From: Sathya Gunasekaran Date: Fri, 15 Sep 2023 17:16:05 +0100 Subject: [PATCH] [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. --- .../src/HIR/BuildHIR.ts | 37 +++++++++++++------ 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/compiler/packages/babel-plugin-react-forget/src/HIR/BuildHIR.ts b/compiler/packages/babel-plugin-react-forget/src/HIR/BuildHIR.ts index 42f3838df0..65679cfa04 100644 --- a/compiler/packages/babel-plugin-react-forget/src/HIR/BuildHIR.ts +++ b/compiler/packages/babel-plugin-react-forget/src/HIR/BuildHIR.ts @@ -32,6 +32,7 @@ import { InstructionKind, InstructionValue, JsxAttribute, + LoweredFunction, ObjectPattern, ObjectProperty, ObjectPropertyKey, @@ -2708,6 +2709,25 @@ function lowerFunctionExpression( if (expr.isFunctionExpression()) { name = expr.get("id")?.node?.name ?? null; } + const loweredFunc = lowerFunction(builder, expr); + if (!loweredFunc) { + return { kind: "UnsupportedNode", node: exprNode, loc: exprLoc }; + } + return { + kind: "FunctionExpression", + name, + expr: expr.node, + loc: exprLoc, + loweredFunc, + }; +} + +function lowerFunction( + builder: HIRBuilder, + expr: NodePath< + t.FunctionExpression | t.ArrowFunctionExpression | t.ObjectMethod + > +): LoweredFunction | null { const componentScope: Scope = builder.parentFunction.scope; const captured = gatherCapturedDeps(builder, expr, componentScope); @@ -2729,19 +2749,12 @@ function lowerFunctionExpression( lowering .unwrapErr() .details.forEach((detail) => builder.errors.pushErrorDetail(detail)); - return { - kind: "UnsupportedNode", - node: exprNode, - loc: exprLoc, - }; + return null; } loweredFunc = lowering.unwrap(); return { - kind: "FunctionExpression", - name, - loweredFunc: { func: loweredFunc, dependencies: captured.refs }, - expr: expr.node, - loc: exprLoc, + func: loweredFunc, + dependencies: captured.refs, }; } @@ -3270,7 +3283,9 @@ function captureScopes({ from, to }: { from: Scope; to: Scope }): Set { function gatherCapturedDeps( builder: HIRBuilder, - fn: NodePath, + fn: NodePath< + t.FunctionExpression | t.ArrowFunctionExpression | t.ObjectMethod + >, componentScope: Scope ): { identifiers: t.Identifier[]; refs: Place[] } { const capturedIds: Map = new Map();