[hir] Track context refs when inferring reactive ids
Sathya Gunasekaran committed
May 31, 2023 at 15:37 UTC
793b69243f96afbe12c349451b9e30cf6e6f7c2a
5 files changed
+64
-6
compiler/forget/src/ReactiveScopes/InferReactiveIdentifiers.ts
+5
@@ -130,6 +130,11 @@ class Visitor extends ReactiveFunctionVisitor<State> {
130
state.temporaries.get(instr.value.object.identifier.id) ??
131
instr.value.object.identifier.id;
132
state.temporaries.set(instr.lvalue.identifier.id, resolvedId);
133
+ } else if (instr.value.kind === "LoadContext") {
134
+ state.temporaries.set(
135
+ instr.lvalue.identifier.id,
136
+ instr.value.place.identifier.id
137
+ );
138
}
139
}
140
}
compiler/forget/src/__tests__/fixtures/compiler/lambda-mutated-non-reactive-to-reactive.expect.md
renamed
+9
-6
@@ -17,7 +17,7 @@ function f(a) {
17
```javascript
18
import { unstable_useMemoCache as useMemoCache } from "react";
19
function f(a) {
20
- const $ = useMemoCache(3);
20
+ const $ = useMemoCache(4);
21
const c_0 = $[0] !== a;
22
let x;
23
if (c_0) {
@@ -29,14 +29,17 @@ function f(a) {
29
} else {
30
x = $[1];
31
}
32
- let t0;
33
- if ($[2] === Symbol.for("react.memo_cache_sentinel")) {
34
- t0 = <div x={x} />;
32
+ const t0 = x;
33
+ const c_2 = $[2] !== t0;
34
+ let t1;
35
+ if (c_2) {
36
+ t1 = <div x={t0} />;
37
$[2] = t0;
38
+ $[3] = t1;
39
} else {
37
- t0 = $[2];
40
+ t1 = $[3];
41
}
39
- return t0;
42
+ return t1;
43
}
44
45
```
compiler/forget/src/__tests__/fixtures/compiler/lambda-mutated-non-reactive-to-reactive.js
renamed
compiler/forget/src/__tests__/fixtures/compiler/lambda-mutated-ref-non-reactive.expect.md
new
+42
@@ -0,0 +1,42 @@
1
+
2
+## Input
3
+
4
+```javascript
5
+function f(a) {
6
+ let x;
7
+ (() => {
8
+ x = {};
9
+ })();
10
+ // this is not reactive on `x` as `x` is never reactive
11
+ return <div x={x} />;
12
+}
13
+
14
+```
15
+
16
+## Code
17
+
18
+```javascript
19
+import { unstable_useMemoCache as useMemoCache } from "react";
20
+function f(a) {
21
+ const $ = useMemoCache(2);
22
+ let x;
23
+ if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
24
+ (() => {
25
+ x = {};
26
+ })();
27
+ $[0] = x;
28
+ } else {
29
+ x = $[0];
30
+ }
31
+ let t0;
32
+ if ($[1] === Symbol.for("react.memo_cache_sentinel")) {
33
+ t0 = <div x={x} />;
34
+ $[1] = t0;
35
+ } else {
36
+ t0 = $[1];
37
+ }
38
+ return t0;
39
+}
40
+
41
+```
42
+
\ No newline at end of file
compiler/forget/src/__tests__/fixtures/compiler/lambda-mutated-ref-non-reactive.js
new
+8
@@ -0,0 +1,8 @@
1
+function f(a) {
2
+ let x;
3
+ (() => {
4
+ x = {};
5
+ })();
6
+ // this is not reactive on `x` as `x` is never reactive
7
+ return <div x={x} />;
8
+}