Skip to content

Commit a5bbcfb

Browse files
committed
refactor(minifier): clean up remove_unused_expression
1 parent 2625bdf commit a5bbcfb

File tree

3 files changed

+30
-39
lines changed

3 files changed

+30
-39
lines changed

crates/oxc_ecmascript/src/side_effects/may_have_side_effects.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -529,20 +529,18 @@ fn get_array_minimum_length(arr: &ArrayExpression) -> usize {
529529
impl<'a> MayHaveSideEffects<'a> for CallExpression<'a> {
530530
fn may_have_side_effects(&self, ctx: &impl MayHaveSideEffectsContext<'a>) -> bool {
531531
if (self.pure && ctx.annotations()) || ctx.manual_pure_functions(&self.callee) {
532-
self.arguments.iter().any(|e| e.may_have_side_effects(ctx))
533-
} else {
534-
true
532+
return self.arguments.iter().any(|e| e.may_have_side_effects(ctx));
535533
}
534+
true
536535
}
537536
}
538537

539538
impl<'a> MayHaveSideEffects<'a> for NewExpression<'a> {
540539
fn may_have_side_effects(&self, ctx: &impl MayHaveSideEffectsContext<'a>) -> bool {
541540
if (self.pure && ctx.annotations()) || ctx.manual_pure_functions(&self.callee) {
542-
self.arguments.iter().any(|e| e.may_have_side_effects(ctx))
543-
} else {
544-
true
541+
return self.arguments.iter().any(|e| e.may_have_side_effects(ctx));
545542
}
543+
true
546544
}
547545
}
548546

crates/oxc_minifier/src/peephole/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ impl<'a> Traverse<'a, MinifierState<'a>> for PeepholeOptimizations {
255255
Self::try_compress_normal_assignment_to_combined_logical_assignment(e, ctx);
256256
Self::try_compress_normal_assignment_to_combined_assignment(e, ctx);
257257
Self::try_compress_assignment_to_update_expression(expr, ctx);
258-
Self::remove_unused_assignment_expression(expr, ctx);
258+
Self::remove_unused_assignment_expr(expr, ctx);
259259
}
260260
Expression::SequenceExpression(_) => Self::try_fold_sequence_expression(expr, ctx),
261261
Expression::ArrowFunctionExpression(e) => Self::try_compress_arrow_expression(e, ctx),
@@ -497,7 +497,7 @@ impl<'a> Traverse<'a, MinifierState<'a>> for DeadCodeElimination {
497497
PeepholeOptimizations::try_fold_sequence_expression(e, ctx);
498498
}
499499
Expression::AssignmentExpression(_) => {
500-
PeepholeOptimizations::remove_unused_assignment_expression(e, ctx);
500+
PeepholeOptimizations::remove_unused_assignment_expr(e, ctx);
501501
}
502502
_ => {}
503503
}

