From 9f533db16ed151ce734e921810eb253e8d9404b3 Mon Sep 17 00:00:00 2001 From: camc314 <18101008+camc314@users.noreply.github.com> Date: Sat, 31 Jan 2026 14:22:38 +0000 Subject: [PATCH] feat(linter): add `find_prev_token_within` method for token search (#18769) --- crates/oxc_linter/src/context/mod.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/crates/oxc_linter/src/context/mod.rs b/crates/oxc_linter/src/context/mod.rs index e34b78edc97f6..3733e589b4d2d 100644 --- a/crates/oxc_linter/src/context/mod.rs +++ b/crates/oxc_linter/src/context/mod.rs @@ -173,6 +173,21 @@ impl<'a> LintContext<'a> { .map(|(a, _)| a as u32) } + /// Finds the previous occurrence of the given token within a bounded span, + /// starting from the specified position, skipping over comments. + /// + /// Returns the offset from `start` if the token is found before `end`, + /// otherwise returns `None`. + #[expect(clippy::cast_possible_truncation)] + pub fn find_prev_token_within(&self, start: u32, end: u32, token: &str) -> Option { + let source = self.source_range(Span::new(start, end)); + + source + .rmatch_indices(token) + .find(|(a, _)| !self.is_inside_comment(start + *a as u32)) + .map(|(a, _)| a as u32) + } + /// Path to the file currently being linted. #[inline] pub fn file_path(&self) -> &Path {