Skip to content

Commit 5a9e1b9

Browse files
authoredMar 14, 2025··
refactor(linter): improve typescript-no-misused-new (#9766)
- Refactor `if lets` into `let ... else` for better readability through reduced nesting. - Add correctness examples and better "What it does" explanation to docs #6050
1 parent 27d6e9b commit 5a9e1b9

File tree

1 file changed

+53
-34
lines changed

1 file changed

+53
-34
lines changed
 

‎crates/oxc_linter/src/rules/typescript/no_misused_new.rs

+53-34
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ pub struct NoMisusedNew;
2626
declare_oxc_lint!(
2727
/// ### What it does
2828
///
29-
/// Enforce valid definition of `new` and `constructor`
29+
/// Enforces valid definition of new and constructor. This rule prevents classes from defining
30+
/// a method named `new` and interfaces from defining a method named `constructor`.
3031
///
3132
/// ### Why is this bad?
3233
///
@@ -38,17 +39,34 @@ declare_oxc_lint!(
3839
/// Developers new to JavaScript classes and/or TypeScript interfaces may
3940
/// sometimes confuse when to use constructor or new.
4041
///
41-
/// ### Example
42+
/// ### Examples
43+
///
44+
/// Examples of **incorrect** code for this rule:
4245
/// ```typescript
4346
/// declare class C {
4447
/// new(): C;
4548
/// }
49+
/// ```
4650
///
51+
/// ```typescript
4752
/// interface I {
4853
/// new (): I;
4954
/// constructor(): void;
5055
/// }
5156
/// ```
57+
///
58+
/// Examples of **correct** code for this rule:
59+
/// ```typescript
60+
/// declare class C {
61+
/// constructor();
62+
/// }
63+
/// ```
64+
///
65+
/// ```typescript
66+
/// interface I {
67+
/// new (): C;
68+
/// }
69+
/// ```
5270
NoMisusedNew,
5371
typescript,
5472
correctness
@@ -61,18 +79,21 @@ impl Rule for NoMisusedNew {
6179
let decl_name = &interface_decl.id.name;
6280

6381
for signature in &interface_decl.body.body {
64-
if let TSSignature::TSConstructSignatureDeclaration(sig) = signature {
65-
if let Some(return_type) = &sig.return_type {
66-
if let TSType::TSTypeReference(type_ref) = &return_type.type_annotation
67-
{
68-
if let TSTypeName::IdentifierReference(id) = &type_ref.type_name {
69-
if id.name == decl_name {
70-
ctx.diagnostic(no_misused_new_interface_diagnostic(
71-
Span::new(sig.span.start, sig.span.start + 3),
72-
));
73-
}
74-
}
75-
}
82+
let TSSignature::TSConstructSignatureDeclaration(sig) = signature else {
83+
continue;
84+
};
85+
let Some(return_type) = &sig.return_type else {
86+
continue;
87+
};
88+
let TSType::TSTypeReference(type_ref) = &return_type.type_annotation else {
89+
continue;
90+
};
91+
if let TSTypeName::IdentifierReference(id) = &type_ref.type_name {
92+
if id.name == decl_name {
93+
ctx.diagnostic(no_misused_new_interface_diagnostic(Span::new(
94+
sig.span.start,
95+
sig.span.start + 3,
96+
)));
7697
}
7798
}
7899
}
@@ -85,27 +106,25 @@ impl Rule for NoMisusedNew {
85106
}
86107
}
87108
AstKind::Class(cls) => {
88-
if let Some(cls_id) = &cls.id {
89-
let cls_name = &cls_id.name;
109+
let Some(cls_id) = &cls.id else {
110+
return;
111+
};
112+
let cls_name = &cls_id.name;
90113

91-
for element in &cls.body.body {
92-
if let ClassElement::MethodDefinition(method) = element {
93-
if method.key.is_specific_id("new") && method.value.body.is_none() {
94-
if let Some(return_type) = &method.value.return_type {
95-
if let TSType::TSTypeReference(type_ref) =
96-
&return_type.type_annotation
97-
{
98-
if let TSTypeName::IdentifierReference(current_id) =
99-
&type_ref.type_name
100-
{
101-
if current_id.name == cls_name {
102-
ctx.diagnostic(no_misused_new_class_diagnostic(
103-
method.key.span(),
104-
));
105-
}
106-
}
107-
}
108-
}
114+
for element in &cls.body.body {
115+
let ClassElement::MethodDefinition(method) = element else {
116+
continue;
117+
};
118+
if method.key.is_specific_id("new") && method.value.body.is_none() {
119+
let Some(return_type) = &method.value.return_type else {
120+
continue;
121+
};
122+
let TSType::TSTypeReference(type_ref) = &return_type.type_annotation else {
123+
continue;
124+
};
125+
if let TSTypeName::IdentifierReference(current_id) = &type_ref.type_name {
126+
if current_id.name == cls_name {
127+
ctx.diagnostic(no_misused_new_class_diagnostic(method.key.span()));
109128
}
110129
}
111130
}

0 commit comments

Comments
 (0)
Please sign in to comment.