crates/oxc_minifier/src/peephole/remove_unused_expression.rs

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,23 @@ impl<'a> PeepholeOptimizations {
1717
/// `SimplifyUnusedExpr`: <https://github.com/evanw/esbuild/blob/v0.24.2/internal/js_ast/js_ast_helpers.go#L534>
1818
pub fn remove_unused_expression(e: &mut Expression<'a>, ctx: &mut Ctx<'a, '_>) -> bool {
1919
match e {
20-
Expression::ArrayExpression(_) => Self::fold_array_expression(e, ctx),
21-
Expression::UnaryExpression(_) => Self::fold_unary_expression(e, ctx),
22-
Expression::NewExpression(_) => Self::fold_new_constructor(e, ctx),
23-
Expression::LogicalExpression(_) => Self::fold_logical_expression(e, ctx),
24-
Expression::SequenceExpression(_) => Self::fold_sequence_expression(e, ctx),
25-
Expression::TemplateLiteral(_) => Self::fold_template_literal(e, ctx),
26-
Expression::ObjectExpression(_) => Self::fold_object_expression(e, ctx),
27-
Expression::ConditionalExpression(_) => Self::fold_conditional_expression(e, ctx),
28-
Expression::BinaryExpression(_) => Self::fold_binary_expression(e, ctx),
29-
Expression::CallExpression(_) => Self::remove_unused_call_expression(e, ctx),
30-
Expression::AssignmentExpression(_) => {
31-
Self::remove_unused_assignment_expression(e, ctx)
32-
}
33-
Expression::ClassExpression(_) => Self::remove_unused_class_expression(e, ctx),
20+
Expression::ArrayExpression(_) => Self::remove_unused_array_expr(e, ctx),
21+
Expression::AssignmentExpression(_) => Self::remove_unused_assignment_expr(e, ctx),
22+
Expression::BinaryExpression(_) => Self::remove_unused_binary_expr(e, ctx),
23+
Expression::CallExpression(_) => Self::remove_unused_call_expr(e, ctx),
24+
Expression::ClassExpression(_) => Self::remove_unused_class_expr(e, ctx),
25+
Expression::ConditionalExpression(_) => Self::remove_unused_conditional_expr(e, ctx),
26+
Expression::LogicalExpression(_) => Self::remove_unused_logical_expr(e, ctx),
27+
Expression::NewExpression(_) => Self::remove_unused_new_expr(e, ctx),
28+
Expression::ObjectExpression(_) => Self::remove_unused_object_expr(e, ctx),
29+
Expression::SequenceExpression(_) => Self::remove_unused_sequence_expr(e, ctx),
30+
Expression::TemplateLiteral(_) => Self::remove_unused_template_literal(e, ctx),
31+
Expression::UnaryExpression(_) => Self::remove_unused_unary_expr(e, ctx),
3432
_ => !e.may_have_side_effects(ctx),
3533
}
3634
}
3735

