[compiler] Option to only compile component syntax

Summary: Projects which have heavily adopted Flow component syntax may wish to enable the compiler only for components and hooks that use the syntax, rather than trying to guess which functions are components and hooks. This provides that option.

ghstack-source-id: 579ac9f0fa
Pull Request resolved: https://github.com/facebook/react/pull/29864
This commit is contained in:
Mike Vitousek
2024-06-11 14:08:21 -07:00
parent 2c959f1d77
commit 3cd3735515
2 changed files with 18 additions and 11 deletions
@@ -131,6 +131,8 @@ const CompilationModeSchema = z.enum([
* This is the default mode
*/
"infer",
// Compile only components using Flow component syntax and hooks using hook syntax.
"syntax",
// Compile only functions which are explicitly annotated with "use forget"
"annotation",
// Compile all top-level functions
@@ -499,23 +499,28 @@ function getReactFunctionType(
return getComponentOrHookLike(fn, hookPattern) ?? "Other";
}
}
// Component and hook declarations are known components/hooks
let componentSyntaxType: ReactFunctionType | null = null;
if (fn.isFunctionDeclaration()) {
if (isComponentDeclaration(fn.node)) {
componentSyntaxType = "Component";
} else if (isHookDeclaration(fn.node)) {
componentSyntaxType = "Hook";
}
}
switch (pass.opts.compilationMode) {
case "annotation": {
// opt-ins are checked above
return null;
}
case "infer": {
// Component and hook declarations are known components/hooks
if (fn.isFunctionDeclaration()) {
if (isComponentDeclaration(fn.node)) {
return "Component";
} else if (isHookDeclaration(fn.node)) {
return "Hook";
}
}
// Otherwise check if this is a component or hook-like function
return getComponentOrHookLike(fn, hookPattern);
// Check if this is a component or hook-like function
return componentSyntaxType ?? getComponentOrHookLike(fn, hookPattern);
}
case "syntax": {
return componentSyntaxType;
}
case "all": {
// Compile only top level functions