[patch] PruneHoistedContexts should traverse lambdas

This commit is contained in:
Mofei Zhang
2024-01-29 17:45:44 -05:00
parent 74d8a18637
commit c3e9cba0bb
3 changed files with 82 additions and 1 deletions
@@ -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;
@@ -0,0 +1,60 @@
## Input
```javascript
function Component({}) {
const outer = () => {
const inner = () => {
return x;
};
const x = 3;
return inner();
};
return <div>{outer()}</div>;
}
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 = <div>{outer()}</div>;
$[1] = t1;
} else {
t1 = $[1];
}
return t1;
}
export const FIXTURE_ENTRYPOINT = {
fn: Component,
params: [{}],
};
```
### Eval output
(kind: ok) <div>3</div>
@@ -0,0 +1,15 @@
function Component({}) {
const outer = () => {
const inner = () => {
return x;
};
const x = 3;
return inner();
};
return <div>{outer()}</div>;
}
export const FIXTURE_ENTRYPOINT = {
fn: Component,
params: [{}],
};