Dont compile functions w multiple args (infer mode)
Joe Savona committed
Sep 14, 2023 at 16:22 UTC
14630c06183626027eb10add313137445f515587
3 files changed
+51
-2
compiler/packages/babel-plugin-react-forget/src/Entrypoint/Program.ts
+8
-2
@@ -480,8 +480,14 @@ function isReactFunctionLike(
480
functionName !== null &&
481
(isComponentName(functionName) || isHook(functionName))
482
) {
483
- // As an added check we also look for hook invocations or JSX
484
- return callsHooksOrCreatesJsx(node);
483
+ return (
484
+ // As an added check we also look for hook invocations or JSX
485
+ callsHooksOrCreatesJsx(node) &&
486
+ // and avoid helper functions that take more than one argument
487
+ // helpers are _usually_ named with lowercase, but some code may
488
+ // violate this rule
489
+ node.get("params").length <= 1
490
+ );
491
}
492
// Otherwise for function or arrow function expressions, check if they
493
// appear as the argument to React.forwardRef() or React.memo():
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/dont-compile-functions-with-multiple-params.expect.md
new
+33
@@ -0,0 +1,33 @@
1
+
2
+## Input
3
+
4
+```javascript
5
+// @compilationMode(infer)
6
+// Takes multiple parameters - not a component!
7
+function Component(foo, bar) {
8
+ return <div />;
9
+}
10
+
11
+export const FIXTURE_ENTRYPOINT = {
12
+ fn: Component,
13
+ params: [null, null],
14
+};
15
+
16
+```
17
+
18
+## Code
19
+
20
+```javascript
21
+// @compilationMode(infer)
22
+// Takes multiple parameters - not a component!
23
+function Component(foo, bar) {
24
+ return <div />;
25
+}
26
+
27
+export const FIXTURE_ENTRYPOINT = {
28
+ fn: Component,
29
+ params: [null, null],
30
+};
31
+
32
+```
33
+
\ No newline at end of file
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/dont-compile-functions-with-multiple-params.js
new
+10
@@ -0,0 +1,10 @@
1
+// @compilationMode(infer)
2
+// Takes multiple parameters - not a component!
3
+function Component(foo, bar) {
4
+ return <div />;
5
+}
6
+
7
+export const FIXTURE_ENTRYPOINT = {
8
+ fn: Component,
9
+ params: [null, null],
10
+};