diff --git a/compiler/packages/babel-plugin-react-forget/src/Entrypoint/Program.ts b/compiler/packages/babel-plugin-react-forget/src/Entrypoint/Program.ts index 27ec7a1a4d..593347b0c0 100644 --- a/compiler/packages/babel-plugin-react-forget/src/Entrypoint/Program.ts +++ b/compiler/packages/babel-plugin-react-forget/src/Entrypoint/Program.ts @@ -265,6 +265,19 @@ export function compileProgram( } }, + FunctionExpression( + fn: NodePath, + pass: CompilerPass + ): void { + if (!shouldVisitNode(fn, pass)) { + return; + } + + if (compileAndInsertNewFunctionDeclaration(fn, pass) === true) { + hasForgetMutatedOriginalSource = true; + } + }, + ArrowFunctionExpression( fn: NodePath, pass: CompilerPass diff --git a/compiler/packages/babel-plugin-react-forget/src/HIR/BuildHIR.ts b/compiler/packages/babel-plugin-react-forget/src/HIR/BuildHIR.ts index 7510bb4edc..2edcf18e32 100644 --- a/compiler/packages/babel-plugin-react-forget/src/HIR/BuildHIR.ts +++ b/compiler/packages/babel-plugin-react-forget/src/HIR/BuildHIR.ts @@ -83,6 +83,8 @@ export function lower( let id: string | null = null; if (func.isFunctionDeclaration() && func.get("id").node != null) { id = (func.get("id") as NodePath).node.name; + } else if (func.isFunctionExpression() && func.get("id").node != null) { + id = (func.get("id") as NodePath).node.name; } const params: Array = []; func.get("params").forEach((param) => { diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/infer-function-expression-component.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/infer-function-expression-component.expect.md new file mode 100644 index 0000000000..4bc8b55c44 --- /dev/null +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/infer-function-expression-component.expect.md @@ -0,0 +1,31 @@ + +## Input + +```javascript +// @compilationMode(infer) + +const Component = function ComponentName(props) { + return ; +}; + +``` + +## Code + +```javascript +import { unstable_useMemoCache as useMemoCache } from "react"; // @compilationMode(infer) + +const Component = function ComponentName(props) { + const $ = useMemoCache(1); + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t0 = ; + $[0] = t0; + } else { + t0 = $[0]; + } + return t0; +}; + +``` + \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/infer-function-expression-component.js b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/infer-function-expression-component.js new file mode 100644 index 0000000000..7fe4c9c0cc --- /dev/null +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/infer-function-expression-component.js @@ -0,0 +1,5 @@ +// @compilationMode(infer) + +const Component = function ComponentName(props) { + return ; +}; diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/rules-of-hooks/rules-of-hooks-9a47e97b5d13.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/rules-of-hooks/rules-of-hooks-9a47e97b5d13.expect.md index 978a78e0c1..d14c4d9de7 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/rules-of-hooks/rules-of-hooks-9a47e97b5d13.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/rules-of-hooks/rules-of-hooks-9a47e97b5d13.expect.md @@ -14,11 +14,23 @@ const FancyButton = React.forwardRef(function (props, ref) { ## Code ```javascript -// Valid because hooks can be used in anonymous function arguments to +import { unstable_useMemoCache as useMemoCache } from "react"; // Valid because hooks can be used in anonymous function arguments to // forwardRef. const FancyButton = React.forwardRef(function (props, ref) { + const $ = useMemoCache(3); useHook(); - return