From c6b45bf824f8d7b65a42b9a22fd9dea1e3440fe5 Mon Sep 17 00:00:00 2001 From: therewillbecode Date: Thu, 13 Mar 2025 21:03:29 +0000 Subject: [PATCH 1/2] Remove if let nesting --- .../src/rules/typescript/no_misused_new.rs | 65 ++++++++++--------- 1 file changed, 33 insertions(+), 32 deletions(-) diff --git a/crates/oxc_linter/src/rules/typescript/no_misused_new.rs b/crates/oxc_linter/src/rules/typescript/no_misused_new.rs index 2f4a868a087bd..7e57e6f3e5f6e 100644 --- a/crates/oxc_linter/src/rules/typescript/no_misused_new.rs +++ b/crates/oxc_linter/src/rules/typescript/no_misused_new.rs @@ -61,18 +61,21 @@ impl Rule for NoMisusedNew { let decl_name = &interface_decl.id.name; for signature in &interface_decl.body.body { - if let TSSignature::TSConstructSignatureDeclaration(sig) = signature { - if let Some(return_type) = &sig.return_type { - if let TSType::TSTypeReference(type_ref) = &return_type.type_annotation - { - if let TSTypeName::IdentifierReference(id) = &type_ref.type_name { - if id.name == decl_name { - ctx.diagnostic(no_misused_new_interface_diagnostic( - Span::new(sig.span.start, sig.span.start + 3), - )); - } - } - } + let TSSignature::TSConstructSignatureDeclaration(sig) = signature else { + continue; + }; + let Some(return_type) = &sig.return_type else { + continue; + }; + let TSType::TSTypeReference(type_ref) = &return_type.type_annotation else { + continue; + }; + if let TSTypeName::IdentifierReference(id) = &type_ref.type_name { + if id.name == decl_name { + ctx.diagnostic(no_misused_new_interface_diagnostic(Span::new( + sig.span.start, + sig.span.start + 3, + ))); } } } @@ -85,27 +88,25 @@ impl Rule for NoMisusedNew { } } AstKind::Class(cls) => { - if let Some(cls_id) = &cls.id { - let cls_name = &cls_id.name; + let Some(cls_id) = &cls.id else { + return; + }; + let cls_name = &cls_id.name; - for element in &cls.body.body { - if let ClassElement::MethodDefinition(method) = element { - if method.key.is_specific_id("new") && method.value.body.is_none() { - if let Some(return_type) = &method.value.return_type { - if let TSType::TSTypeReference(type_ref) = - &return_type.type_annotation - { - if let TSTypeName::IdentifierReference(current_id) = - &type_ref.type_name - { - if current_id.name == cls_name { - ctx.diagnostic(no_misused_new_class_diagnostic( - method.key.span(), - )); - } - } - } - } + for element in &cls.body.body { + let ClassElement::MethodDefinition(method) = element else { + continue; + }; + if method.key.is_specific_id("new") && method.value.body.is_none() { + let Some(return_type) = &method.value.return_type else { + continue; + }; + let TSType::TSTypeReference(type_ref) = &return_type.type_annotation else { + continue; + }; + if let TSTypeName::IdentifierReference(current_id) = &type_ref.type_name { + if current_id.name == cls_name { + ctx.diagnostic(no_misused_new_class_diagnostic(method.key.span())); } } } From 395d6e73c1fdc8655d845e87920849b7210c98ce Mon Sep 17 00:00:00 2001 From: therewillbecode Date: Thu, 13 Mar 2025 21:17:30 +0000 Subject: [PATCH 2/2] Add correctness examples in docs --- .../src/rules/typescript/no_misused_new.rs | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/crates/oxc_linter/src/rules/typescript/no_misused_new.rs b/crates/oxc_linter/src/rules/typescript/no_misused_new.rs index 7e57e6f3e5f6e..d338bbc00b3b8 100644 --- a/crates/oxc_linter/src/rules/typescript/no_misused_new.rs +++ b/crates/oxc_linter/src/rules/typescript/no_misused_new.rs @@ -26,7 +26,8 @@ pub struct NoMisusedNew; declare_oxc_lint!( /// ### What it does /// - /// Enforce valid definition of `new` and `constructor` + /// Enforces valid definition of new and constructor. This rule prevents classes from defining + /// a method named `new` and interfaces from defining a method named `constructor`. /// /// ### Why is this bad? /// @@ -38,17 +39,34 @@ declare_oxc_lint!( /// Developers new to JavaScript classes and/or TypeScript interfaces may /// sometimes confuse when to use constructor or new. /// - /// ### Example + /// ### Examples + /// + /// Examples of **incorrect** code for this rule: /// ```typescript /// declare class C { /// new(): C; /// } + /// ``` /// + /// ```typescript /// interface I { /// new (): I; /// constructor(): void; /// } /// ``` + /// + /// Examples of **correct** code for this rule: + /// ```typescript + /// declare class C { + /// constructor(); + /// } + /// ``` + /// + /// ```typescript + /// interface I { + /// new (): C; + /// } + /// ``` NoMisusedNew, typescript, correctness