Skip to content

Commit 6ae0227

Browse files
committed
refactor: remove redundant code
1 parent b1b1d68 commit 6ae0227

File tree

3 files changed

+53
-61
lines changed

3 files changed

+53
-61
lines changed

crates/oxc_linter/src/generated/rule_runner_impls.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3546,7 +3546,7 @@ impl RuleRunner for crate::rules::unicorn::prefer_date_now::PreferDateNow {
35463546

35473547
impl RuleRunner for crate::rules::unicorn::prefer_default_parameters::PreferDefaultParameters {
35483548
const NODE_TYPES: Option<&AstTypesBitset> = Some(&AstTypesBitset::from_types(&[
3549-
AstType::ExpressionStatement,
3549+
AstType::AssignmentExpression,
35503550
AstType::VariableDeclaration,
35513551
]));
35523552
const RUN_FUNCTIONS: RuleRunFunctionsImplemented = RuleRunFunctionsImplemented::Run;

crates/oxc_linter/src/rules/unicorn/prefer_default_parameters.rs

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use oxc_ast::{
22
AstKind,
33
ast::{
44
AssignmentOperator, AssignmentTarget, BindingPatternKind, Expression, FormalParameter,
5-
LogicalOperator,
5+
LogicalOperator, Statement,
66
},
77
};
88
use oxc_diagnostics::OxcDiagnostic;
@@ -86,13 +86,6 @@ fn find_enclosing_function<'a>(
8686
}
8787
}
8888

89-
fn get_param_name<'a>(param: &FormalParameter<'a>) -> Option<&'a str> {
90-
match &param.pattern.kind {
91-
BindingPatternKind::BindingIdentifier(ident) => Some(ident.name.as_str()),
92-
_ => None,
93-
}
94-
}
95-
9689
fn has_no_side_effects_before<'a>(
9790
ctx: &LintContext<'a>,
9891
node: &AstNode<'a>,
@@ -107,7 +100,13 @@ fn has_no_side_effects_before<'a>(
107100
let node_span = node.kind().span();
108101

