mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
fix(linter): false positive in typescript/consistent-type-definitions (#7560)
closes #7552
This commit is contained in:
parent
6cc7a48b52
commit
123b5b7f38
2 changed files with 62 additions and 2 deletions
|
|
@ -78,7 +78,14 @@ impl Rule for ConsistentTypeDefinitions {
|
||||||
TSType::TSTypeLiteral(_)
|
TSType::TSTypeLiteral(_)
|
||||||
if self.config == ConsistentTypeDefinitionsConfig::Interface =>
|
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 name_span_start = &decl.id.span.start;
|
||||||
let mut name_span_end = &decl.id.span.end;
|
let mut name_span_end = &decl.id.span.end;
|
||||||
|
|
@ -172,7 +179,14 @@ impl Rule for ConsistentTypeDefinitions {
|
||||||
AstKind::TSInterfaceDeclaration(decl)
|
AstKind::TSInterfaceDeclaration(decl)
|
||||||
if self.config == ConsistentTypeDefinitionsConfig::Type =>
|
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 name_span_start = &decl.id.span.start;
|
||||||
let mut name_span_end = &decl.id.span.end;
|
let mut name_span_end = &decl.id.span.end;
|
||||||
|
|
@ -359,6 +373,11 @@ fn test() {
|
||||||
",
|
",
|
||||||
Some(serde_json::json!(["type"])),
|
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![
|
let fix = vec![
|
||||||
|
|
@ -499,6 +518,19 @@ export declare type Test = {
|
||||||
",
|
",
|
||||||
Some(serde_json::json!(["type"])),
|
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)
|
Tester::new(ConsistentTypeDefinitions::NAME, ConsistentTypeDefinitions::CATEGORY, pass, fail)
|
||||||
|
|
|
||||||
|
|
@ -147,3 +147,31 @@ snapshot_kind: text
|
||||||
3 │ foo: string;
|
3 │ foo: string;
|
||||||
╰────
|
╰────
|
||||||
help: Use an `type` instead of a `interface`
|
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`
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue