More tests
More tests, in particular demonstrating that we don't memoize destructured rest elements propery — yet! Follow-up fixes.
Joe Savona committed
Mar 9, 2023 at 12:44 UTC
fcbfca69c91f61d599573766cd402834ac1f9b44
4 files changed
+133
compiler/forget/src/__tests__/fixtures/hir/escape-analysis-destructured-rest-element.expect.md
new
+26
@@ -0,0 +1,26 @@
1
+
2
+## Input
3
+
4
+```javascript
5
+function Component(props) {
6
+ // b is an object, must be memoized even though the input is not memoized
7
+ const { a, ...b } = props.a;
8
+ // d is an array, mut be memoized even though the input is not memoized
9
+ const [c, ...d] = props.c;
10
+ return <div b={b} d={d}></div>;
11
+}
12
+
13
+```
14
+
15
+## Code
16
+
17
+```javascript
18
+function Component(props) {
19
+ const { a, ...b } = props.a;
20
+
21
+ const [c, ...d] = props.c;
22
+ return <div b={b} d={d}></div>;
23
+}
24
+
25
+```
26
+
\ No newline at end of file
compiler/forget/src/__tests__/fixtures/hir/escape-analysis-destructured-rest-element.js
new
+7
@@ -0,0 +1,7 @@
1
+function Component(props) {
2
+ // b is an object, must be memoized even though the input is not memoized
3
+ const { a, ...b } = props.a;
4
+ // d is an array, mut be memoized even though the input is not memoized
5
+ const [c, ...d] = props.c;
6
+ return <div b={b} d={d}></div>;
7
+}
compiler/forget/src/__tests__/fixtures/hir/escape-analysis-non-escaping-interleaved-allocating-nested-dependency.expect.md
new
+76
@@ -0,0 +1,76 @@
1
+
2
+## Input
3
+
4
+```javascript
5
+function Component(props) {
6
+ // a can be independently memoized, is not mutated later
7
+ // but a is a dependnecy of b, which is a dependency of c.
8
+ // we have to memoize a to avoid breaking memoization of b,
9
+ // to avoid breaking memoization of c.
10
+ const a = [props.a];
11
+
12
+ // a can be independently memoized, is not mutated later,
13
+ // but is a dependency of d which is part of c's scope.
14
+ // we have to memoize b to avoid breaking memoization of c.
15
+ const b = [a];
16
+
17
+ // c and d are interleaved and grouped into a single scope,
18
+ // but they are independent values. d does not escape, but
19
+ // we need to ensure that b is memoized or else b will invalidate
20
+ // on every render since a is a dependency. we also need to
21
+ // ensure that a is memoized, since it's a dependency of b.
22
+ const c = [];
23
+ const d = {};
24
+ d.b = b;
25
+ c.push(props.b);
26
+
27
+ return c;
28
+}
29
+
30
+```
31
+
32
+## Code
33
+
34
+```javascript
35
+function Component(props) {
36
+ const $ = React.unstable_useMemoCache(7);
37
+ const c_0 = $[0] !== props.a;
38
+ let t0;
39
+ if (c_0) {
40
+ t0 = [props.a];
41
+ $[0] = props.a;
42
+ $[1] = t0;
43
+ } else {
44
+ t0 = $[1];
45
+ }
46
+ const a = t0;
47
+ const c_2 = $[2] !== a;
48
+ let t1;
49
+ if (c_2) {
50
+ t1 = [a];
51
+ $[2] = a;
52
+ $[3] = t1;
53
+ } else {
54
+ t1 = $[3];
55
+ }
56
+ const b = t1;
57
+ const c_4 = $[4] !== b;
58
+ const c_5 = $[5] !== props.b;
59
+ let c;
60
+ if (c_4 || c_5) {
61
+ c = [];
62
+ const d = {};
63
+ d.b = b;
64
+
65
+ c.push(props.b);
66
+ $[4] = b;
67
+ $[5] = props.b;
68
+ $[6] = c;
69
+ } else {
70
+ c = $[6];
71
+ }
72
+ return c;
73
+}
74
+
75
+```
76
+
\ No newline at end of file
compiler/forget/src/__tests__/fixtures/hir/escape-analysis-non-escaping-interleaved-allocating-nested-dependency.js
new
+24
@@ -0,0 +1,24 @@
1
+function Component(props) {
2
+ // a can be independently memoized, is not mutated later
3
+ // but a is a dependnecy of b, which is a dependency of c.
4
+ // we have to memoize a to avoid breaking memoization of b,
5
+ // to avoid breaking memoization of c.
6
+ const a = [props.a];
7
+
8
+ // a can be independently memoized, is not mutated later,
9
+ // but is a dependency of d which is part of c's scope.
10
+ // we have to memoize b to avoid breaking memoization of c.
11
+ const b = [a];
12
+
13
+ // c and d are interleaved and grouped into a single scope,
14
+ // but they are independent values. d does not escape, but
15
+ // we need to ensure that b is memoized or else b will invalidate
16
+ // on every render since a is a dependency. we also need to
17
+ // ensure that a is memoized, since it's a dependency of b.
18
+ const c = [];
19
+ const d = {};
20
+ d.b = b;
21
+ c.push(props.b);
22
+
23
+ return c;
24
+}