Skip to content

Commit

Permalink
[compiler] Option to only compile component syntax
Browse files Browse the repository at this point in the history
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: 579ac9f0fa01d8cdb6a0b8f9923906a0b37662f3
Pull Request resolved: #29864
  • Loading branch information
mvitousek committed Jun 11, 2024
1 parent 2c959f1 commit 3cd3735
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3cd3735

Please sign in to comment.