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; +}