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 } ;
22use 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.
83pub 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