@@ -26,7 +26,8 @@ pub struct NoMisusedNew;
26
26
declare_oxc_lint ! (
27
27
/// ### What it does
28
28
///
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`.
30
31
///
31
32
/// ### Why is this bad?
32
33
///
@@ -38,17 +39,34 @@ declare_oxc_lint!(
38
39
/// Developers new to JavaScript classes and/or TypeScript interfaces may
39
40
/// sometimes confuse when to use constructor or new.
40
41
///
41
- /// ### Example
42
+ /// ### Examples
43
+ ///
44
+ /// Examples of **incorrect** code for this rule:
42
45
/// ```typescript
43
46
/// declare class C {
44
47
/// new(): C;
45
48
/// }
49
+ /// ```
46
50
///
51
+ /// ```typescript
47
52
/// interface I {
48
53
/// new (): I;
49
54
/// constructor(): void;
50
55
/// }
51
56
/// ```
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
+ /// ```
52
70
NoMisusedNew ,
53
71
typescript,
54
72
correctness
@@ -61,18 +79,21 @@ impl Rule for NoMisusedNew {
61
79
let decl_name = & interface_decl. id . name ;
62
80
63
81
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
+ ) ) ) ;
76
97
}
77
98
}
78
99
}
@@ -85,27 +106,25 @@ impl Rule for NoMisusedNew {
85
106
}
86
107
}
87
108
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 ;
90
113
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 ( ) ) ) ;
109
128
}
110
129
}
111
130
}
0 commit comments