109102
for stmt in &body.statements {
110-
if stmt.span() == node_span {
103+
let stmt_matches = match stmt {
104+
Statement::ExpressionStatement(expr_stmt) => expr_stmt.expression.span() == node_span,
105+
Statement::VariableDeclaration(var_decl) => var_decl.span == node_span,
106+
_ => stmt.span() == node_span,
107+
};
108+
109+
if stmt_matches {
111110
return true;
112111
}
113112

@@ -244,7 +243,7 @@ fn check_expression<'a>(
244243

245244
let param_name = param_ident.name.as_str();
246245

247-
if !&logical_expr.right.get_inner_expression().is_literal() {
246+
if !logical_expr.right.get_inner_expression().is_literal() {
248247
return;
249248
}
250249

@@ -263,9 +262,9 @@ fn check_expression<'a>(
263262
_ => return,
264263
};
265264

266-
let Some((param_index, param)) =
267-
params.items.iter().enumerate().find(|(_, p)| get_param_name(p) == Some(param_name))
268-
else {
265+
let Some((param_index, param)) = params.items.iter().enumerate().find(|(_, p)| {
266+
p.pattern.get_binding_identifier().map(|ident| ident.name.as_str()) == Some(param_name)
267+
}) else {
269268
return;
270269
};
271270

@@ -281,10 +280,6 @@ fn check_expression<'a>(
281280
return;
282281
}
283282

284-
if !matches!(param.pattern.kind, BindingPatternKind::BindingIdentifier(_)) {
285-
return;
286-
}
287-
288283
if !has_no_side_effects_before(ctx, node, function_body_id, param_name) {
289284
return;
290285
}
@@ -303,23 +298,20 @@ fn check_expression<'a>(
303298
impl Rule for PreferDefaultParameters {
304299
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
305300
match node.kind() {
306-
AstKind::ExpressionStatement(expr_stmt) => {
307-
if let Expression::AssignmentExpression(assign_expr) = &expr_stmt.expression {
308-
if assign_expr.operator != AssignmentOperator::Assign {
309-
return;
310-
}
311-
if let AssignmentTarget::AssignmentTargetIdentifier(left_ident) =
312-
&assign_expr.left
313-
{
314-
check_expression(
315-
ctx,
316-
node,
317-
&left_ident.name,
318-
&assign_expr.right,
319-
true,
320-
expr_stmt.span,
321-
);
322-
}
301+
AstKind::AssignmentExpression(assign_expr) => {
302+
if assign_expr.operator != AssignmentOperator::Assign {
303+
return;
304+
}
305+
if let AssignmentTarget::AssignmentTargetIdentifier(left_ident) = &assign_expr.left
306+
{
307+
check_expression(
308+
ctx,
309+
node,
310+
&left_ident.name,
311+
&assign_expr.right,
312+
true,
313+
assign_expr.span,
314+
);
323315
}
324316
}
325317
AstKind::VariableDeclaration(var_decl) => {

crates/oxc_linter/src/snapshots/unicorn_prefer_default_parameters.snap

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ source: crates/oxc_linter/src/tester.rs
55
╭─[prefer_default_parameters.tsx:2:2]
66
1function abc(foo) {
77
2foo = foo || 123;
8-
· ────────────────
8+
· ────────────────
99
3 │ }
1010
╰────
1111
help: Replace the reassignment with a default parameter.
@@ -14,7 +14,7 @@ source: crates/oxc_linter/src/tester.rs
1414
╭─[prefer_default_parameters.tsx:2:2]
1515
1function abc(foo) {
1616
2foo = foo || true;
17-
· ─────────────────
17+
· ─────────────────
1818
3 │ }
1919
╰────
2020
help: Replace the reassignment with a default parameter.
@@ -23,7 +23,7 @@ source: crates/oxc_linter/src/tester.rs
2323
╭─[prefer_default_parameters.tsx:2:2]
2424
1function abc(foo) {
2525
2foo = foo || 123;
26-
· ────────────────
26+
· ────────────────
2727
3console.log(foo);
2828
╰────
2929
help: Replace the reassignment with a default parameter.
@@ -50,7 +50,7 @@ source: crates/oxc_linter/src/tester.rs
5050
╭─[prefer_default_parameters.tsx:2:2]
5151
1const abc = function(foo) {
5252
2foo = foo || 123;
53-
· ────────────────
53+
· ────────────────
5454
3 │ }
5555
╰────
5656
help: Replace the reassignment with a default parameter.
@@ -59,7 +59,7 @@ source: crates/oxc_linter/src/tester.rs
5959
╭─[prefer_default_parameters.tsx:2:2]
6060
1const abc = (foo) => {
6161
2foo = foo || 'bar';
62-
· ──────────────────
62+
· ──────────────────
6363
3 │ };
6464
╰────
6565
help: Replace the reassignment with a default parameter.
@@ -68,7 +68,7 @@ source: crates/oxc_linter/src/tester.rs
6868
╭─[prefer_default_parameters.tsx:2:2]
6969
1const abc = foo => {
7070
2foo = foo || 'bar';
71-
· ──────────────────
71+
· ──────────────────
7272
3 │ };
7373
╰────
7474
help: Replace the reassignment with a default parameter.
@@ -86,7 +86,7 @@ source: crates/oxc_linter/src/tester.rs
8686
╭─[prefer_default_parameters.tsx:2:2]
8787
1function abc(foo) {
8888
2foo = foo || 'bar';
89-
· ──────────────────
89+
· ──────────────────
9090
3bar();
9191
╰────
9292
help: Replace the reassignment with a default parameter.
@@ -95,7 +95,7 @@ source: crates/oxc_linter/src/tester.rs
9595
╭─[prefer_default_parameters.tsx:2:2]
9696
1function abc(foo) {
9797
2foo = foo ?? 123;
98-
· ────────────────
98+
· ────────────────
9999
3 │ }
100100
╰────
101101
help: Replace the reassignment with a default parameter.
@@ -122,7 +122,7 @@ source: crates/oxc_linter/src/tester.rs
122122
╭─[prefer_default_parameters.tsx:3:3]
123123
2abc(foo) {
124124
3foo = foo || 123;
125-
· ────────────────
125+
· ────────────────
126126
4 │ }
127127
╰────
128128
help: Replace the reassignment with a default parameter.
@@ -131,7 +131,7 @@ source: crates/oxc_linter/src/tester.rs
131131
╭─[prefer_default_parameters.tsx:3:3]
132132
2abc(foo) {
133133
3foo = foo || 123;
134-
· ────────────────
134+
· ────────────────
135135
4 │ },
136136
╰────
137137
help: Replace the reassignment with a default parameter.
@@ -140,7 +140,7 @@ source: crates/oxc_linter/src/tester.rs
140140
╭─[prefer_default_parameters.tsx:3:3]
141141
2abc(foo) {
142142
3foo = foo || 123;
143-
· ────────────────
143+
· ────────────────
144144
4 │ }
145145
╰────
146146
help: Replace the reassignment with a default parameter.
@@ -149,37 +149,37 @@ source: crates/oxc_linter/src/tester.rs
149149
╭─[prefer_default_parameters.tsx:3:3]
150150
2abc(foo) {
151151
3foo = foo || 123;
152-
· ────────────────
152+
· ────────────────
153153
4 │ }
154154
╰────
155155
help: Replace the reassignment with a default parameter.
156156

157157
eslint-plugin-unicorn(prefer-default-parameters): Prefer default parameters over reassignment for 'foo'.
158158
╭─[prefer_default_parameters.tsx:1:21]
159159
1function abc(foo) { foo = foo || 'bar'; }
160-
· ──────────────────
160+
· ──────────────────
161161
╰────
162162
help: Replace the reassignment with a default parameter.
163163

164164
eslint-plugin-unicorn(prefer-default-parameters): Prefer default parameters over reassignment for 'foo'.
165165
╭─[prefer_default_parameters.tsx:1:21]
166166
1function abc(foo) { foo = foo || 'bar';}
167-
· ──────────────────
167+
· ──────────────────
168168
╰────
169169
help: Replace the reassignment with a default parameter.
170170

171171
eslint-plugin-unicorn(prefer-default-parameters): Prefer default parameters over reassignment for 'foo'.
172172
╭─[prefer_default_parameters.tsx:1:29]
173173
1const abc = function(foo) { foo = foo || 'bar';}
174-
· ──────────────────
174+
· ──────────────────
175175
╰────
176176
help: Replace the reassignment with a default parameter.
177177

178178
eslint-plugin-unicorn(prefer-default-parameters): Prefer default parameters over reassignment for 'foo'.
179179
╭─[prefer_default_parameters.tsx:2:2]
180180
1function abc(foo) {
181181
2foo = foo || 'bar'; bar(); baz();
182-
· ──────────────────
182+
· ──────────────────
183183
3 │ }
184184
╰────
185185
help: Replace the reassignment with a default parameter.
@@ -188,7 +188,7 @@ source: crates/oxc_linter/src/tester.rs
188188
╭─[prefer_default_parameters.tsx:2:2]
189189
1function abc(foo) {
190190
2foo = foo || 'bar';
191-
· ──────────────────
191+
· ──────────────────
192192
3function def(bar) {
193193
╰────
194194
help: Replace the reassignment with a default parameter.
@@ -197,7 +197,7 @@ source: crates/oxc_linter/src/tester.rs
197197
╭─[prefer_default_parameters.tsx:4:3]
198198
3function def(bar) {
199199
4bar = bar || 'foo';
200-
· ──────────────────
200+
· ──────────────────
201201
5 │ }
202202
╰────
203203
help: Replace the reassignment with a default parameter.
@@ -206,7 +206,7 @@ source: crates/oxc_linter/src/tester.rs
206206
╭─[prefer_default_parameters.tsx:4:3]
207207
3function def(bar) {
208208
4bar = bar || 'foo';
209-
· ──────────────────
209+
· ──────────────────
210210
5 │ }
211211
╰────
212212
help: Replace the reassignment with a default parameter.
@@ -224,7 +224,7 @@ source: crates/oxc_linter/src/tester.rs
224224
╭─[prefer_default_parameters.tsx:3:3]
225225
2abc(foo) {
226226
3foo = foo || 123;
227-
· ────────────────
227+
· ────────────────
228228
4 │ },
229229
╰────
230230
help: Replace the reassignment with a default parameter.
@@ -233,7 +233,7 @@ source: crates/oxc_linter/src/tester.rs
233233
╭─[prefer_default_parameters.tsx:6:3]
234234
5def(foo) {
235235
6foo = foo || 123;
236-
· ────────────────
236+
· ────────────────
237237
7 │ }
238238
╰────
239239
help: Replace the reassignment with a default parameter.
@@ -242,7 +242,7 @@ source: crates/oxc_linter/src/tester.rs
242242
╭─[prefer_default_parameters.tsx:3:3]
243243
2abc(foo) {
244244
3foo = foo || 123;
245-
· ────────────────
245+
· ────────────────
246246
4 │ }
247247
╰────
248248
help: Replace the reassignment with a default parameter.
@@ -251,7 +251,7 @@ source: crates/oxc_linter/src/tester.rs
251251
╭─[prefer_default_parameters.tsx:6:3]
252252
5def(foo) {
253253
6foo = foo || 123;
254-
· ────────────────
254+
· ────────────────
255255
7 │ }
256256
╰────
257257
help: Replace the reassignment with a default parameter.
@@ -260,7 +260,7 @@ source: crates/oxc_linter/src/tester.rs
260260
╭─[prefer_default_parameters.tsx:3:2]
261261
2const noSideEffects = 123;
262262
3foo = foo || 123;
263-
· ────────────────
263+
· ────────────────
264264
4 │ }
265265
╰────
266266
help: Replace the reassignment with a default parameter.
@@ -269,7 +269,7 @@ source: crates/oxc_linter/src/tester.rs
269269
╭─[prefer_default_parameters.tsx:5:2]
270270
4
271271
5foo = foo || 123;
272-
· ────────────────
272+
· ────────────────
273273
6console.log(foo);
274274
╰────
275275
help: Replace the reassignment with a default parameter.
@@ -278,7 +278,7 @@ source: crates/oxc_linter/src/tester.rs
278278
╭─[prefer_default_parameters.tsx:3:2]
279279
2const bar = function() {};
280280
3foo = foo || 123;
281-
· ────────────────
281+
· ────────────────
282282
4 │ }
283283
╰────
284284
help: Replace the reassignment with a default parameter.

0 commit comments

Comments
 (0)