@@ -19,74 +19,40 @@ pub fn jest() -> impl Pass {
1919#[ derive( Default ) ]
2020struct Jest {
2121 imported : Vec < Id > ,
22- mock_vars : Vec < Id > ,
2322}
2423
2524impl Jest {
2625 fn visit_mut_stmt_like < T > ( & mut self , orig : & mut Vec < T > )
2726 where
2827 T : StmtLike + VisitMutWith < Self > ,
2928 {
30- // First pass to collect mock variable declarations
3129 for item in & mut * orig {
32- // Check for declarations with identifiers starting with "mock"
33- if let Some ( Stmt :: Decl ( Decl :: Var ( var_decl) ) ) = item. as_stmt ( ) {
34- for decl in & var_decl. decls {
35- if let Pat :: Ident ( ident) = & decl. name {
36- let name = & * ident. id . sym ;
37- if name. starts_with ( "mock" ) {
38- self . mock_vars . push ( ident. id . to_id ( ) ) ;
39- }
40- }
41- }
42- }
43-
44- // Standard visitation
4530 item. visit_mut_with ( self ) ;
4631 }
4732
4833 let items = orig. take ( ) ;
4934
5035 let mut new = Vec :: with_capacity ( items. len ( ) ) ;
5136 let mut hoisted = Vec :: with_capacity ( 8 ) ;
52-
5337 items. into_iter ( ) . for_each ( |item| {
5438 match item. try_into_stmt ( ) {
55- Ok ( stmt) => {
56- // Check for mock variable declarations to hoist
57- if let Stmt :: Decl ( Decl :: Var ( var_decl) ) = & stmt {
58- let mut should_hoist = false ;
59- for decl in & var_decl. decls {
60- if let Pat :: Ident ( ident) = & decl. name {
61- if self . mock_vars . iter ( ) . any ( |id| * id == ident. id . to_id ( ) ) {
62- should_hoist = true ;
63- break ;
64- }
65- }
66- }
67-
68- if should_hoist {
69- hoisted. push ( T :: from ( stmt) ) ;
70- return ;
71- }
72- }
73-
74- // Check for jest calls to hoist
75- if let Stmt :: Expr ( ExprStmt { expr, .. } ) = & stmt {
76- if let Expr :: Call ( CallExpr {
39+ Ok ( stmt) => match & stmt {
40+ Stmt :: Expr ( ExprStmt { expr, .. } ) => match & * * expr {
41+ Expr :: Call ( CallExpr {
7742 callee : Callee :: Expr ( callee) ,
7843 ..
79- } ) = & * * expr
80- {
44+ } ) => {
8145 if self . should_hoist ( callee) {
82- hoisted. push ( T :: from ( stmt) ) ;
83- return ;
46+ hoisted. push ( T :: from ( stmt) )
47+ } else {
48+ new. push ( T :: from ( stmt) )
8449 }
8550 }
86- }
51+ _ => new. push ( T :: from ( stmt) ) ,
52+ } ,
8753
88- new. push ( T :: from ( stmt) ) ;
89- }
54+ _ => new. push ( T :: from ( stmt) ) ,
55+ } ,
9056 Err ( node) => new. push ( node) ,
9157 } ;
9258 } ) ;
@@ -116,7 +82,6 @@ impl VisitMut for Jest {
11682 noop_visit_mut_type ! ( ) ;
11783
11884 fn visit_mut_module_items ( & mut self , items : & mut Vec < ModuleItem > ) {
119- // First collect imported jest methods
12085 for item in items. iter ( ) {
12186 if let ModuleItem :: ModuleDecl ( ModuleDecl :: Import ( ImportDecl {
12287 specifiers, src, ..
@@ -153,7 +118,6 @@ impl VisitMut for Jest {
153118 }
154119 }
155120
156- // Now process and hoist as needed
157121 self . visit_mut_stmt_like ( items)
158122 }
159123
0 commit comments