From 3b3d15eaeb70814a096dba5d87b87c3ee7241cf7 Mon Sep 17 00:00:00 2001 From: Sathya Gunasekaran Date: Fri, 31 Mar 2023 16:36:04 +0100 Subject: [PATCH] [hir] Check if callee before pruning member path --- compiler/forget/src/HIR/BuildHIR.ts | 5 ++- ...g-function-member-expr-arguments.expect.md | 39 +++++++++++++++++++ ...apturing-function-member-expr-arguments.js | 10 +++++ 3 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 compiler/forget/src/__tests__/fixtures/compiler/capturing-function-member-expr-arguments.expect.md create mode 100644 compiler/forget/src/__tests__/fixtures/compiler/capturing-function-member-expr-arguments.js diff --git a/compiler/forget/src/HIR/BuildHIR.ts b/compiler/forget/src/HIR/BuildHIR.ts index 9aaa6ce52f..4873441441 100644 --- a/compiler/forget/src/HIR/BuildHIR.ts +++ b/compiler/forget/src/HIR/BuildHIR.ts @@ -2488,7 +2488,10 @@ function gatherCapturedDeps( if (path.isMemberExpression()) { // For CallExpression, we need to depend on the receiver, not the // function itself. - if (path.parent.type === "CallExpression") { + if ( + path.parent.type === "CallExpression" && + path.parent.callee === path.node + ) { path = path.get("object"); } diff --git a/compiler/forget/src/__tests__/fixtures/compiler/capturing-function-member-expr-arguments.expect.md b/compiler/forget/src/__tests__/fixtures/compiler/capturing-function-member-expr-arguments.expect.md new file mode 100644 index 0000000000..a9060d21ee --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/compiler/capturing-function-member-expr-arguments.expect.md @@ -0,0 +1,39 @@ + +## Input + +```javascript +function Foo(props) { + const onFoo = useCallback( + (reason) => { + log(props.router.location); + }, + [props.router.location] + ); + + return onFoo; +} + +``` + +## Code + +```javascript +function Foo(props) { + const $ = React.unstable_useMemoCache(2); + const c_0 = $[0] !== props.router.location; + let t0; + if (c_0) { + t0 = (reason) => { + log(props.router.location); + }; + $[0] = props.router.location; + $[1] = t0; + } else { + t0 = $[1]; + } + const onFoo = t0; + return onFoo; +} + +``` + \ No newline at end of file diff --git a/compiler/forget/src/__tests__/fixtures/compiler/capturing-function-member-expr-arguments.js b/compiler/forget/src/__tests__/fixtures/compiler/capturing-function-member-expr-arguments.js new file mode 100644 index 0000000000..5eeb6bf8f2 --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/compiler/capturing-function-member-expr-arguments.js @@ -0,0 +1,10 @@ +function Foo(props) { + const onFoo = useCallback( + (reason) => { + log(props.router.location); + }, + [props.router.location] + ); + + return onFoo; +}