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 619493f695..ce0d2c0875 100644 --- a/compiler/packages/babel-plugin-react-forget/src/Entrypoint/Program.ts +++ b/compiler/packages/babel-plugin-react-forget/src/Entrypoint/Program.ts @@ -489,10 +489,7 @@ function isComponentOrHookLike( ): boolean { const functionName = getFunctionName(node); // Check if the name is component or hook like: - if ( - functionName !== null && - (isComponentName(functionName) || isHook(functionName)) - ) { + if (functionName !== null && isComponentName(functionName)) { return ( // As an added check we also look for hook invocations or JSX callsHooksOrCreatesJsx(node) && @@ -503,7 +500,11 @@ function isComponentOrHookLike( */ node.get("params").length <= 1 ); + } else if (functionName !== null && isHook(functionName)) { + // Hooks have hook invocations or JSX, but can take any # of arguments + return callsHooksOrCreatesJsx(node); } + /* * Otherwise for function or arrow function expressions, check if they * appear as the argument to React.forwardRef() or React.memo(): diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/infer-compile-hooks-with-multiple-params.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/infer-compile-hooks-with-multiple-params.expect.md new file mode 100644 index 0000000000..ded2aed352 --- /dev/null +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/infer-compile-hooks-with-multiple-params.expect.md @@ -0,0 +1,52 @@ + +## Input + +```javascript +// @compilationMode(infer) +import { useNoAlias } from "shared-runtime"; + +// This should be compiled by Forget +function useFoo(value1, value2) { + return { + value: useNoAlias(value1 + value2), + }; +} + +export const FIXTURE_ENTRYPOINT = { + fn: useFoo, + params: [1, 2], +}; + +``` + +## Code + +```javascript +import { unstable_useMemoCache as useMemoCache } from "react"; // @compilationMode(infer) +import { useNoAlias } from "shared-runtime"; + +// This should be compiled by Forget +function useFoo(value1, value2) { + const $ = useMemoCache(2); + + const t0 = useNoAlias(value1 + value2); + let t1; + if ($[0] !== t0) { + t1 = { value: t0 }; + $[0] = t0; + $[1] = t1; + } else { + t1 = $[1]; + } + return t1; +} + +export const FIXTURE_ENTRYPOINT = { + fn: useFoo, + params: [1, 2], +}; + +``` + +### Eval output +(kind: ok) {"value":{}} \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/infer-compile-hooks-with-multiple-params.js b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/infer-compile-hooks-with-multiple-params.js new file mode 100644 index 0000000000..2190b3a356 --- /dev/null +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/infer-compile-hooks-with-multiple-params.js @@ -0,0 +1,14 @@ +// @compilationMode(infer) +import { useNoAlias } from "shared-runtime"; + +// This should be compiled by Forget +function useFoo(value1, value2) { + return { + value: useNoAlias(value1 + value2), + }; +} + +export const FIXTURE_ENTRYPOINT = { + fn: useFoo, + params: [1, 2], +}; diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/dont-compile-functions-with-multiple-params.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/infer-dont-compile-components-with-multiple-params.expect.md similarity index 100% rename from compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/dont-compile-functions-with-multiple-params.expect.md rename to compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/infer-dont-compile-components-with-multiple-params.expect.md diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/dont-compile-functions-with-multiple-params.js b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/infer-dont-compile-components-with-multiple-params.js similarity index 100% rename from compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/dont-compile-functions-with-multiple-params.js rename to compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/infer-dont-compile-components-with-multiple-params.js