[patch] PruneHoistedContexts should traverse lambdas
Mofei Zhang committed
Jan 29, 2024 at 17:45 UTC
c3e9cba0bb71c1fa315d75272c759081faad3599
3 files changed
+82
-1
compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/CodegenReactiveFunction.ts
+7
-1
@@ -6,7 +6,12 @@
6
*/
7
8
import * as t from "@babel/types";
9
-import { pruneUnusedLValues, pruneUnusedLabels, renameVariables } from ".";
9
+import {
10
+ pruneHoistedContexts,
11
+ pruneUnusedLValues,
12
+ pruneUnusedLabels,
13
+ renameVariables,
14
+} from ".";
15
import { CompilerError, ErrorSeverity } from "../CompilerError";
16
import { Environment, EnvironmentConfig, ExternalFunction } from "../HIR";
17
import {
@@ -1514,6 +1519,7 @@ function codegenInstructionValue(
1519
pruneUnusedLabels(reactiveFunction);
1520
pruneUnusedLValues(reactiveFunction);
1521
renameVariables(reactiveFunction);
1522
+ pruneHoistedContexts(reactiveFunction);
1523
const fn = codegenReactiveFunction(reactiveFunction, cx).unwrap();
1524
if (instrValue.expr.type === "ArrowFunctionExpression") {
1525
let body: t.BlockStatement | t.Expression = fn.body;
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/hoisting-within-lambda.expect.md
new
+60
@@ -0,0 +1,60 @@
1
+
2
+## Input
3
+
4
+```javascript
5
+function Component({}) {
6
+ const outer = () => {
7
+ const inner = () => {
8
+ return x;
9
+ };
10
+ const x = 3;
11
+ return inner();
12
+ };
13
+ return <div>{outer()}</div>;
14
+}
15
+
16
+export const FIXTURE_ENTRYPOINT = {
17
+ fn: Component,
18
+ params: [{}],
19
+};
20
+
21
+```
22
+
23
+## Code
24
+
25
+```javascript
26
+import { unstable_useMemoCache as useMemoCache } from "react";
27
+function Component(t22) {
28
+ const $ = useMemoCache(2);
29
+ let t0;
30
+ if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
31
+ t0 = () => {
32
+ const inner = () => x;
33
+
34
+ const x = 3;
35
+ return inner();
36
+ };
37
+ $[0] = t0;
38
+ } else {
39
+ t0 = $[0];
40
+ }
41
+ const outer = t0;
42
+ let t1;
43
+ if ($[1] === Symbol.for("react.memo_cache_sentinel")) {
44
+ t1 = <div>{outer()}</div>;
45
+ $[1] = t1;
46
+ } else {
47
+ t1 = $[1];
48
+ }
49
+ return t1;
50
+}
51
+
52
+export const FIXTURE_ENTRYPOINT = {
53
+ fn: Component,
54
+ params: [{}],
55
+};
56
+
57
+```
58
+
59
+### Eval output
60
+(kind: ok) <div>3</div>
\ No newline at end of file
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/hoisting-within-lambda.js
new
+15
@@ -0,0 +1,15 @@
1
+function Component({}) {
2
+ const outer = () => {
3
+ const inner = () => {
4
+ return x;
5
+ };
6
+ const x = 3;
7
+ return inner();
8
+ };
9
+ return <div>{outer()}</div>;
10
+}
11
+
12
+export const FIXTURE_ENTRYPOINT = {
13
+ fn: Component,
14
+ params: [{}],
15
+};