From 14630c06183626027eb10add313137445f515587 Mon Sep 17 00:00:00 2001 From: Joe Savona Date: Thu, 14 Sep 2023 16:22:06 -0700 Subject: [PATCH] Dont compile functions w multiple args (infer mode) --- .../src/Entrypoint/Program.ts | 10 ++++-- ...e-functions-with-multiple-params.expect.md | 33 +++++++++++++++++++ ...-compile-functions-with-multiple-params.js | 10 ++++++ 3 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/dont-compile-functions-with-multiple-params.expect.md create mode 100644 compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/dont-compile-functions-with-multiple-params.js 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 a2d0663a3c..a3ffbdbfc5 100644 --- a/compiler/packages/babel-plugin-react-forget/src/Entrypoint/Program.ts +++ b/compiler/packages/babel-plugin-react-forget/src/Entrypoint/Program.ts @@ -480,8 +480,14 @@ function isReactFunctionLike( functionName !== null && (isComponentName(functionName) || isHook(functionName)) ) { - // As an added check we also look for hook invocations or JSX - return callsHooksOrCreatesJsx(node); + return ( + // As an added check we also look for hook invocations or JSX + callsHooksOrCreatesJsx(node) && + // and avoid helper functions that take more than one argument + // helpers are _usually_ named with lowercase, but some code may + // violate this rule + node.get("params").length <= 1 + ); } // 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/dont-compile-functions-with-multiple-params.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/dont-compile-functions-with-multiple-params.expect.md new file mode 100644 index 0000000000..184d22ae7d --- /dev/null +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/dont-compile-functions-with-multiple-params.expect.md @@ -0,0 +1,33 @@ + +## Input + +```javascript +// @compilationMode(infer) +// Takes multiple parameters - not a component! +function Component(foo, bar) { + return
; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [null, null], +}; + +``` + +## Code + +```javascript +// @compilationMode(infer) +// Takes multiple parameters - not a component! +function Component(foo, bar) { + return
; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [null, null], +}; + +``` + \ No newline at end of file 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/dont-compile-functions-with-multiple-params.js new file mode 100644 index 0000000000..9b2cf36a19 --- /dev/null +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/dont-compile-functions-with-multiple-params.js @@ -0,0 +1,10 @@ +// @compilationMode(infer) +// Takes multiple parameters - not a component! +function Component(foo, bar) { + return
; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [null, null], +};