mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
feat(linter): implement @typescript-eslint/no-empty-interface
This commit is contained in:
parent
83f69b1c61
commit
2e6cb6d3d2
3 changed files with 163 additions and 0 deletions
|
|
@ -54,6 +54,7 @@ oxc_macros::declare_all_lint_rules! {
|
||||||
eslint::use_isnan,
|
eslint::use_isnan,
|
||||||
eslint::valid_typeof,
|
eslint::valid_typeof,
|
||||||
typescript::isolated_declaration,
|
typescript::isolated_declaration,
|
||||||
|
typescript::no_empty_interface,
|
||||||
typescript::no_extra_non_null_assertion,
|
typescript::no_extra_non_null_assertion,
|
||||||
typescript::no_non_null_asserted_optional_chain
|
typescript::no_non_null_asserted_optional_chain
|
||||||
}
|
}
|
||||||
|
|
|
||||||
91
crates/oxc_linter/src/rules/typescript/no_empty_interface.rs
Normal file
91
crates/oxc_linter/src/rules/typescript/no_empty_interface.rs
Normal file
|
|
@ -0,0 +1,91 @@
|
||||||
|
use oxc_ast::AstKind;
|
||||||
|
use oxc_diagnostics::{
|
||||||
|
miette::{self, Diagnostic},
|
||||||
|
thiserror::Error,
|
||||||
|
};
|
||||||
|
use oxc_macros::declare_oxc_lint;
|
||||||
|
use oxc_span::Span;
|
||||||
|
|
||||||
|
use crate::{context::LintContext, rule::Rule, AstNode};
|
||||||
|
|
||||||
|
#[derive(Debug, Error, Diagnostic)]
|
||||||
|
#[error("typescript-eslint(no-empty-interface): an empty interface is equivalent to `{{}}`")]
|
||||||
|
#[diagnostic(severity(warning))]
|
||||||
|
struct NoEmptyInterfaceDiagnostic(#[label] pub Span);
|
||||||
|
|
||||||
|
#[derive(Debug, Error, Diagnostic)]
|
||||||
|
#[error(
|
||||||
|
"typescript-eslint(no-empty-interface): an interface declaring no members is equivalent to its supertype"
|
||||||
|
)]
|
||||||
|
#[diagnostic(severity(warning))]
|
||||||
|
struct NoEmptyInterfaceExtendDiagnostic(#[label] pub Span);
|
||||||
|
|
||||||
|
#[derive(Debug, Default, Clone)]
|
||||||
|
pub struct NoEmptyInterface;
|
||||||
|
|
||||||
|
declare_oxc_lint!(
|
||||||
|
/// ### What it does
|
||||||
|
///
|
||||||
|
/// Disallow the declaration of empty interfaces.
|
||||||
|
///
|
||||||
|
/// ### Why is this bad?
|
||||||
|
///
|
||||||
|
/// An empty interface in TypeScript does very little: any non-nullable value is assignable to {}.
|
||||||
|
/// Using an empty interface is often a sign of programmer error, such as misunderstanding the concept of {} or forgetting to fill in fields.
|
||||||
|
/// This rule aims to ensure that only meaningful interfaces are declared in the code.
|
||||||
|
///
|
||||||
|
/// ### Example
|
||||||
|
/// ```javascript
|
||||||
|
/// interface Foo {}
|
||||||
|
/// interface Bar extends Foo {}
|
||||||
|
/// ```
|
||||||
|
NoEmptyInterface,
|
||||||
|
correctness
|
||||||
|
);
|
||||||
|
|
||||||
|
impl Rule for NoEmptyInterface {
|
||||||
|
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
|
||||||
|
if let AstKind::TSInterfaceDeclaration(interface) = node.kind() {
|
||||||
|
if interface.body.body.is_empty() {
|
||||||
|
match &interface.extends {
|
||||||
|
None => {
|
||||||
|
ctx.diagnostic(NoEmptyInterfaceDiagnostic(interface.span));
|
||||||
|
}
|
||||||
|
Some(extends) if extends.len() == 1 => {
|
||||||
|
ctx.diagnostic(NoEmptyInterfaceExtendDiagnostic(interface.span));
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test() {
|
||||||
|
use crate::tester::Tester;
|
||||||
|
|
||||||
|
let pass = vec![
|
||||||
|
"interface Foo { name: string; }",
|
||||||
|
"interface Foo { name: string; }
|
||||||
|
interface Bar { age: number; }
|
||||||
|
// valid because extending multiple interfaces can be used instead of a union type
|
||||||
|
interface Baz extends Foo, Bar {}",
|
||||||
|
];
|
||||||
|
|
||||||
|
let fail = vec![
|
||||||
|
"interface Foo {}",
|
||||||
|
"interface Foo { props: string; } interface Bar extends Foo {} class Baz {}",
|
||||||
|
"interface Foo { props: string; } interface Bar extends Foo {} class Bar {}",
|
||||||
|
"interface Foo { props: string; } interface Bar extends Foo {} const bar = class Bar {};",
|
||||||
|
"interface Foo { name: string; } interface Bar extends Foo {}",
|
||||||
|
"interface Foo extends Array<number> {}",
|
||||||
|
"interface Foo extends Array<number | {}> {}",
|
||||||
|
"interface Bar { bar: string; } interface Foo extends Array<Bar> {}",
|
||||||
|
"type R = Record<string, unknown>; interface Foo extends R {}",
|
||||||
|
"interface Foo<T> extends Bar<T> {}",
|
||||||
|
"declare module FooBar { type Baz = typeof baz; export interface Bar extends Baz {} }",
|
||||||
|
];
|
||||||
|
|
||||||
|
Tester::new_without_config(NoEmptyInterface::NAME, pass, fail).test_and_snapshot();
|
||||||
|
}
|
||||||
71
crates/oxc_linter/src/snapshots/no_empty_interface.snap
Normal file
71
crates/oxc_linter/src/snapshots/no_empty_interface.snap
Normal file
|
|
@ -0,0 +1,71 @@
|
||||||
|
---
|
||||||
|
source: crates/oxc_linter/src/tester.rs
|
||||||
|
expression: no_empty_interface
|
||||||
|
---
|
||||||
|
⚠ typescript-eslint(no-empty-interface): an empty interface is equivalent to `{}`
|
||||||
|
╭─[no_empty_interface.tsx:1:1]
|
||||||
|
1 │ interface Foo {}
|
||||||
|
· ────────────────
|
||||||
|
╰────
|
||||||
|
|
||||||
|
⚠ typescript-eslint(no-empty-interface): an interface declaring no members is equivalent to its supertype
|
||||||
|
╭─[no_empty_interface.tsx:1:1]
|
||||||
|
1 │ interface Foo { props: string; } interface Bar extends Foo {} class Baz {}
|
||||||
|
· ────────────────────────────
|
||||||
|
╰────
|
||||||
|
|
||||||
|
⚠ typescript-eslint(no-empty-interface): an interface declaring no members is equivalent to its supertype
|
||||||
|
╭─[no_empty_interface.tsx:1:1]
|
||||||
|
1 │ interface Foo { props: string; } interface Bar extends Foo {} class Bar {}
|
||||||
|
· ────────────────────────────
|
||||||
|
╰────
|
||||||
|
|
||||||
|
⚠ typescript-eslint(no-empty-interface): an interface declaring no members is equivalent to its supertype
|
||||||
|
╭─[no_empty_interface.tsx:1:1]
|
||||||
|
1 │ interface Foo { props: string; } interface Bar extends Foo {} const bar = class Bar {};
|
||||||
|
· ────────────────────────────
|
||||||
|
╰────
|
||||||
|
|
||||||
|
⚠ typescript-eslint(no-empty-interface): an interface declaring no members is equivalent to its supertype
|
||||||
|
╭─[no_empty_interface.tsx:1:1]
|
||||||
|
1 │ interface Foo { name: string; } interface Bar extends Foo {}
|
||||||
|
· ────────────────────────────
|
||||||
|
╰────
|
||||||
|
|
||||||
|
⚠ typescript-eslint(no-empty-interface): an interface declaring no members is equivalent to its supertype
|
||||||
|
╭─[no_empty_interface.tsx:1:1]
|
||||||
|
1 │ interface Foo extends Array<number> {}
|
||||||
|
· ──────────────────────────────────────
|
||||||
|
╰────
|
||||||
|
|
||||||
|
⚠ typescript-eslint(no-empty-interface): an interface declaring no members is equivalent to its supertype
|
||||||
|
╭─[no_empty_interface.tsx:1:1]
|
||||||
|
1 │ interface Foo extends Array<number | {}> {}
|
||||||
|
· ───────────────────────────────────────────
|
||||||
|
╰────
|
||||||
|
|
||||||
|
⚠ typescript-eslint(no-empty-interface): an interface declaring no members is equivalent to its supertype
|
||||||
|
╭─[no_empty_interface.tsx:1:1]
|
||||||
|
1 │ interface Bar { bar: string; } interface Foo extends Array<Bar> {}
|
||||||
|
· ───────────────────────────────────
|
||||||
|
╰────
|
||||||
|
|
||||||
|
⚠ typescript-eslint(no-empty-interface): an interface declaring no members is equivalent to its supertype
|
||||||
|
╭─[no_empty_interface.tsx:1:1]
|
||||||
|
1 │ type R = Record<string, unknown>; interface Foo extends R {}
|
||||||
|
· ──────────────────────────
|
||||||
|
╰────
|
||||||
|
|
||||||
|
⚠ typescript-eslint(no-empty-interface): an interface declaring no members is equivalent to its supertype
|
||||||
|
╭─[no_empty_interface.tsx:1:1]
|
||||||
|
1 │ interface Foo<T> extends Bar<T> {}
|
||||||
|
· ──────────────────────────────────
|
||||||
|
╰────
|
||||||
|
|
||||||
|
⚠ typescript-eslint(no-empty-interface): an interface declaring no members is equivalent to its supertype
|
||||||
|
╭─[no_empty_interface.tsx:1:1]
|
||||||
|
1 │ declare module FooBar { type Baz = typeof baz; export interface Bar extends Baz {} }
|
||||||
|
· ────────────────────────────
|
||||||
|
╰────
|
||||||
|
|
||||||
|
|
||||||
Loading…
Reference in a new issue