diff --git a/compiler/forget/packages/babel-plugin-react-forget/src/HIR/FindContextIdentifiers.ts b/compiler/forget/packages/babel-plugin-react-forget/src/HIR/FindContextIdentifiers.ts index 9ca3901a02..a60cb1b2db 100644 --- a/compiler/forget/packages/babel-plugin-react-forget/src/HIR/FindContextIdentifiers.ts +++ b/compiler/forget/packages/babel-plugin-react-forget/src/HIR/FindContextIdentifiers.ts @@ -6,7 +6,9 @@ import { GeneratedSource } from "./HIR"; type FindContextIdentifierState = { inLambda: number; currentLambda: Array< - NodePath | NodePath + | NodePath + | NodePath + | NodePath >; contextIdentifiers: Set; }; @@ -22,6 +24,20 @@ export function findContextIdentifiers( func.traverse( { + FunctionDeclaration: { + enter( + fn: NodePath, + state: FindContextIdentifierState + ): void { + state.currentLambda.push(fn); + }, + exit( + fn: NodePath, + state: FindContextIdentifierState + ): void { + state.currentLambda.pop(); + }, + }, FunctionExpression: { enter( fn: NodePath, @@ -69,6 +85,7 @@ export function findContextIdentifiers( function handleAssignment( currentLambda: + | NodePath | NodePath | NodePath, contextIdentifiers: Set, diff --git a/compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/declare-reassign-variable-in-function-declaration.expect.md b/compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/declare-reassign-variable-in-function-declaration.expect.md new file mode 100644 index 0000000000..a18fd717ab --- /dev/null +++ b/compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/declare-reassign-variable-in-function-declaration.expect.md @@ -0,0 +1,46 @@ + +## Input + +```javascript +function Component() { + let x = null; + function foo() { + x = 9; + } + const y = bar(foo); + return ; +} + +``` + +## Code + +```javascript +import { unstable_useMemoCache as useMemoCache } from "react"; +function Component() { + const $ = useMemoCache(2); + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + let x; + x = null; + const foo = function foo() { + x = 9; + }; + t0 = bar(foo); + $[0] = t0; + } else { + t0 = $[0]; + } + const y = t0; + let t1; + if ($[1] === Symbol.for("react.memo_cache_sentinel")) { + t1 = ; + $[1] = t1; + } else { + t1 = $[1]; + } + return t1; +} + +``` + \ No newline at end of file diff --git a/compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/declare-reassign-variable-in-function-declaration.js b/compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/declare-reassign-variable-in-function-declaration.js new file mode 100644 index 0000000000..ca3ff47860 --- /dev/null +++ b/compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/declare-reassign-variable-in-function-declaration.js @@ -0,0 +1,8 @@ +function Component() { + let x = null; + function foo() { + x = 9; + } + const y = bar(foo); + return ; +}