[lambdas] Track ReactiveScopeDepenency of ComputedLoad
Fixes an issue where we did not track mutations to captured computed loads in lambdas.
Sathya Gunasekaran committed
Mar 28, 2023 at 16:29 UTC
e0180ce86a112fe5e16b7fe6cb434750dac3c992
9 files changed
+206
compiler/forget/src/Inference/AnalyseFunctions.ts
+6
@@ -62,6 +62,12 @@ export default function analyseFunctions(func: HIRFunction): void {
62
);
63
break;
64
}
65
+ case "ComputedLoad": {
66
+ // The path is set to an empty string as the path doesn't really
67
+ // matter for a computed load.
68
+ state.declareProperty(instr.lvalue, instr.value.object, "");
69
+ break;
70
+ }
71
case "LoadLocal": {
72
if (instr.lvalue.identifier.name === null) {
73
state.declareTemporary(instr.lvalue, instr.value.place);
compiler/forget/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load-2.expect.md
new
+39
@@ -0,0 +1,39 @@
1
+
2
+## Input
3
+
4
+```javascript
5
+function bar(a) {
6
+ let x = [a];
7
+ let y = {};
8
+ (function () {
9
+ y = x[0][1];
10
+ })();
11
+
12
+ return y;
13
+}
14
+
15
+```
16
+
17
+## Code
18
+
19
+```javascript
20
+function bar(a) {
21
+ const $ = React.unstable_useMemoCache(2);
22
+ const c_0 = $[0] !== a;
23
+ let y;
24
+ if (c_0) {
25
+ const x = [a];
26
+ y = {};
27
+ (function () {
28
+ y = x[0][1];
29
+ })();
30
+ $[0] = a;
31
+ $[1] = y;
32
+ } else {
33
+ y = $[1];
34
+ }
35
+ return y;
36
+}
37
+
38
+```
39
+
\ No newline at end of file
compiler/forget/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load-2.js
new
+9
@@ -0,0 +1,9 @@
1
+function bar(a) {
2
+ let x = [a];
3
+ let y = {};
4
+ (function () {
5
+ y = x[0][1];
6
+ })();
7
+
8
+ return y;
9
+}
compiler/forget/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load-3.expect.md
new
+45
@@ -0,0 +1,45 @@
1
+
2
+## Input
3
+
4
+```javascript
5
+function bar(a, b) {
6
+ let x = [a, b];
7
+ let y = {};
8
+ let t = {};
9
+ (function () {
10
+ y = x[0][1];
11
+ t = x[1][0];
12
+ })();
13
+
14
+ return y;
15
+}
16
+
17
+```
18
+
19
+## Code
20
+
21
+```javascript
22
+function bar(a, b) {
23
+ const $ = React.unstable_useMemoCache(3);
24
+ const c_0 = $[0] !== a;
25
+ const c_1 = $[1] !== b;
26
+ let y;
27
+ if (c_0 || c_1) {
28
+ const x = [a, b];
29
+ y = {};
30
+ const t = {};
31
+ (function () {
32
+ y = x[0][1];
33
+ t = x[1][0];
34
+ })();
35
+ $[0] = a;
36
+ $[1] = b;
37
+ $[2] = y;
38
+ } else {
39
+ y = $[2];
40
+ }
41
+ return y;
42
+}
43
+
44
+```
45
+
\ No newline at end of file
compiler/forget/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load-3.js
new
+11
@@ -0,0 +1,11 @@
1
+function bar(a, b) {
2
+ let x = [a, b];
3
+ let y = {};
4
+ let t = {};
5
+ (function () {
6
+ y = x[0][1];
7
+ t = x[1][0];
8
+ })();
9
+
10
+ return y;
11
+}
compiler/forget/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load-4.expect.md
new
+39
@@ -0,0 +1,39 @@
1
+
2
+## Input
3
+
4
+```javascript
5
+function bar(a) {
6
+ let x = [a];
7
+ let y = {};
8
+ (function () {
9
+ y = x[0].a[1];
10
+ })();
11
+
12
+ return y;
13
+}
14
+
15
+```
16
+
17
+## Code
18
+
19
+```javascript
20
+function bar(a) {
21
+ const $ = React.unstable_useMemoCache(2);
22
+ const c_0 = $[0] !== a;
23
+ let y;
24
+ if (c_0) {
25
+ const x = [a];
26
+ y = {};
27
+ (function () {
28
+ y = x[0].a[1];
29
+ })();
30
+ $[0] = a;
31
+ $[1] = y;
32
+ } else {
33
+ y = $[1];
34
+ }
35
+ return y;
36
+}
37
+
38
+```
39
+
\ No newline at end of file
compiler/forget/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load-4.js
new
+9
@@ -0,0 +1,9 @@
1
+function bar(a) {
2
+ let x = [a];
3
+ let y = {};
4
+ (function () {
5
+ y = x[0].a[1];
6
+ })();
7
+
8
+ return y;
9
+}
compiler/forget/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load.expect.md
new
+39
@@ -0,0 +1,39 @@
1
+
2
+## Input
3
+
4
+```javascript
5
+function bar(a) {
6
+ let x = [a];
7
+ let y = {};
8
+ (function () {
9
+ y = x[0];
10
+ })();
11
+
12
+ return y;
13
+}
14
+
15
+```
16
+
17
+## Code
18
+
19
+```javascript
20
+function bar(a) {
21
+ const $ = React.unstable_useMemoCache(2);
22
+ const c_0 = $[0] !== a;
23
+ let y;
24
+ if (c_0) {
25
+ const x = [a];
26
+ y = {};
27
+ (function () {
28
+ y = x[0];
29
+ })();
30
+ $[0] = a;
31
+ $[1] = y;
32
+ } else {
33
+ y = $[1];
34
+ }
35
+ return y;
36
+}
37
+
38
+```
39
+
\ No newline at end of file
compiler/forget/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load.js
new
+9
@@ -0,0 +1,9 @@
1
+function bar(a) {
2
+ let x = [a];
3
+ let y = {};
4
+ (function () {
5
+ y = x[0];
6
+ })();
7
+
8
+ return y;
9
+}