@@ -7,7 +7,7 @@ use oxc_ast::{
7
7
} ,
8
8
AstKind ,
9
9
} ;
10
- use oxc_semantic:: { AstNode , ReferenceId } ;
10
+ use oxc_semantic:: { AstNode , ReferenceId , Semantic } ;
11
11
use oxc_span:: CompactStr ;
12
12
use phf:: phf_set;
13
13
@@ -149,13 +149,13 @@ pub struct PossibleJestNode<'a, 'b> {
149
149
pub fn collect_possible_jest_call_node < ' a , ' c > (
150
150
ctx : & ' c LintContext < ' a > ,
151
151
) -> Vec < PossibleJestNode < ' a , ' c > > {
152
- iter_possible_jest_call_node ( ctx) . collect ( )
152
+ iter_possible_jest_call_node ( ctx. semantic ( ) ) . collect ( )
153
153
}
154
154
155
155
/// Iterate over all possible Jest fn Call Expression,
156
156
/// for `expect(1).toBe(1)`, the result will be an iter over node `expect(1)` and node `expect(1).toBe(1)`.
157
157
pub fn iter_possible_jest_call_node < ' a , ' c > (
158
- ctx : & ' c LintContext < ' a > ,
158
+ semantic : & ' c Semantic < ' a > ,
159
159
) -> impl Iterator < Item = PossibleJestNode < ' a , ' c > > + ' c {
160
160
// Some people may write codes like below, we need lookup imported test function and global test function.
161
161
// ```
@@ -165,17 +165,17 @@ pub fn iter_possible_jest_call_node<'a, 'c>(
165
165
// expect(1 + 2).toEqual(3);
166
166
// });
167
167
// ```
168
- let reference_id_with_original_list = collect_ids_referenced_to_import ( ctx ) . chain (
169
- collect_ids_referenced_to_global ( ctx )
168
+ let reference_id_with_original_list = collect_ids_referenced_to_import ( semantic ) . chain (
169
+ collect_ids_referenced_to_global ( semantic )
170
170
// set the original of global test function to None
171
171
. map ( |id| ( id, None ) ) ,
172
172
) ;
173
173
174
174
// get the longest valid chain of Jest Call Expression
175
175
reference_id_with_original_list. flat_map ( move |( reference_id, original) | {
176
- let mut id = ctx . symbols ( ) . get_reference ( reference_id) . node_id ( ) ;
176
+ let mut id = semantic . symbols ( ) . get_reference ( reference_id) . node_id ( ) ;
177
177
std:: iter:: from_fn ( move || loop {
178
- let parent = ctx . nodes ( ) . parent_node ( id) ;
178
+ let parent = semantic . nodes ( ) . parent_node ( id) ;
179
179
if let Some ( parent) = parent {
180
180
let parent_kind = parent. kind ( ) ;
181
181
if matches ! ( parent_kind, AstKind :: CallExpression ( _) ) {
@@ -197,19 +197,21 @@ pub fn iter_possible_jest_call_node<'a, 'c>(
197
197
}
198
198
199
199
fn collect_ids_referenced_to_import < ' a , ' c > (
200
- ctx : & ' c LintContext < ' a > ,
200
+ semantic : & ' c Semantic < ' a > ,
201
201
) -> impl Iterator < Item = ( ReferenceId , Option < & ' a str > ) > + ' c {
202
- ctx. symbols ( )
202
+ semantic
203
+ . symbols ( )
203
204
. resolved_references
204
205
. iter_enumerated ( )
205
206
. filter_map ( |( symbol_id, reference_ids) | {
206
- if ctx. symbols ( ) . get_flags ( symbol_id) . is_import ( ) {
207
- let id = ctx. symbols ( ) . get_declaration ( symbol_id) ;
208
- let Some ( AstKind :: ImportDeclaration ( import_decl) ) = ctx. nodes ( ) . parent_kind ( id)
207
+ if semantic. symbols ( ) . get_flags ( symbol_id) . is_import ( ) {
208
+ let id = semantic. symbols ( ) . get_declaration ( symbol_id) ;
209
+ let Some ( AstKind :: ImportDeclaration ( import_decl) ) =
210
+ semantic. nodes ( ) . parent_kind ( id)
209
211
else {
210
212
return None ;
211
213
} ;
212
- let name = ctx . symbols ( ) . get_name ( symbol_id) ;
214
+ let name = semantic . symbols ( ) . get_name ( symbol_id) ;
213
215
214
216
if matches ! ( import_decl. source. value. as_str( ) , "@jest/globals" | "vitest" ) {
215
217
let original = find_original_name ( import_decl, name) ;
@@ -241,9 +243,10 @@ fn find_original_name<'a>(import_decl: &'a ImportDeclaration<'a>, name: &str) ->
241
243
}
242
244
243
245
fn collect_ids_referenced_to_global < ' c > (
244
- ctx : & ' c LintContext ,
246
+ semantic : & ' c Semantic ,
245
247
) -> impl Iterator < Item = ReferenceId > + ' c {
246
- ctx. scopes ( )
248
+ semantic
249
+ . scopes ( )
247
250
. root_unresolved_references ( )
248
251
. iter ( )
249
252
. filter ( |( name, _) | JEST_METHOD_NAMES . contains ( name. as_str ( ) ) )
0 commit comments