Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions crates/oxc_linter/src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,21 @@ impl<'a> LintContext<'a> {
.map(|(a, _)| a as u32)
}

/// Finds the next 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_next_token_within(&self, start: u32, end: u32, token: &str) -> Option<u32> {
let source = self.source_range(Span::new(start, end));

source
.match_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 {
Expand Down
16 changes: 13 additions & 3 deletions crates/oxc_linter/src/rules/import/no_empty_named_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,17 @@ impl Rule for NoEmptyNamedBlocks {
};

// import Default, {} from 'mod'
// Find the comma and "from" keyword, skipping any comments
let Some(comma_offset) = ctx.find_next_token_from(specifier.span.end, ",") else { return };
let Some(comma_offset) =
ctx.find_next_token_within(specifier.span.end, import_decl.span.end, ",")
else {
return;
};
let comma_pos = specifier.span.end + comma_offset;
let Some(from_offset) = ctx.find_next_token_from(comma_pos, "from") else { return };

let Some(from_offset) = ctx.find_next_token_within(comma_pos, import_decl.span.end, "from")
else {
return;
};
let from_pos = comma_pos + from_offset;

let start = specifier.span.end;
Expand All @@ -94,6 +101,9 @@ fn test() {
"import type Default, { Named } from 'mod'",
"import type * as Namespace from 'mod'",
"import * as Namespace from 'mod'",
r#"
import nodePath from "node:path"; a,"from";
"#,
];

let fail = vec![
Expand Down
Loading