Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion crates/oxc_minifier/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ oxc_allocator = { workspace = true }
oxc_ast = { workspace = true }
oxc_ast_visit = { workspace = true }
oxc_codegen = { workspace = true }
oxc_data_structures = { workspace = true, features = ["stack"] }
oxc_ecmascript = { workspace = true }
oxc_mangler = { workspace = true }
oxc_parser = { workspace = true }
Expand Down
28 changes: 9 additions & 19 deletions crates/oxc_minifier/src/peephole/fold_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,18 @@ use oxc_traverse::Ancestor;

use crate::ctx::Ctx;

use super::{PeepholeOptimizations, State};
use super::PeepholeOptimizations;

impl<'a> PeepholeOptimizations {
/// Constant Folding
///
/// <https://github.com/google/closure-compiler/blob/v20240609/src/com/google/javascript/jscomp/PeepholeFoldConstants.java>
pub fn fold_constants_exit_expression(
&self,
expr: &mut Expression<'a>,
state: &mut State,
ctx: &mut Ctx<'a, '_>,
) {
pub fn fold_constants_exit_expression(&self, expr: &mut Expression<'a>, ctx: &mut Ctx<'a, '_>) {
match expr {
Expression::TemplateLiteral(t) => {
self.try_inline_values_in_template_literal(t, state, ctx);
self.try_inline_values_in_template_literal(t, ctx);
}
Expression::ObjectExpression(e) => self.fold_object_spread(e, state, ctx),
Expression::ObjectExpression(e) => self.fold_object_spread(e, ctx),
_ => {}
}

Expand All @@ -43,7 +38,7 @@ impl<'a> PeepholeOptimizations {
_ => None,
} {
*expr = folded_expr;
state.changed = true;
ctx.state.changed = true;
}

// Save `const value = false` into constant values.
Expand Down Expand Up @@ -664,12 +659,7 @@ impl<'a> PeepholeOptimizations {
None
}

fn fold_object_spread(
&self,
e: &mut ObjectExpression<'a>,
state: &mut State,
ctx: &mut Ctx<'a, '_>,
) {
fn fold_object_spread(&self, e: &mut ObjectExpression<'a>, ctx: &mut Ctx<'a, '_>) {
let (new_size, should_fold) =
e.properties.iter().fold((0, false), |(new_size, should_fold), p| {
let ObjectPropertyKind::SpreadProperty(spread_element) = p else {
Expand Down Expand Up @@ -745,7 +735,7 @@ impl<'a> PeepholeOptimizations {
}

e.properties = new_properties;
state.changed = true;
ctx.state.changed = true;
}

fn is_spread_inlineable_object_literal(
Expand Down Expand Up @@ -774,7 +764,7 @@ impl<'a> PeepholeOptimizations {
fn try_inline_values_in_template_literal(
&self,
t: &mut TemplateLiteral<'a>,
state: &mut State,

ctx: &mut Ctx<'a, '_>,
) {
let has_expr_to_inline = t
Expand Down Expand Up @@ -824,7 +814,7 @@ impl<'a> PeepholeOptimizations {
}
}

state.changed = true;
ctx.state.changed = true;
}
}

Expand Down
5 changes: 2 additions & 3 deletions crates/oxc_minifier/src/peephole/minimize_conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use oxc_syntax::es_target::ESTarget;

use crate::ctx::Ctx;

use super::{PeepholeOptimizations, State};
use super::PeepholeOptimizations;

/// Minimize Conditions
///
Expand All @@ -15,7 +15,6 @@ impl<'a> PeepholeOptimizations {
pub fn minimize_conditions_exit_expression(
&self,
expr: &mut Expression<'a>,
state: &mut State,
ctx: &mut Ctx<'a, '_>,
) {
let mut changed = false;
Expand Down Expand Up @@ -57,7 +56,7 @@ impl<'a> PeepholeOptimizations {
}
}
if changed {
state.changed = true;
ctx.state.changed = true;
}
}

Expand Down
13 changes: 4 additions & 9 deletions crates/oxc_minifier/src/peephole/minimize_for_statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,11 @@ use oxc_span::GetSpan;

use crate::ctx::Ctx;

use super::{PeepholeOptimizations, State};
use super::PeepholeOptimizations;

impl<'a> PeepholeOptimizations {
/// `mangleFor`: <https://github.com/evanw/esbuild/blob/v0.24.2/internal/js_ast/js_parser.go#L9801>
pub fn minimize_for_statement(
&self,
for_stmt: &mut ForStatement<'a>,
state: &mut State,
ctx: &mut Ctx<'a, '_>,
) {
pub fn minimize_for_statement(&self, for_stmt: &mut ForStatement<'a>, ctx: &mut Ctx<'a, '_>) {
// Get the first statement in the loop
let mut first = &for_stmt.body;
if let Statement::BlockStatement(block_stmt) = first {
Expand Down Expand Up @@ -66,7 +61,7 @@ impl<'a> PeepholeOptimizations {

let alternate = if_stmt.alternate.take();
for_stmt.body = Self::drop_first_statement(span, body, alternate, ctx);
state.changed = true;
ctx.state.changed = true;
return;
}
// "for (;;) if (x) y(); else break;" => "for (; x;) y();"
Expand Down Expand Up @@ -103,7 +98,7 @@ impl<'a> PeepholeOptimizations {

let consequent = if_stmt.consequent.take_in(ctx.ast);
for_stmt.body = Self::drop_first_statement(span, body, Some(consequent), ctx);
state.changed = true;
ctx.state.changed = true;
}
}

