fix(linter): false positive in typescript/consistent-type-definitions (#7560)

closes #7552
This commit is contained in:
dalaoshu 2024-12-01 16:03:29 +08:00 committed by GitHub
parent 6cc7a48b52
commit 123b5b7f38
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 62 additions and 2 deletions

View file

@ -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;
@ -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;
@ -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![
@ -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)

View file

@ -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`