Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[compiler] Option to only compile component syntax #29864

Merged
merged 5 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 explicitSyntax: ReactFunctionType | null = null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: syntaxKind? kindFromSyntax? my brain reads this as a boolean

if (fn.isFunctionDeclaration()) {
if (isComponentDeclaration(fn.node)) {
explicitSyntax = "Component";
} else if (isHookDeclaration(fn.node)) {
explicitSyntax = "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 explicitSyntax ?? getComponentOrHookLike(fn, hookPattern);
}
case "syntax": {
return explicitSyntax;
}
case "all": {
// Compile only top level functions
Expand Down