Expand Down
28 changes: 11 additions & 17 deletions crates/oxc_minifier/src/peephole/minimize_if_statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@ use oxc_span::GetSpan;

use crate::ctx::Ctx;

use super::{PeepholeOptimizations, State};
use super::PeepholeOptimizations;

impl<'a> PeepholeOptimizations {
/// `MangleIf`: <https://github.com/evanw/esbuild/blob/v0.24.2/internal/js_parser/js_parser.go#L9860>
pub fn try_minimize_if(
&self,
if_stmt: &mut IfStatement<'a>,
state: &mut State,
ctx: &mut Ctx<'a, '_>,
) -> Option<Statement<'a>> {
self.wrap_to_avoid_ambiguous_else(if_stmt, state, ctx);
self.wrap_to_avoid_ambiguous_else(if_stmt, ctx);
if let Statement::ExpressionStatement(expr_stmt) = &mut if_stmt.consequent {
if if_stmt.alternate.is_none() {
let (op, e) = match &mut if_stmt.test {
Expand Down Expand Up @@ -48,7 +47,7 @@ impl<'a> PeepholeOptimizations {
{
// "if (a) {}" => "a;"
let mut expr = if_stmt.test.take_in(ctx.ast);
self.remove_unused_expression(&mut expr, state, ctx);
self.remove_unused_expression(&mut expr, ctx);
return Some(ctx.ast.statement_expression(if_stmt.span, expr));
} else if let Some(Statement::ExpressionStatement(expr_stmt)) = &mut if_stmt.alternate {
let (op, e) = match &mut if_stmt.test {
Expand All @@ -71,7 +70,7 @@ impl<'a> PeepholeOptimizations {
if_stmt.test = unary_expr.argument.take_in(ctx.ast);
if_stmt.consequent = stmt.take_in(ctx.ast);
if_stmt.alternate = None;
state.changed = true;
ctx.state.changed = true;
}
// "if (a) {} else return b;" => "if (!a) return b;"
_ => {
Expand All @@ -82,8 +81,8 @@ impl<'a> PeepholeOptimizations {
);
if_stmt.consequent = stmt.take_in(ctx.ast);
if_stmt.alternate = None;
self.try_minimize_if(if_stmt, state, ctx);
state.changed = true;
self.try_minimize_if(if_stmt, ctx);
ctx.state.changed = true;
}
}
}
Expand All @@ -96,8 +95,8 @@ impl<'a> PeepholeOptimizations {
// "if (!a) return b; else return c;" => "if (a) return c; else return b;"
if_stmt.test = unary_expr.argument.take_in(ctx.ast);
std::mem::swap(&mut if_stmt.consequent, alternate);
self.wrap_to_avoid_ambiguous_else(if_stmt, state, ctx);
state.changed = true;
self.wrap_to_avoid_ambiguous_else(if_stmt, ctx);
ctx.state.changed = true;
}
}
// "if (a) return b; else {}" => "if (a) return b;" is handled by remove_dead_code
Expand All @@ -116,7 +115,7 @@ impl<'a> PeepholeOptimizations {
ctx,
);
if_stmt.consequent = if2_stmt.consequent.take_in(ctx.ast);
state.changed = true;
ctx.state.changed = true;
}
}
}
Expand All @@ -127,12 +126,7 @@ impl<'a> PeepholeOptimizations {
/// Wrap to avoid ambiguous else.
/// `if (foo) if (bar) baz else quaz` -> `if (foo) { if (bar) baz else quaz }`
#[expect(clippy::cast_possible_truncation)]
fn wrap_to_avoid_ambiguous_else(
&self,
if_stmt: &mut IfStatement<'a>,
state: &mut State,
ctx: &mut Ctx<'a, '_>,
) {
fn wrap_to_avoid_ambiguous_else(&self, if_stmt: &mut IfStatement<'a>, ctx: &mut Ctx<'a, '_>) {
if let Statement::IfStatement(if2) = &mut if_stmt.consequent {
if if2.consequent.is_jump_statement() && if2.alternate.is_some() {
let scope_id = ScopeId::new(ctx.scoping.scoping().scopes_len() as u32);
Expand All @@ -143,7 +137,7 @@ impl<'a> PeepholeOptimizations {
scope_id,
),
));
state.changed = true;
ctx.state.changed = true;
}
}
}
Expand Down
Loading
Loading