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
13 changes: 13 additions & 0 deletions crates/oxc_linter/src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,19 @@ impl<'a> LintContext<'a> {
span.source_text(self.parent.semantic().source_text())
}

/// Finds the next occurrence of the given token in the source code,
/// starting from the specified position, skipping over comments.
#[expect(clippy::cast_possible_truncation)]
pub fn find_next_token_from(&self, start: u32, token: &str) -> Option<u32> {
let source =
self.source_range(Span::new(start, self.parent.semantic().source_text().len() as u32));

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
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ impl Rule for ConsistentTypeDefinitions {
{
let start = if decl.declare {
let base_start = decl.span.start + 7;
ctx.source_range(Span::new(base_start, decl.span.end))
.find("type")
.map_or(base_start + 1, |v| u32::try_from(v).unwrap_or(0) + base_start)

ctx.find_next_token_from(base_start, "type")
.map_or(base_start + 1, |v| v + base_start)
} else {
decl.span.start
};
Expand Down Expand Up @@ -201,9 +201,9 @@ impl Rule for ConsistentTypeDefinitions {
{
let start = if decl.declare {
let base_start = decl.span.start + 7;
ctx.source_range(Span::new(base_start, decl.span.end))
.find("interface")
.map_or(base_start + 1, |v| u32::try_from(v).unwrap_or(0) + base_start)

ctx.find_next_token_from(base_start, "interface")
.map_or(base_start + 1, |v| v + base_start)
} else {
decl.span.start
};
Expand Down Expand Up @@ -383,6 +383,8 @@ fn test() {
("declare…interface S {}", Some(serde_json::json!(["type"]))),
("export declare…type S={}", Some(serde_json::json!(["interface"]))),
("export declare…interface S {}", Some(serde_json::json!(["type"]))),
("declare /* interface */ interface T { x: number; };", Some(serde_json::json!(["type"]))),
("declare /* type */ type T = { x: number; };", Some(serde_json::json!(["interface"]))),
];

let fix = vec![
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,17 @@ source: crates/oxc_linter/src/tester.rs
· ─────────
╰────
help: Use an `type` instead of a `interface`

⚠ typescript-eslint(consistent-type-definitions): Use an `type` instead of a `interface`
╭─[consistent_type_definitions.tsx:1:25]
1 │ declare /* interface */ interface T { x: number; };
· ─────────
╰────
help: Use an `type` instead of a `interface`

⚠ typescript-eslint(consistent-type-definitions): Use an `interface` instead of a `type`
╭─[consistent_type_definitions.tsx:1:20]
1 │ declare /* type */ type T = { x: number; };
· ────
╰────
help: Use an `interface` instead of a `type`
Loading