[hir] Use receiver of CallExpression as dep
Sathya Gunasekaran committed
Feb 15, 2023 at 15:03 UTC
06e5681198c92ffce8fd3c0344a9034c2d75ddf7
4 files changed
+82
-2
compiler/forget/src/HIR/BuildHIR.ts
+6
@@ -1987,6 +1987,12 @@ function gatherCapturedDeps(
1987
return;
1988
}
1989
1990
+ // For CallExpression, we need to depend on the receiver, not the
1991
+ // function itself.
1992
+ if (path.parent.type === "CallExpression" && path.isMemberExpression()) {
1993
+ path = path.get("object");
1994
+ }
1995
+
1996
path.skip();
1997
capturedIds.add(binding.identifier);
1998
capturedRefs.add(lowerExpressionToPlace(builder, path));
compiler/forget/src/__tests__/fixtures/hir/capturing-function-member-expr-call.expect.md
new
+63
@@ -0,0 +1,63 @@
1
+
2
+## Input
3
+
4
+```javascript
5
+function component({ mutator }) {
6
+ const poke = () => {
7
+ mutator.poke();
8
+ };
9
+
10
+ const hide = () => {
11
+ mutator.user.hide();
12
+ };
13
+
14
+ return <Foo poke={poke} hide={hide}></Foo>;
15
+}
16
+
17
+```
18
+
19
+## Code
20
+
21
+```javascript
22
+function component(t10) {
23
+ const $ = React.unstable_useMemoCache();
24
+ const t0 = t10;
25
+ const mutator = t0.mutator;
26
+ const c_0 = $[0] !== mutator;
27
+ let poke;
28
+ if (c_0) {
29
+ poke = () => {
30
+ mutator.poke();
31
+ };
32
+ $[0] = mutator;
33
+ $[1] = poke;
34
+ } else {
35
+ poke = $[1];
36
+ }
37
+ const c_2 = $[2] !== t0.mutator.user;
38
+ let hide;
39
+ if (c_2) {
40
+ hide = () => {
41
+ mutator.user.hide();
42
+ };
43
+ $[2] = t0.mutator.user;
44
+ $[3] = hide;
45
+ } else {
46
+ hide = $[3];
47
+ }
48
+ const c_4 = $[4] !== poke;
49
+ const c_5 = $[5] !== hide;
50
+ let t1;
51
+ if (c_4 || c_5) {
52
+ t1 = <Foo poke={poke} hide={hide}></Foo>;
53
+ $[4] = poke;
54
+ $[5] = hide;
55
+ $[6] = t1;
56
+ } else {
57
+ t1 = $[6];
58
+ }
59
+ return t1;
60
+}
61
+
62
+```
63
+
\ No newline at end of file
compiler/forget/src/__tests__/fixtures/hir/capturing-function-member-expr-call.js
new
+11
@@ -0,0 +1,11 @@
1
+function component({ mutator }) {
2
+ const poke = () => {
3
+ mutator.poke();
4
+ };
5
+
6
+ const hide = () => {
7
+ mutator.user.hide();
8
+ };
9
+
10
+ return <Foo poke={poke} hide={hide}></Foo>;
11
+}
compiler/forget/src/__tests__/fixtures/hir/capturing-nested-member-call.expect.md
+2
-2
@@ -35,13 +35,13 @@ function component(a) {
35
} else {
36
z = $[3];
37
}
38
- const c_4 = $[4] !== z.a.a;
38
+ const c_4 = $[4] !== z.a;
39
let x;
40
if (c_4) {
41
x = function () {
42
z.a.a();
43
};
44
- $[4] = z.a.a;
44
+ $[4] = z.a;
45
$[5] = x;
46
} else {
47
x = $[5];