1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
use crate::generated::ancestor::Ancestor;
use oxc_allocator::{ArenaVec, TakeIn};
use oxc_ast::ast::*;
use oxc_ast_visit::Visit;
use oxc_ecmascript::{
constant_evaluation::{ConstantEvaluation, ConstantValue},
side_effects::MayHaveSideEffects,
};
use oxc_span::GetSpan;
use crate::{TraverseCtx, keep_var::KeepVar};
use super::PeepholeOptimizations;
/// Remove Dead Code from the AST.
///
/// Terser option: `dead_code: true`.
///
/// See `KeepVar` at the end of this file for `var` hoisting logic.
/// <https://github.com/google/closure-compiler/blob/v20240609/src/com/google/javascript/jscomp/PeepholeRemoveDeadCode.java>
impl<'a> PeepholeOptimizations {
/// Remove block from single line blocks
/// `{ block } -> block`
pub fn try_optimize_block(stmt: &mut Statement<'a>, ctx: &mut TraverseCtx<'a>) {
let Statement::BlockStatement(s) = stmt else { return };
match s.body.len() {
0 => {
let parent = ctx.parent();
if parent.is_while_statement()
|| parent.is_do_while_statement()
|| parent.is_for_statement()
|| parent.is_for_in_statement()
|| parent.is_for_of_statement()
|| parent.is_block_statement()
|| parent.is_program()
{
// Remove the block if it is empty and the parent is a block statement.
let new_stmt = Statement::new_empty_statement(s.span, ctx);
ctx.replace_statement(stmt, new_stmt);
}
}
1 => {
let first = &s.body[0];
if matches!(first, Statement::VariableDeclaration(decl) if !decl.kind.is_var())
|| matches!(first, Statement::ClassDeclaration(_))
|| matches!(first, Statement::FunctionDeclaration(_))
{
return;
}
let new_stmt = s.body.remove(0);
ctx.replace_statement(stmt, new_stmt);
}
_ => {}
}
}
#[expect(clippy::float_cmp)]
pub fn try_fold_if(stmt: &mut Statement<'a>, ctx: &mut TraverseCtx<'a>) {
let Statement::IfStatement(if_stmt) = stmt else { return };
// Descend and remove `else` blocks first.
match &mut if_stmt.alternate {
Some(Statement::IfStatement(_)) => {
Self::try_fold_if(if_stmt.alternate.as_mut().unwrap(), ctx);
}
Some(Statement::BlockStatement(s)) if s.body.is_empty() => {
if_stmt.alternate = None;
}
Some(Statement::EmptyStatement(_)) => {
if_stmt.alternate = None;
}
_ => {}
}
if let Some(boolean) = if_stmt.test.evaluate_value_to_boolean(ctx) {
let test_has_side_effects = if_stmt.test.may_have_side_effects(ctx);
// Use "1" and "0" instead of "true" and "false" to be shorter.
// And also prevent swapping consequent and alternate when `!0` is encountered.
//
// Idempotency: skip the rewrite when `if_stmt.test` is already the canonical
// numeric form (NumericLiteral 1.0 / 0.0). Without this gate, the typed
// `replace_expression` helper would re-record a mutation on every loop
// iteration (NumericLiteral 1.0 still evaluates to Some(true) via the line-73
// predicate), preventing fixed-point convergence.
if !test_has_side_effects
&& !matches!(
&if_stmt.test,
Expression::NumericLiteral(n)
if (boolean && n.value == 1.0) || (!boolean && n.value == 0.0)
)
{
let new_test = Expression::new_numeric_literal(
if_stmt.test.span(),
if boolean { 1.0 } else { 0.0 },
None,
NumberBase::Decimal,
ctx,
);
ctx.replace_expression(&mut if_stmt.test, new_test);
}
let mut keep_var = KeepVar::new();
if boolean {
if let Some(alternate) = &if_stmt.alternate {
keep_var.visit_statement(alternate);
}
} else {
keep_var.visit_statement(&if_stmt.consequent);
}
let var_stmt = keep_var
.get_variable_declaration_statement(&ctx.ast)
.and_then(|stmt| Self::remove_unused_variable_declaration(stmt, ctx));
let has_var_stmt = var_stmt.is_some();
if let Some(var_stmt) = var_stmt {
// Idempotency: skip when the target slot is already in canonical KeepVar
// output shape (a `var` declaration whose declarators all lack initializers).
// On the next loop iteration `KeepVar` would re-extract the same names from
// that slot and produce a structurally-equivalent fresh allocation; routing
// through `replace_statement` would re-record a mutation indefinitely.
if boolean {
let already_canonical =
if_stmt.alternate.as_ref().is_some_and(Self::is_keep_var_canonical);
if !already_canonical {
if let Some(alternate) = if_stmt.alternate.as_mut() {
ctx.replace_statement(alternate, var_stmt);
} else {
// `KeepVar` only produced a stmt because it visited the alternate,
// so the alternate must be Some. Defensive fall-through preserves
// historical behaviour: install it without dropping anything.
if_stmt.alternate = Some(var_stmt);
ctx.notice_change();
}
}
} else if !Self::is_keep_var_canonical(&if_stmt.consequent) {
ctx.replace_statement(&mut if_stmt.consequent, var_stmt);
}
return;
}
if test_has_side_effects {
if !has_var_stmt {
if boolean {
// Idempotent: `Option::take` only fires when the slot still holds a value.
if let Some(old) = if_stmt.alternate.take() {
ctx.drop_statement(&old);
}
} else if !matches!(&if_stmt.consequent, Statement::EmptyStatement(_)) {
// Idempotency: skip when consequent is already empty.
let new_consequent =
Statement::new_empty_statement(if_stmt.consequent.span(), ctx);
ctx.replace_statement(&mut if_stmt.consequent, new_consequent);
}
}
return;
}
let new_stmt = if boolean {
if_stmt.consequent.take_in(ctx)
} else if let Some(alternate) = if_stmt.alternate.take() {
alternate
} else {
Statement::new_empty_statement(if_stmt.span, ctx)
};
ctx.replace_statement(stmt, new_stmt);
}
}
/// True when `stmt` is already in the canonical shape produced by
/// `KeepVar::get_variable_declaration_statement`: a single `var` declaration
/// whose declarators all lack initializers. Used as an idempotency gate in
/// `try_fold_if` so the var-hoisting rewrite doesn't re-fire across loop
/// iterations when `KeepVar` would just re-emit the same shape.
fn is_keep_var_canonical(stmt: &Statement<'a>) -> bool {
matches!(
stmt,
Statement::VariableDeclaration(decl)
if decl.kind.is_var() && decl.declarations.iter().all(|d| d.init.is_none())
)
}
pub fn try_fold_for(stmt: &mut Statement<'a>, ctx: &mut TraverseCtx<'a>) {
let Statement::ForStatement(for_stmt) = stmt else { return };
if let Some(init) = &mut for_stmt.init
&& let Some(init_expr) = init.as_expression_mut()
&& Self::remove_unused_expression(init_expr, ctx)
{
ctx.drop_expression(init_expr);
for_stmt.init = None;
}
if let Some(update) = &mut for_stmt.update
&& Self::remove_unused_expression(update, ctx)
{
ctx.drop_expression(update);
for_stmt.update = None;
}
let test_boolean =
for_stmt.test.as_ref().and_then(|test| test.evaluate_value_to_boolean(ctx));
if for_stmt.test.as_ref().is_some_and(|test| test.may_have_side_effects(ctx)) {
return;
}
match test_boolean {
Some(false) => match &for_stmt.init {
Some(ForStatementInit::VariableDeclaration(_)) => {
let mut keep_var = KeepVar::new();
keep_var.visit_statement(&for_stmt.body);
let mut var_decl = keep_var.get_variable_declaration(&ctx.ast);
let Some(ForStatementInit::VariableDeclaration(var_init)) = &mut for_stmt.init
else {
return;
};
if var_init.kind.is_var() {
if let Some(var_decl) = &mut var_decl {
var_decl.declarations.splice(0..0, var_init.declarations.take_in(ctx));
} else {
var_decl = Some(var_init.take_in_box(ctx));
}
}
let new_stmt = var_decl.map_or_else(
|| Statement::new_empty_statement(for_stmt.span, ctx),
Statement::VariableDeclaration,
);
ctx.replace_statement(stmt, new_stmt);
}
None => {
let mut keep_var = KeepVar::new();
keep_var.visit_statement(&for_stmt.body);
let new_stmt = keep_var.get_variable_declaration(&ctx.ast).map_or_else(
|| Statement::new_empty_statement(for_stmt.span, ctx),
Statement::VariableDeclaration,
);
ctx.replace_statement(stmt, new_stmt);
}
_ => {}
},
Some(true) => {
// Remove the test expression.
if let Some(old) = for_stmt.test.take() {
ctx.drop_expression(&old);
}
}
None => {}
}
}
/// Remove meaningless labeled statements.
///
/// ```js
/// a: break a;
/// ```
pub fn try_fold_labeled(stmt: &mut Statement<'a>, ctx: &mut TraverseCtx<'a>) {
let Statement::LabeledStatement(s) = stmt else { return };
let id = s.label.name.as_str();
if ctx.options().drop_labels.contains(id) {
let new_stmt = Statement::new_empty_statement(s.span, ctx);
ctx.replace_statement(stmt, new_stmt);
return;
}
// Check the first statement in the block, or just the `break [id] ` statement.
// Check if we need to remove the whole block.
match &mut s.body {
Statement::BreakStatement(break_stmt)
if break_stmt.label.as_ref().is_some_and(|l| l.name.as_str() == id) => {}
Statement::BlockStatement(block) if block.body.first().is_some_and(|first| matches!(first, Statement::BreakStatement(break_stmt) if break_stmt.label.as_ref().is_some_and(|l| l.name.as_str() == id))) => {}
Statement::EmptyStatement(_) => {
let new_stmt = Statement::new_empty_statement(s.span, ctx);
ctx.replace_statement(stmt, new_stmt);
return;
}
_ => return
}
let mut var = KeepVar::new();
var.visit_statement(&s.body);
let var_decl = var.get_variable_declaration_statement(&ctx.ast);
let new_stmt = var_decl.unwrap_or_else(|| Statement::new_empty_statement(s.span, ctx));
ctx.replace_statement(stmt, new_stmt);
}
pub fn try_fold_expression_stmt(stmt: &mut Statement<'a>, ctx: &mut TraverseCtx<'a>) {
let Statement::ExpressionStatement(expr_stmt) = stmt else { return };
// We need to check if it is in arrow function with `expression: true`.
// This is the only scenario where we can't remove it even if `ExpressionStatement`.
if let Ancestor::ArrowFunctionExpressionBody(body) = ctx.ancestry.ancestor(1)
&& *body.expression()
{
return;
}
if Self::remove_unused_expression(&mut expr_stmt.expression, ctx) {
let new_stmt = Statement::new_empty_statement(expr_stmt.span, ctx);
ctx.replace_statement(stmt, new_stmt);
}
}
pub fn try_fold_try(stmt: &mut Statement<'a>, ctx: &mut TraverseCtx<'a>) {
let Statement::TryStatement(s) = stmt else { return };
if let Some(handler) = &s.handler
&& s.block.body.is_empty()
{
let body = &handler.body.body;
let is_canonical_body =
body.is_empty() || (body.len() == 1 && Self::is_keep_var_canonical(&body[0]));
if !is_canonical_body {
let mut var = KeepVar::new();
var.visit_block_statement(&handler.body);
let Some(handler) = &mut s.handler else { return };
for dropped in handler.body.body.take_in(ctx) {
ctx.drop_statement(&dropped);
}
if let Some(var_decl) = var.get_variable_declaration_statement(&ctx.ast) {
handler.body.body.push(var_decl);
}
}
}
if let Some(finalizer) = &s.finalizer
&& finalizer.body.is_empty()
&& s.handler.is_some()
{
s.finalizer = None;
}
if s.block.body.is_empty()
&& s.handler.as_ref().is_none_or(|handler| handler.body.body.is_empty())
{
let new_stmt = if let Some(finalizer) = &mut s.finalizer {
let mut block = BlockStatement::boxed(finalizer.span, ArenaVec::new_in(ctx), ctx);
std::mem::swap(finalizer, &mut block);
Statement::BlockStatement(block)
} else {
Statement::new_empty_statement(s.span, ctx)
};
ctx.replace_statement(stmt, new_stmt);
}
}
/// Try folding conditional expression (?:) if the condition results of the condition is known.
pub fn try_fold_conditional_expression(expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
let Expression::ConditionalExpression(e) = expr else { return };
let Some(v) = e.test.evaluate_value_to_boolean(ctx) else { return };
let new_expr = if e.test.may_have_side_effects(ctx) {
// "(a, true) ? b : c" => "a, b"
let exprs = ArenaVec::from_array_in(
[
{
let mut test = e.test.take_in(ctx);
Self::remove_unused_expression(&mut test, ctx);
test
},
if v { e.consequent.take_in(ctx) } else { e.alternate.take_in(ctx) },
],
ctx,
);
Expression::new_sequence_expression(e.span, exprs, ctx)
} else {
let result_expr = if v { e.consequent.take_in(ctx) } else { e.alternate.take_in(ctx) };
let should_keep_as_sequence_expr = Self::should_keep_indirect_access(&result_expr, ctx);
// "(1 ? a.b : 0)()" => "(0, a.b)()"
if should_keep_as_sequence_expr {
Expression::new_sequence_expression(
e.span,
ArenaVec::from_array_in(
[
Expression::new_numeric_literal(
e.span,
0.0,
None,
NumberBase::Decimal,
ctx,
),
result_expr,
],
ctx,
),
ctx,
)
} else {
result_expr
}
};
ctx.replace_expression(expr, new_expr);
}
pub fn remove_sequence_expression(expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
let Expression::SequenceExpression(e) = expr else { return };
let should_keep_as_sequence_expr = e
.expressions
.last()
.is_some_and(|last_expr| Self::should_keep_indirect_access(last_expr, ctx));
if should_keep_as_sequence_expr
&& e.expressions.len() == 2
&& e.expressions.first().unwrap().is_number_0()
{
return;
}
let old_len = e.expressions.len();
let mut i = 0;
e.expressions.retain_mut(|e| {
i += 1;
if should_keep_as_sequence_expr && i == old_len - 1 {
// Idempotency: skip the rewrite when `e` is already the canonical
// `0` placeholder. Without this gate the re-wrap re-bumps the
// mutation counter every iteration (mirrors the `len == 2` guard
// above), spinning the fixed-point loop. `0` is side-effect-free,
// so `remove_unused_expression` would return `true` and produce a
// structurally-identical fresh `0`.
if !e.is_number_0() && Self::remove_unused_expression(e, ctx) {
let new_expr = Expression::new_numeric_literal(
e.span(),
0.0,
None,
NumberBase::Decimal,
ctx,
);
ctx.replace_expression(e, new_expr);
}
return true;
}
if i == old_len {
return true;
}
if Self::remove_unused_expression(e, ctx) {
ctx.drop_expression(e);
false
} else {
true
}
});
if e.expressions.len() == 1 {
let new_expr = e.expressions.pop().unwrap();
ctx.replace_expression(expr, new_expr);
}
}
pub fn keep_track_of_pure_functions(stmt: &mut Statement<'a>, ctx: &mut TraverseCtx<'a>) {
match stmt {
Statement::FunctionDeclaration(f) => {
if let Some(body) = &f.body {
Self::try_save_pure_function(
f.id.as_ref(),
&f.params,
body,
f.r#async,
f.generator,
ctx,
);
}
}
Statement::VariableDeclaration(decl) => {
for d in &decl.declarations {
if let BindingPattern::BindingIdentifier(id) = &d.id {
match &d.init {
Some(Expression::ArrowFunctionExpression(a)) => {
Self::try_save_pure_function(
Some(id),
&a.params,
&a.body,
a.r#async,
false,
ctx,
);
}
Some(Expression::FunctionExpression(f)) => {
if let Some(body) = &f.body {
Self::try_save_pure_function(
Some(id),
&f.params,
body,
f.r#async,
f.generator,
ctx,
);
}
}
_ => {}
}
}
}
}
_ => {}
}
}
fn try_save_pure_function(
id: Option<&BindingIdentifier<'a>>,
params: &FormalParameters<'a>,
body: &FunctionBody<'a>,
r#async: bool,
generator: bool,
ctx: &mut TraverseCtx<'a>,
) {
if r#async || generator {
return;
}
// `function foo({}) {} foo(null)` is runtime type error.
if !params.items.iter().all(|pat| pat.pattern.is_binding_identifier()) {
return;
}
if body.statements.iter().any(|stmt| stmt.may_have_side_effects(ctx)) {
return;
}
let Some(symbol_id) = id.and_then(|id| id.symbol_id.get()) else { return };
if ctx.scoping().get_resolved_references(symbol_id).all(|r| r.flags().is_read_only()) {
ctx.state.pure_functions.insert(
symbol_id,
if body.is_empty() { Some(ConstantValue::Undefined) } else { None },
);
}
}
pub fn remove_dead_code_call_expression(expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
let Expression::CallExpression(e) = expr else { return };
if let Expression::Identifier(ident) = &e.callee {
let reference_id = ident.reference_id();
if let Some(symbol_id) = ctx.scoping().get_reference(reference_id).symbol_id()
&& matches!(
ctx.state.pure_functions.get(&symbol_id),
Some(Some(ConstantValue::Undefined))
)
{
let mut exprs = Self::fold_arguments_into_needed_expressions(&mut e.arguments, ctx);
if exprs.is_empty() {
let new_expr = Expression::new_void_0(e.span, ctx);
ctx.replace_expression(expr, new_expr);
return;
}
exprs.push(Expression::new_void_0(e.span, ctx));
let new_expr = Expression::new_sequence_expression(e.span, exprs, ctx);
ctx.replace_expression(expr, new_expr);
}
}
}
/// Whether the indirect access should be kept.
/// For example, `(0, foo.bar)()` should not be transformed to `foo.bar()`.
/// Example case: `let o = { f() { assert.ok(this !== o); } }; (true && o.f)(); (true && o.f)``;`
///
/// * `access_value` - The expression that may need to be kept as indirect reference (`foo.bar` in the example above)
pub fn should_keep_indirect_access(
access_value: &Expression<'a>,
ctx: &TraverseCtx<'a>,
) -> bool {
match ctx.parent() {
Ancestor::CallExpressionCallee(_) | Ancestor::TaggedTemplateExpressionTag(_) => {
match access_value {
Expression::Identifier(id) => id.name == "eval" && ctx.is_global_reference(id),
match_member_expression!(Expression) => true,
_ => false,
}
}
Ancestor::UnaryExpressionArgument(unary) => match unary.operator() {
UnaryOperator::Typeof => {
// Example case: `typeof (0, foo)` (error) -> `typeof foo` (no error)
if let Expression::Identifier(id) = access_value {
ctx.is_global_reference(id)
} else {
false
}
}
UnaryOperator::Delete => {
match access_value {
// Example case: `delete (0, foo)` (no error) -> `delete foo` (error)
Expression::Identifier(_)
// Example case: `delete (0, foo.#a)` (no error) -> `delete foo.#a` (error)
| Expression::PrivateFieldExpression(_)
// Example case: `typeof (0, foo.bar)` (noop) -> `typeof foo.bar` (deletes bar)
| Expression::ComputedMemberExpression(_)
| Expression::StaticMemberExpression(_) => true,
// Example case: `typeof (0, foo?.bar)` (noop) -> `typeof foo?.bar` (deletes bar)
Expression::ChainExpression(chain) => {
matches!(&chain.expression, match_member_expression!(ChainElement))
}
_ => false,
}
}
_ => false,
},
_ => false,
}
}
/// Wrap `expr` as `(0, expr)` so an access that
/// [`Self::should_keep_indirect_access`] flagged stays indirect.
///
/// The shape is load-bearing: `remove_sequence_expression`'s idempotency
/// gate recognizes exactly a 2-element sequence whose head `is_number_0`.
/// Sibling fold sites in `fold_constants.rs` / `try_fold_conditional_expression`
/// still build it inline; migrating them here is welcome.
pub fn preserve_indirect_access(
span: Span,
expr: Expression<'a>,
ctx: &TraverseCtx<'a>,
) -> Expression<'a> {
Expression::new_sequence_expression(
span,
ArenaVec::from_array_in(
[Expression::new_numeric_literal(span, 0.0, None, NumberBase::Decimal, ctx), expr],
ctx,
),
ctx,
)
}
pub fn remove_dead_code_exit_class_body(body: &mut ClassBody<'a>, _ctx: &mut TraverseCtx<'a>) {
body.body.retain(|e| !matches!(e, ClassElement::StaticBlock(s) if s.body.is_empty()));
}
pub fn remove_empty_spread_arguments(args: &mut ArenaVec<'a, Argument<'a>>) {
if args.len() != 1 {
return;
}
let Argument::SpreadElement(e) = &args[0] else { return };
let Expression::ArrayExpression(e) = &e.argument else { return };
if e.elements.is_empty() {
args.drain(..);
}
}
}