Fixture for pruning unmemoized nonreactive deps
Adds a fixture for our existing behavior that reactive scope dependencies exclude values which are non-reactive. The idea is that regardless of whether the value may actually get recreated over time or not, a "nonreactive" value cannot semantically change and therefore we can ignore changes in its pointer address.
Joe Savona committed
Dec 14, 2023 at 12:05 UTC
fc3604301983d8adcd1b5c4b8dce4ece48d4f615
2 files changed
+72
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/unmemoized-nonreactive-dependency-is-pruned-as-dependency.expect.md
new
+55
@@ -0,0 +1,55 @@
1
+
2
+## Input
3
+
4
+```javascript
5
+import { mutate, useNoAlias } from "shared-runtime";
6
+
7
+function Component(props) {
8
+ // Here `x` cannot be memoized bc its mutable range spans a hook call:
9
+ const x = [];
10
+ useNoAlias();
11
+ mutate(x);
12
+
13
+ // However, `x` is non-reactive. It cannot semantically change, so we
14
+ // exclude it as a dependency of the JSX element:
15
+ return <div>{x}</div>;
16
+}
17
+
18
+export const FIXTURE_ENTRYPOINT = {
19
+ fn: Component,
20
+ params: [{ value: 42 }],
21
+};
22
+
23
+```
24
+
25
+## Code
26
+
27
+```javascript
28
+import { unstable_useMemoCache as useMemoCache } from "react";
29
+import { mutate, useNoAlias } from "shared-runtime";
30
+
31
+function Component(props) {
32
+ const $ = useMemoCache(1);
33
+
34
+ const x = [];
35
+ useNoAlias();
36
+ mutate(x);
37
+ let t0;
38
+ if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
39
+ t0 = <div>{x}</div>;
40
+ $[0] = t0;
41
+ } else {
42
+ t0 = $[0];
43
+ }
44
+ return t0;
45
+}
46
+
47
+export const FIXTURE_ENTRYPOINT = {
48
+ fn: Component,
49
+ params: [{ value: 42 }],
50
+};
51
+
52
+```
53
+
54
+### Eval output
55
+(kind: ok) <div></div>
\ No newline at end of file
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/unmemoized-nonreactive-dependency-is-pruned-as-dependency.js
new
+17
@@ -0,0 +1,17 @@
1
+import { mutate, useNoAlias } from "shared-runtime";
2
+
3
+function Component(props) {
4
+ // Here `x` cannot be memoized bc its mutable range spans a hook call:
5
+ const x = [];
6
+ useNoAlias();
7
+ mutate(x);
8
+
9
+ // However, `x` is non-reactive. It cannot semantically change, so we
10
+ // exclude it as a dependency of the JSX element:
11
+ return <div>{x}</div>;
12
+}
13
+
14
+export const FIXTURE_ENTRYPOINT = {
15
+ fn: Component,
16
+ params: [{ value: 42 }],
17
+};