Skip to content

Commit

Permalink
fix(linter): false positive in `typescript/consistent-type-definition…
Browse files Browse the repository at this point in the history
…s` (#7560)

closes #7552
  • Loading branch information
shulaoda authored Dec 1, 2024
1 parent 6cc7a48 commit 123b5b7
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,14 @@ impl Rule for ConsistentTypeDefinitions {
TSType::TSTypeLiteral(_)
if self.config == ConsistentTypeDefinitionsConfig::Interface =>
{
let start = if decl.declare { decl.span.start + 8 } else { decl.span.start };
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)
} else {
decl.span.start
};

let name_span_start = &decl.id.span.start;
let mut name_span_end = &decl.id.span.end;
Expand Down Expand Up @@ -172,7 +179,14 @@ impl Rule for ConsistentTypeDefinitions {
AstKind::TSInterfaceDeclaration(decl)
if self.config == ConsistentTypeDefinitionsConfig::Type =>
{
let start = if decl.declare { decl.span.start + 8 } else { decl.span.start };
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)
} else {
decl.span.start
};

let name_span_start = &decl.id.span.start;
let mut name_span_end = &decl.id.span.end;
Expand Down Expand Up @@ -359,6 +373,11 @@ fn test() {
",
Some(serde_json::json!(["type"])),
),
// Issue: <https://github.com/oxc-project/oxc/issues/7552>
("declare…type S={}", Some(serde_json::json!(["interface"]))),
("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"]))),
];

let fix = vec![
Expand Down Expand Up @@ -499,6 +518,19 @@ export declare type Test = {
",
Some(serde_json::json!(["type"])),
),
// Issue: <https://github.com/oxc-project/oxc/issues/7552>
("declare…type S={}", "declare…interface S {}", Some(serde_json::json!(["interface"]))),
("declare…interface S {}", "declare…type S = {}", Some(serde_json::json!(["type"]))),
(
"export declare…type S={}",
"export declare…interface S {}",
Some(serde_json::json!(["interface"])),
),
(
"export declare…interface S {}",
"export declare…type S = {}",
Some(serde_json::json!(["type"])),
),
];

Tester::new(ConsistentTypeDefinitions::NAME, ConsistentTypeDefinitions::CATEGORY, pass, fail)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,31 @@ snapshot_kind: text
3 │ foo: string;
╰────
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:10]
1 │ declare…type S={}
· ────
╰────
help: Use an `interface` instead of a `type`

typescript-eslint(consistent-type-definitions): Use an `type` instead of a `interface`
╭─[consistent_type_definitions.tsx:1:10]
1 │ declare…interface S {}
· ─────────
╰────
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:17]
1 │ export declare…type S={}
· ────
╰────
help: Use an `interface` instead of a `type`

typescript-eslint(consistent-type-definitions): Use an `type` instead of a `interface`
╭─[consistent_type_definitions.tsx:1:17]
1 │ export declare…interface S {}
· ─────────
╰────
help: Use an `type` instead of a `interface`

0 comments on commit 123b5b7

Please sign in to comment.