diff --git a/compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/CodegenReactiveFunction.ts b/compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/CodegenReactiveFunction.ts index 537730811d..396acc0f33 100644 --- a/compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/CodegenReactiveFunction.ts +++ b/compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/CodegenReactiveFunction.ts @@ -6,7 +6,12 @@ */ import * as t from "@babel/types"; -import { pruneUnusedLValues, pruneUnusedLabels, renameVariables } from "."; +import { + pruneHoistedContexts, + pruneUnusedLValues, + pruneUnusedLabels, + renameVariables, +} from "."; import { CompilerError, ErrorSeverity } from "../CompilerError"; import { Environment, EnvironmentConfig, ExternalFunction } from "../HIR"; import { @@ -1514,6 +1519,7 @@ function codegenInstructionValue( pruneUnusedLabels(reactiveFunction); pruneUnusedLValues(reactiveFunction); renameVariables(reactiveFunction); + pruneHoistedContexts(reactiveFunction); const fn = codegenReactiveFunction(reactiveFunction, cx).unwrap(); if (instrValue.expr.type === "ArrowFunctionExpression") { let body: t.BlockStatement | t.Expression = fn.body; diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/hoisting-within-lambda.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/hoisting-within-lambda.expect.md new file mode 100644 index 0000000000..77b679730c --- /dev/null +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/hoisting-within-lambda.expect.md @@ -0,0 +1,60 @@ + +## Input + +```javascript +function Component({}) { + const outer = () => { + const inner = () => { + return x; + }; + const x = 3; + return inner(); + }; + return
{outer()}
; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{}], +}; + +``` + +## Code + +```javascript +import { unstable_useMemoCache as useMemoCache } from "react"; +function Component(t22) { + const $ = useMemoCache(2); + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t0 = () => { + const inner = () => x; + + const x = 3; + return inner(); + }; + $[0] = t0; + } else { + t0 = $[0]; + } + const outer = t0; + let t1; + if ($[1] === Symbol.for("react.memo_cache_sentinel")) { + t1 =
{outer()}
; + $[1] = t1; + } else { + t1 = $[1]; + } + return t1; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{}], +}; + +``` + +### Eval output +(kind: ok)
3
\ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/hoisting-within-lambda.js b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/hoisting-within-lambda.js new file mode 100644 index 0000000000..934123cfa6 --- /dev/null +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/hoisting-within-lambda.js @@ -0,0 +1,15 @@ +function Component({}) { + const outer = () => { + const inner = () => { + return x; + }; + const x = 3; + return inner(); + }; + return
{outer()}
; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{}], +};