Skip to content

Commit e4c7614

Browse files
authored
refactor(linter): cleanup eslint/no-useless-constructor (oxc-project#11221)
- Update rule docs to format of oxc-project#6050 - Simplify code by removing unnecessary dereference, span() call and kind() check - Make fix test case easier to read by removing use of raw string - Update comment to mention "parameter properties" instead of incorrect "access modifiers"
1 parent a695472 commit e4c7614

File tree

1 file changed

+12
-19
lines changed

1 file changed

+12
-19
lines changed

crates/oxc_linter/src/rules/eslint/no_useless_constructor.rs

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,13 @@ pub struct NoUselessConstructor;
4444
declare_oxc_lint!(
4545
/// ### What it does
4646
///
47-
/// Disallow unnecessary constructors
47+
/// Disallow constructors that can be safely removed without changing how the class works.
4848
///
49-
/// This rule flags class constructors that can be safely removed without
50-
/// changing how the class works.
49+
/// ### Why is this bad?
5150
///
5251
/// ES2015 provides a default class constructor if one is not specified. As
5352
/// such, it is unnecessary to provide an empty constructor or one that
54-
/// simply delegates into its parent class, as in the following examples:
53+
/// simply delegates into its parent class.
5554
///
5655
/// ### Examples
5756
///
@@ -91,7 +90,7 @@ declare_oxc_lint!(
9190
/// doSomething();
9291
/// }
9392
/// }
94-
///```
93+
/// ```
9594
NoUselessConstructor,
9695
eslint,
9796
suspicious,
@@ -117,16 +116,12 @@ impl Rule for NoUselessConstructor {
117116
return;
118117
}
119118

120-
let class = ctx
121-
.nodes()
122-
.ancestors(node.id())
123-
.skip(1)
124-
.find(|parent| matches!(parent.kind(), AstKind::Class(_)));
119+
let class =
120+
ctx.nodes().ancestors(node.id()).skip(1).find_map(|parent| parent.kind().as_class());
125121
debug_assert!(class.is_some(), "Found a constructor outside of a class definition");
126-
let Some(class_node) = class else {
122+
let Some(class) = class else {
127123
return;
128124
};
129-
let AstKind::Class(class) = class_node.kind() else { unreachable!() };
130125
if class.declare {
131126
return;
132127
}
@@ -149,7 +144,7 @@ fn lint_empty_constructor<'a>(
149144
return;
150145
}
151146

152-
// allow constructors with access modifiers since they actually declare
147+
// allow constructors with parameter properties since they actually declare
153148
// class members
154149
if constructor.value.params.items.iter().any(FormalParameter::has_modifier) {
155150
return;
@@ -169,15 +164,15 @@ fn lint_redundant_super_call<'a>(
169164
return;
170165
};
171166

172-
let params = &*constructor.value.params;
167+
let params = &constructor.value.params;
173168
let super_args = &super_call.arguments;
174169

175170
if is_only_simple_params(params)
176171
&& !is_overriding(params)
177172
&& (is_spread_arguments(super_args) || is_passing_through(params, super_args))
178173
{
179174
ctx.diagnostic_with_fix(
180-
no_redundant_super_call(constructor.key.span(), super_call.span()),
175+
no_redundant_super_call(constructor.key.span(), super_call.span),
181176
|fixer| fixer.delete_range(constructor.span),
182177
);
183178
}
@@ -347,10 +342,8 @@ fn test() {
347342
let fix = vec![
348343
("class A { constructor(){} }", "class A { }"),
349344
(
350-
r"
351-
class A extends B { constructor() { super(); } foo() { bar(); } }",
352-
r"
353-
class A extends B { foo() { bar(); } }",
345+
"class A extends B { constructor() { super(); } foo() { bar(); } }",
346+
"class A extends B { foo() { bar(); } }",
354347
),
355348
];
356349

0 commit comments

Comments
 (0)