diff --git a/crates/oxc_linter/src/rules/typescript/no_unnecessary_type_constraint.rs b/crates/oxc_linter/src/rules/typescript/no_unnecessary_type_constraint.rs index c0887cccb47fe..b54c6e234da98 100644 --- a/crates/oxc_linter/src/rules/typescript/no_unnecessary_type_constraint.rs +++ b/crates/oxc_linter/src/rules/typescript/no_unnecessary_type_constraint.rs @@ -32,20 +32,46 @@ declare_oxc_lint!( /// /// ### Why is this bad? /// - /// Generic type parameters (``) in TypeScript may be "constrained" with an extends keyword. - /// When no extends is provided, type parameters default a constraint to unknown. It is therefore redundant to extend from any or unknown. + /// Generic type parameters (``) in TypeScript may be "constrained" with an `extends` + /// keyword. When no `extends` is provided, type parameters default a constraint to `unknown`. + /// It is therefore redundant to `extend` from `any` or `unknown`. /// - /// ### Example + /// ### Examples + /// + /// Examples of **incorrect** code for this rule: /// ```typescript /// interface FooAny {} /// interface FooUnknown {} + /// /// type BarAny = {}; /// type BarUnknown = {}; + /// + /// const QuuxAny = () => {}; + /// + /// function QuuzAny() {} + /// ``` + /// + /// ```typescript /// class BazAny { /// quxAny() {} /// } - /// const QuuxAny = () => {}; - /// function QuuzAny() {} + /// ``` + /// + /// Examples of **correct** code for this rule: + /// ```typescript + /// interface Foo {} + /// + /// type Bar = {}; + /// + /// const Quux = () => {}; + /// + /// function Quuz() {} + /// ``` + /// + /// ```typescript + /// class Baz { + /// qux() {} + /// } /// ``` NoUnnecessaryTypeConstraint, typescript, @@ -54,22 +80,26 @@ declare_oxc_lint!( impl Rule for NoUnnecessaryTypeConstraint { fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) { - if let AstKind::TSTypeParameterDeclaration(decl) = node.kind() { - for param in &decl.params { - if let Some(ty) = ¶m.constraint { - let (value, ty_span) = match ty { - TSType::TSAnyKeyword(t) => ("any", t.span), - TSType::TSUnknownKeyword(t) => ("unknown", t.span), - _ => continue, - }; - ctx.diagnostic(no_unnecessary_type_constraint_diagnostic( - param.name.name.as_str(), - value, - param.name.span, - ty_span, - )); - } - } + let AstKind::TSTypeParameterDeclaration(decl) = node.kind() else { + return; + }; + + for param in &decl.params { + let Some(ty) = ¶m.constraint else { + continue; + }; + + let (value, ty_span) = match ty { + TSType::TSAnyKeyword(t) => ("any", t.span), + TSType::TSUnknownKeyword(t) => ("unknown", t.span), + _ => continue, + }; + ctx.diagnostic(no_unnecessary_type_constraint_diagnostic( + param.name.name.as_str(), + value, + param.name.span, + ty_span, + )); } }