[hir] Skip computed part of the captured ref
Sathya Gunasekaran committed
Mar 31, 2023 at 17:37 UTC
1aec5ef523202b187007a8298c3707fae9429f23
4 files changed
+46
-4
compiler/forget/src/HIR/BuildHIR.ts
+9
-2
@@ -2608,11 +2608,18 @@ function gatherCapturedDeps(
2608
path = path.get("object");
2609
}
2610
2611
+ // Skip the computed part of the member expression.
2612
+ while (path.isMemberExpression() && path.node.computed) {
2613
+ path = path.get("object");
2614
+ }
2615
+
2616
path.skip();
2617
}
2618
2614
- capturedIds.add(binding.identifier);
2615
- capturedRefs.add(lowerExpressionToTemporary(builder, path));
2619
+ if (!capturedIds.has(binding.identifier)) {
2620
+ capturedIds.add(binding.identifier);
2621
+ capturedRefs.add(lowerExpressionToTemporary(builder, path));
2622
+ }
2623
}
2624
2625
fn.get("body").traverse({
compiler/forget/src/__tests__/fixtures/compiler/capturing-function-member-expr-call.expect.md
+2
-2
@@ -19,9 +19,9 @@ function component({ mutator }) {
19
## Code
20
21
```javascript
22
-function component(t29) {
22
+function component(t26) {
23
const $ = React.unstable_useMemoCache(7);
24
- const { mutator } = t29;
24
+ const { mutator } = t26;
25
const c_0 = $[0] !== mutator;
26
let t0;
27
if (c_0) {
compiler/forget/src/__tests__/fixtures/compiler/capturing-function-skip-computed-path.expect.md
new
+31
@@ -0,0 +1,31 @@
1
+
2
+## Input
3
+
4
+```javascript
5
+function StoreLandingUnseenGiftModalContainer(a) {
6
+ const giftsSeen = { a };
7
+ return ((gift) => (gift.id ? giftsSeen[gift.id] : false))();
8
+}
9
+
10
+```
11
+
12
+## Code
13
+
14
+```javascript
15
+function StoreLandingUnseenGiftModalContainer(a) {
16
+ const $ = React.unstable_useMemoCache(2);
17
+ const c_0 = $[0] !== a;
18
+ let t0;
19
+ if (c_0) {
20
+ const giftsSeen = { a };
21
+ t0 = ((gift) => (gift.id ? giftsSeen[gift.id] : false))();
22
+ $[0] = a;
23
+ $[1] = t0;
24
+ } else {
25
+ t0 = $[1];
26
+ }
27
+ return t0;
28
+}
29
+
30
+```
31
+
\ No newline at end of file
compiler/forget/src/__tests__/fixtures/compiler/capturing-function-skip-computed-path.js
new
+4
@@ -0,0 +1,4 @@
1
+function StoreLandingUnseenGiftModalContainer(a) {
2
+ const giftsSeen = { a };
3
+ return ((gift) => (gift.id ? giftsSeen[gift.id] : false))();
4
+}