Skip to content

Commit 155fe7e

Browse files
committedOct 21, 2024·
refactor(linter): allow Semantic to be passed for collecting Jest nodes (#6720)
This refactors the Jest node collection to not be rule-specific by allowing `Semantic` to be passed instead of the LintContext. This prepares the code to be used outside of a particular rule context.
1 parent ad8f281 commit 155fe7e

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed
 

‎crates/oxc_linter/src/utils/jest.rs

+18-15
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use oxc_ast::{
77
},
88
AstKind,
99
};
10-
use oxc_semantic::{AstNode, ReferenceId};
10+
use oxc_semantic::{AstNode, ReferenceId, Semantic};
1111
use oxc_span::CompactStr;
1212
use phf::phf_set;
1313

@@ -149,13 +149,13 @@ pub struct PossibleJestNode<'a, 'b> {
149149
pub fn collect_possible_jest_call_node<'a, 'c>(
150150
ctx: &'c LintContext<'a>,
151151
) -> Vec<PossibleJestNode<'a, 'c>> {
152-
iter_possible_jest_call_node(ctx).collect()
152+
iter_possible_jest_call_node(ctx.semantic()).collect()
153153
}
154154

155155
/// Iterate over all possible Jest fn Call Expression,
156156
/// for `expect(1).toBe(1)`, the result will be an iter over node `expect(1)` and node `expect(1).toBe(1)`.
157157
pub fn iter_possible_jest_call_node<'a, 'c>(
158-
ctx: &'c LintContext<'a>,
158+
semantic: &'c Semantic<'a>,
159159
) -> impl Iterator<Item = PossibleJestNode<'a, 'c>> + 'c {
160160
// Some people may write codes like below, we need lookup imported test function and global test function.
161161
// ```
@@ -165,17 +165,17 @@ pub fn iter_possible_jest_call_node<'a, 'c>(
165165
// expect(1 + 2).toEqual(3);
166166
// });
167167
// ```
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)
170170
// set the original of global test function to None
171171
.map(|id| (id, None)),
172172
);
173173

174174
// get the longest valid chain of Jest Call Expression
175175
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();
177177
std::iter::from_fn(move || loop {
178-
let parent = ctx.nodes().parent_node(id);
178+
let parent = semantic.nodes().parent_node(id);
179179
if let Some(parent) = parent {
180180
let parent_kind = parent.kind();
181181
if matches!(parent_kind, AstKind::CallExpression(_)) {
@@ -197,19 +197,21 @@ pub fn iter_possible_jest_call_node<'a, 'c>(
197197
}
198198

199199
fn collect_ids_referenced_to_import<'a, 'c>(
200-
ctx: &'c LintContext<'a>,
200+
semantic: &'c Semantic<'a>,
201201
) -> impl Iterator<Item = (ReferenceId, Option<&'a str>)> + 'c {
202-
ctx.symbols()
202+
semantic
203+
.symbols()
203204
.resolved_references
204205
.iter_enumerated()
205206
.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)
209211
else {
210212
return None;
211213
};
212-
let name = ctx.symbols().get_name(symbol_id);
214+
let name = semantic.symbols().get_name(symbol_id);
213215

214216
if matches!(import_decl.source.value.as_str(), "@jest/globals" | "vitest") {
215217
let original = find_original_name(import_decl, name);
@@ -241,9 +243,10 @@ fn find_original_name<'a>(import_decl: &'a ImportDeclaration<'a>, name: &str) ->
241243
}
242244

243245
fn collect_ids_referenced_to_global<'c>(
244-
ctx: &'c LintContext,
246+
semantic: &'c Semantic,
245247
) -> impl Iterator<Item = ReferenceId> + 'c {
246-
ctx.scopes()
248+
semantic
249+
.scopes()
247250
.root_unresolved_references()
248251
.iter()
249252
.filter(|(name, _)| JEST_METHOD_NAMES.contains(name.as_str()))

0 commit comments

Comments
 (0)
Please sign in to comment.