Skip to content

Commit 562fac7

Browse files
authored
fix(next-swc): Fix react compiler usefulness detector (15.3) (#79480)
### What? Fix the usefulness detector for the React Compiler ### Why? JSX was also a target of the React Compiler
1 parent 06097fd commit 562fac7

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

crates/next-custom-transforms/src/react_compiler.rs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use swc_core::ecma::{
2-
ast::{Callee, Expr, FnDecl, FnExpr, Pat, Program, ReturnStmt, VarDeclarator},
2+
ast::{Callee, Expr, FnDecl, FnExpr, Pat, Program, ReturnStmt, Stmt, VarDeclarator},
33
visit::{Visit, VisitWith},
44
};
55

@@ -34,6 +34,25 @@ impl Visit for Finder {
3434
node.visit_children_with(self);
3535
}
3636

37+
fn visit_expr(&mut self, node: &Expr) {
38+
if self.found {
39+
return;
40+
}
41+
if matches!(
42+
node,
43+
Expr::JSXMember(..)
44+
| Expr::JSXNamespacedName(..)
45+
| Expr::JSXEmpty(..)
46+
| Expr::JSXElement(..)
47+
| Expr::JSXFragment(..)
48+
) {
49+
self.found = true;
50+
return;
51+
}
52+
53+
node.visit_children_with(self);
54+
}
55+
3756
fn visit_fn_decl(&mut self, node: &FnDecl) {
3857
let old = self.is_interested;
3958
self.is_interested = node.ident.sym.starts_with("use")
@@ -47,7 +66,7 @@ impl Visit for Finder {
4766
fn visit_fn_expr(&mut self, node: &FnExpr) {
4867
let old = self.is_interested;
4968

50-
self.is_interested = node.ident.as_ref().is_some_and(|ident| {
69+
self.is_interested |= node.ident.as_ref().is_some_and(|ident| {
5170
ident.sym.starts_with("use") || ident.sym.starts_with(|c: char| c.is_ascii_uppercase())
5271
});
5372

@@ -69,6 +88,13 @@ impl Visit for Finder {
6988
node.visit_children_with(self);
7089
}
7190

91+
fn visit_stmt(&mut self, node: &Stmt) {
92+
if self.found {
93+
return;
94+
}
95+
node.visit_children_with(self);
96+
}
97+
7298
fn visit_var_declarator(&mut self, node: &VarDeclarator) {
7399
let old = self.is_interested;
74100

0 commit comments

Comments
 (0)