Skip to content

Commit 41075a1

Browse files
authored
fix(es/react-compiler): Fix usefulness detection (#10506)
**Description:** JSX is also a target of the React Compiler **Related issue:** - vercel/next.js#79479
1 parent 4bc4244 commit 41075a1

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

.changeset/ten-tables-design.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
swc_core: patch
3+
swc_ecma_react_compiler: patch
4+
---
5+
6+
fix(react-compiler/support): Fix usefulness detection

crates/swc_ecma_react_compiler/src/fast_check.rs

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
1-
use swc_ecma_ast::{Callee, Expr, FnDecl, FnExpr, Pat, Program, ReturnStmt, VarDeclarator};
1+
use swc_ecma_ast::{Callee, Expr, FnDecl, FnExpr, Pat, Program, ReturnStmt, Stmt, VarDeclarator};
22
use swc_ecma_visit::{Visit, VisitWith};
3-
4-
/// Returns true if the `program` is a good target for the react compiler.
5-
///
6-
/// If this function returns false, it means that it does not worth to apply the
7-
/// React Compiler to the file.
83
pub fn is_required(program: &Program) -> bool {
94
let mut finder = Finder::default();
105
finder.visit_program(program);
@@ -36,6 +31,25 @@ impl Visit for Finder {
3631
node.visit_children_with(self);
3732
}
3833

34+
fn visit_expr(&mut self, node: &Expr) {
35+
if self.found {
36+
return;
37+
}
38+
if matches!(
39+
node,
40+
Expr::JSXMember(..)
41+
| Expr::JSXNamespacedName(..)
42+
| Expr::JSXEmpty(..)
43+
| Expr::JSXElement(..)
44+
| Expr::JSXFragment(..)
45+
) {
46+
self.found = true;
47+
return;
48+
}
49+
50+
node.visit_children_with(self);
51+
}
52+
3953
fn visit_fn_decl(&mut self, node: &FnDecl) {
4054
let old = self.is_interested;
4155
self.is_interested = node.ident.sym.starts_with("use")
@@ -49,7 +63,7 @@ impl Visit for Finder {
4963
fn visit_fn_expr(&mut self, node: &FnExpr) {
5064
let old = self.is_interested;
5165

52-
self.is_interested = node.ident.as_ref().is_some_and(|ident| {
66+
self.is_interested |= node.ident.as_ref().is_some_and(|ident| {
5367
ident.sym.starts_with("use") || ident.sym.starts_with(|c: char| c.is_ascii_uppercase())
5468
});
5569

@@ -71,6 +85,13 @@ impl Visit for Finder {
7185
node.visit_children_with(self);
7286
}
7387

88+
fn visit_stmt(&mut self, node: &Stmt) {
89+
if self.found {
90+
return;
91+
}
92+
node.visit_children_with(self);
93+
}
94+
7495
fn visit_var_declarator(&mut self, node: &VarDeclarator) {
7596
let old = self.is_interested;
7697

0 commit comments

Comments
 (0)