From 06e5681198c92ffce8fd3c0344a9034c2d75ddf7 Mon Sep 17 00:00:00 2001 From: Sathya Gunasekaran Date: Wed, 15 Feb 2023 15:03:29 +0000 Subject: [PATCH] [hir] Use receiver of CallExpression as dep --- compiler/forget/src/HIR/BuildHIR.ts | 6 ++ ...turing-function-member-expr-call.expect.md | 63 +++++++++++++++++++ .../capturing-function-member-expr-call.js | 11 ++++ .../capturing-nested-member-call.expect.md | 4 +- 4 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 compiler/forget/src/__tests__/fixtures/hir/capturing-function-member-expr-call.expect.md create mode 100644 compiler/forget/src/__tests__/fixtures/hir/capturing-function-member-expr-call.js diff --git a/compiler/forget/src/HIR/BuildHIR.ts b/compiler/forget/src/HIR/BuildHIR.ts index a6bb8e2167..9d38badad5 100644 --- a/compiler/forget/src/HIR/BuildHIR.ts +++ b/compiler/forget/src/HIR/BuildHIR.ts @@ -1987,6 +1987,12 @@ function gatherCapturedDeps( return; } + // For CallExpression, we need to depend on the receiver, not the + // function itself. + if (path.parent.type === "CallExpression" && path.isMemberExpression()) { + path = path.get("object"); + } + path.skip(); capturedIds.add(binding.identifier); capturedRefs.add(lowerExpressionToPlace(builder, path)); diff --git a/compiler/forget/src/__tests__/fixtures/hir/capturing-function-member-expr-call.expect.md b/compiler/forget/src/__tests__/fixtures/hir/capturing-function-member-expr-call.expect.md new file mode 100644 index 0000000000..ca9a9fa461 --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/hir/capturing-function-member-expr-call.expect.md @@ -0,0 +1,63 @@ + +## Input + +```javascript +function component({ mutator }) { + const poke = () => { + mutator.poke(); + }; + + const hide = () => { + mutator.user.hide(); + }; + + return ; +} + +``` + +## Code + +```javascript +function component(t10) { + const $ = React.unstable_useMemoCache(); + const t0 = t10; + const mutator = t0.mutator; + const c_0 = $[0] !== mutator; + let poke; + if (c_0) { + poke = () => { + mutator.poke(); + }; + $[0] = mutator; + $[1] = poke; + } else { + poke = $[1]; + } + const c_2 = $[2] !== t0.mutator.user; + let hide; + if (c_2) { + hide = () => { + mutator.user.hide(); + }; + $[2] = t0.mutator.user; + $[3] = hide; + } else { + hide = $[3]; + } + const c_4 = $[4] !== poke; + const c_5 = $[5] !== hide; + let t1; + if (c_4 || c_5) { + t1 = ; + $[4] = poke; + $[5] = hide; + $[6] = t1; + } else { + t1 = $[6]; + } + return t1; +} + +``` + \ No newline at end of file diff --git a/compiler/forget/src/__tests__/fixtures/hir/capturing-function-member-expr-call.js b/compiler/forget/src/__tests__/fixtures/hir/capturing-function-member-expr-call.js new file mode 100644 index 0000000000..1b58c578bc --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/hir/capturing-function-member-expr-call.js @@ -0,0 +1,11 @@ +function component({ mutator }) { + const poke = () => { + mutator.poke(); + }; + + const hide = () => { + mutator.user.hide(); + }; + + return ; +} diff --git a/compiler/forget/src/__tests__/fixtures/hir/capturing-nested-member-call.expect.md b/compiler/forget/src/__tests__/fixtures/hir/capturing-nested-member-call.expect.md index 676b986ecc..c01cdf02b9 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/capturing-nested-member-call.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/capturing-nested-member-call.expect.md @@ -35,13 +35,13 @@ function component(a) { } else { z = $[3]; } - const c_4 = $[4] !== z.a.a; + const c_4 = $[4] !== z.a; let x; if (c_4) { x = function () { z.a.a(); }; - $[4] = z.a.a; + $[4] = z.a; $[5] = x; } else { x = $[5];