@@ -19,40 +19,74 @@ pub fn jest() -> impl Pass {
19
19
#[ derive( Default ) ]
20
20
struct Jest {
21
21
imported : Vec < Id > ,
22
+ mock_vars : Vec < Id > ,
22
23
}
23
24
24
25
impl Jest {
25
26
fn visit_mut_stmt_like < T > ( & mut self , orig : & mut Vec < T > )
26
27
where
27
28
T : StmtLike + VisitMutWith < Self > ,
28
29
{
30
+ // First pass to collect mock variable declarations
29
31
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
30
45
item. visit_mut_with ( self ) ;
31
46
}
32
47
33
48
let items = orig. take ( ) ;
34
49
35
50
let mut new = Vec :: with_capacity ( items. len ( ) ) ;
36
51
let mut hoisted = Vec :: with_capacity ( 8 ) ;
52
+
37
53
items. into_iter ( ) . for_each ( |item| {
38
54
match item. try_into_stmt ( ) {
39
- Ok ( stmt) => match & stmt {
40
- Stmt :: Expr ( ExprStmt { expr, .. } ) => match & * * expr {
41
- Expr :: Call ( CallExpr {
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 {
42
77
callee : Callee :: Expr ( callee) ,
43
78
..
44
- } ) => {
79
+ } ) = & * * expr
80
+ {
45
81
if self . should_hoist ( callee) {
46
- hoisted. push ( T :: from ( stmt) )
47
- } else {
48
- new. push ( T :: from ( stmt) )
82
+ hoisted. push ( T :: from ( stmt) ) ;
83
+ return ;
49
84
}
50
85
}
51
- _ => new. push ( T :: from ( stmt) ) ,
52
- } ,
86
+ }
53
87
54
- _ => new. push ( T :: from ( stmt) ) ,
55
- } ,
88
+ new. push ( T :: from ( stmt) ) ;
89
+ }
56
90
Err ( node) => new. push ( node) ,
57
91
} ;
58
92
} ) ;
@@ -82,6 +116,7 @@ impl VisitMut for Jest {
82
116
noop_visit_mut_type ! ( ) ;
83
117
84
118
fn visit_mut_module_items ( & mut self , items : & mut Vec < ModuleItem > ) {
119
+ // First collect imported jest methods
85
120
for item in items. iter ( ) {
86
121
if let ModuleItem :: ModuleDecl ( ModuleDecl :: Import ( ImportDecl {
87
122
specifiers, src, ..
@@ -118,6 +153,7 @@ impl VisitMut for Jest {
118
153
}
119
154
}
120
155
156
+ // Now process and hoist as needed
121
157
self . visit_mut_stmt_like ( items)
122
158
}
123
159
0 commit comments