38-
fn fold_unary_expression(e: &mut Expression<'a>, ctx: &mut Ctx<'a, '_>) -> bool {
36+
fn remove_unused_unary_expr(e: &mut Expression<'a>, ctx: &mut Ctx<'a, '_>) -> bool {
3937
let Expression::UnaryExpression(unary_expr) = e else { return false };
4038
match unary_expr.operator {
4139
UnaryOperator::Void | UnaryOperator::LogicalNot => {
@@ -56,19 +54,17 @@ impl<'a> PeepholeOptimizations {
5654
}
5755
}
5856

59-
fn fold_sequence_expression(e: &mut Expression<'a>, ctx: &mut Ctx<'a, '_>) -> bool {
57+
fn remove_unused_sequence_expr(e: &mut Expression<'a>, ctx: &mut Ctx<'a, '_>) -> bool {
6058
let Expression::SequenceExpression(sequence_expr) = e else { return false };
61-
6259
let old_len = sequence_expr.expressions.len();
6360
sequence_expr.expressions.retain_mut(|e| !Self::remove_unused_expression(e, ctx));
6461
if sequence_expr.expressions.len() != old_len {
6562
ctx.state.changed = true;
6663
}
67-
6864
sequence_expr.expressions.is_empty()
6965
}
7066

71-
fn fold_logical_expression(e: &mut Expression<'a>, ctx: &mut Ctx<'a, '_>) -> bool {
67+
fn remove_unused_logical_expr(e: &mut Expression<'a>, ctx: &mut Ctx<'a, '_>) -> bool {
7268
let Expression::LogicalExpression(logical_expr) = e else { return false };
7369
if !logical_expr.operator.is_coalesce() {
7470
Self::try_fold_expr_in_boolean_context(&mut logical_expr.left, ctx);
@@ -166,7 +162,7 @@ impl<'a> PeepholeOptimizations {
166162
}
167163

168164
// `([1,2,3, foo()])` -> `foo()`
169-
fn fold_array_expression(e: &mut Expression<'a>, ctx: &mut Ctx<'a, '_>) -> bool {
165+
fn remove_unused_array_expr(e: &mut Expression<'a>, ctx: &mut Ctx<'a, '_>) -> bool {
170166
let Expression::ArrayExpression(array_expr) = e else {
171167
return false;
172168
};
@@ -216,7 +212,7 @@ impl<'a> PeepholeOptimizations {
216212
false
217213
}
218214

219-
fn fold_new_constructor(e: &mut Expression<'a>, ctx: &mut Ctx<'a, '_>) -> bool {
215+
fn remove_unused_new_expr(e: &mut Expression<'a>, ctx: &mut Ctx<'a, '_>) -> bool {
220216
let Expression::NewExpression(new_expr) = e else { return false };
221217
if new_expr.pure && ctx.annotations() {
222218
let mut exprs =
@@ -236,7 +232,7 @@ impl<'a> PeepholeOptimizations {
236232
}
237233

238234
// "`${1}2${foo()}3`" -> "`${foo()}`"
239-
fn fold_template_literal(e: &mut Expression<'a>, ctx: &mut Ctx<'a, '_>) -> bool {
235+
fn remove_unused_template_literal(e: &mut Expression<'a>, ctx: &mut Ctx<'a, '_>) -> bool {
240236
let Expression::TemplateLiteral(temp_lit) = e else { return false };
241237
if temp_lit.expressions.is_empty() {
242238
return true;
@@ -316,7 +312,7 @@ impl<'a> PeepholeOptimizations {
316312
}
317313

318314
// `({ 1: 1, [foo()]: bar() })` -> `foo(), bar()`
319-
fn fold_object_expression(e: &mut Expression<'a>, ctx: &mut Ctx<'a, '_>) -> bool {
315+
fn remove_unused_object_expr(e: &mut Expression<'a>, ctx: &mut Ctx<'a, '_>) -> bool {
320316
let Expression::ObjectExpression(object_expr) = e else {
321317
return false;
322318
};
@@ -381,7 +377,7 @@ impl<'a> PeepholeOptimizations {
381377
false
382378
}
383379

384-
fn fold_conditional_expression(e: &mut Expression<'a>, ctx: &mut Ctx<'a, '_>) -> bool {
380+
fn remove_unused_conditional_expr(e: &mut Expression<'a>, ctx: &mut Ctx<'a, '_>) -> bool {
385381
let Expression::ConditionalExpression(conditional_expr) = e else {
386382
return false;
387383
};
@@ -429,7 +425,7 @@ impl<'a> PeepholeOptimizations {
429425
false
430426
}
431427

432-
fn fold_binary_expression(e: &mut Expression<'a>, ctx: &mut Ctx<'a, '_>) -> bool {
428+
fn remove_unused_binary_expr(e: &mut Expression<'a>, ctx: &mut Ctx<'a, '_>) -> bool {
433429
let Expression::BinaryExpression(binary_expr) = e else {
434430
return false;
435431
};
@@ -522,7 +518,7 @@ impl<'a> PeepholeOptimizations {
522518
false
523519
}
524520

525-
fn remove_unused_call_expression(e: &mut Expression<'a>, ctx: &mut Ctx<'a, '_>) -> bool {
521+
fn remove_unused_call_expr(e: &mut Expression<'a>, ctx: &mut Ctx<'a, '_>) -> bool {
526522
let Expression::CallExpression(call_expr) = e else { return false };
527523

528524
if call_expr.pure && ctx.annotations() {
@@ -601,10 +597,7 @@ impl<'a> PeepholeOptimizations {
601597
}))
602598
}
603599

604-
pub fn remove_unused_assignment_expression(
605-
e: &mut Expression<'a>,
606-
ctx: &mut Ctx<'a, '_>,
607-
) -> bool {
600+
pub fn remove_unused_assignment_expr(e: &mut Expression<'a>, ctx: &mut Ctx<'a, '_>) -> bool {
608601
let Expression::AssignmentExpression(assign_expr) = e else { return false };
609602
if matches!(
610603
ctx.state.options.unused,
@@ -646,7 +639,7 @@ impl<'a> PeepholeOptimizations {
646639
false
647640
}
648641

649-
fn remove_unused_class_expression(e: &mut Expression<'a>, ctx: &mut Ctx<'a, '_>) -> bool {
642+
fn remove_unused_class_expr(e: &mut Expression<'a>, ctx: &mut Ctx<'a, '_>) -> bool {
650643
let Expression::ClassExpression(c) = e else { return false };
651644
if let Some(exprs) = Self::remove_unused_class(c, ctx) {
652645
if exprs.is_empty() {

0 commit comments

Comments
 (0)