[patch] Compile hooks with any number of args in infer mode

This commit is contained in:
Mofei Zhang
2023-12-07 16:14:25 -05:00
parent 995f4b0528
commit 9e267714db
5 changed files with 71 additions and 4 deletions
@@ -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():
@@ -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":{}}
@@ -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],
};