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
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,10 @@ use crate::CompressorPass;
/// `var a; var b = 1; var c = 2` => `var a, b = 1; c = 2`
/// <https://github.com/google/closure-compiler/blob/v20240609/src/com/google/javascript/jscomp/CollapseVariableDeclarations.java>
pub struct CollapseVariableDeclarations {
changed: bool,
pub(crate) changed: bool,
}

impl<'a> CompressorPass<'a> for CollapseVariableDeclarations {
fn changed(&self) -> bool {
self.changed
}

fn build(&mut self, program: &mut Program<'a>, ctx: &mut ReusableTraverseCtx<'a>) {
self.changed = false;
traverse_mut_with_ctx(self, program, ctx);
Expand Down
6 changes: 1 addition & 5 deletions crates/oxc_minifier/src/ast_passes/exploit_assigns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,10 @@ use crate::CompressorPass;
///
/// <https://github.com/google/closure-compiler/blob/v20240609/src/com/google/javascript/jscomp/ExploitAssigns.java>
pub struct ExploitAssigns {
changed: bool,
pub(crate) changed: bool,
}

impl<'a> CompressorPass<'a> for ExploitAssigns {
fn changed(&self) -> bool {
self.changed
}

fn build(&mut self, program: &mut Program<'a>, ctx: &mut ReusableTraverseCtx<'a>) {
self.changed = false;
traverse_mut_with_ctx(self, program, ctx);
Expand Down
173 changes: 121 additions & 52 deletions crates/oxc_minifier/src/ast_passes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,56 @@ use oxc_ast::ast::*;
use oxc_traverse::{traverse_mut_with_ctx, ReusableTraverseCtx, Traverse, TraverseCtx};

pub trait CompressorPass<'a>: Traverse<'a> {
fn changed(&self) -> bool;

fn build(&mut self, program: &mut Program<'a>, ctx: &mut ReusableTraverseCtx<'a>);
}

// See `peepholeOptimizationsOnce`

// For pass:
// ```
// if (options.collapseVariableDeclarations) {
// passes.maybeAdd(exploitAssign);
// passes.maybeAdd(collapseVariableDeclarations);
// }
// ```
pub struct CollapsePass {
_x0_exploit_assigns: ExploitAssigns,
x1_collapse_variable_declarations: CollapseVariableDeclarations,
}

impl CollapsePass {
pub fn new() -> Self {
Self {
_x0_exploit_assigns: ExploitAssigns::new(),
x1_collapse_variable_declarations: CollapseVariableDeclarations::new(),
}
}
}

impl<'a> CompressorPass<'a> for CollapsePass {
fn build(&mut self, program: &mut Program<'a>, ctx: &mut ReusableTraverseCtx<'a>) {
traverse_mut_with_ctx(self, program, ctx);
}
}

impl<'a> Traverse<'a> for CollapsePass {
fn enter_statements(&mut self, stmts: &mut Vec<'a, Statement<'a>>, ctx: &mut TraverseCtx<'a>) {
self.x1_collapse_variable_declarations.enter_statements(stmts, ctx);
}
}

// See `latePeepholeOptimizations`
pub struct EarlyPass {
pub struct LatePeepholeOptimizations {
x0_statement_fusion: StatementFusion,
x1_peephole_remove_dead_code: PeepholeRemoveDeadCode,
// TODO: MinimizeExitPoints
x2_peephole_minimize_conditions: PeepholeMinimizeConditions,
x3_peephole_substitute_alternate_syntax: PeepholeSubstituteAlternateSyntax,
x4_peephole_replace_known_methods: PeepholeReplaceKnownMethods,
x5_peephole_fold_constants: PeepholeFoldConstants,
changed: bool,
}

impl EarlyPass {
impl LatePeepholeOptimizations {
pub fn new() -> Self {
Self {
x0_statement_fusion: StatementFusion::new(),
Expand All @@ -51,29 +83,55 @@ impl EarlyPass {
),
x4_peephole_replace_known_methods: PeepholeReplaceKnownMethods::new(),
x5_peephole_fold_constants: PeepholeFoldConstants::new(),
changed: false,
}
}
}

impl<'a> CompressorPass<'a> for EarlyPass {
fn reset_changed(&mut self) {
self.x0_statement_fusion.changed = false;
self.x1_peephole_remove_dead_code.changed = false;
self.x2_peephole_minimize_conditions.changed = false;
self.x3_peephole_substitute_alternate_syntax.changed = false;
self.x4_peephole_replace_known_methods.changed = false;
self.x5_peephole_fold_constants.changed = false;
}

fn changed(&self) -> bool {
self.changed
self.x0_statement_fusion.changed
|| self.x1_peephole_remove_dead_code.changed
|| self.x2_peephole_minimize_conditions.changed
|| self.x3_peephole_substitute_alternate_syntax.changed
|| self.x4_peephole_replace_known_methods.changed
|| self.x5_peephole_fold_constants.changed
}

pub fn run_in_loop<'a>(
&mut self,
program: &mut Program<'a>,
ctx: &mut ReusableTraverseCtx<'a>,
) {
let mut i = 0;
loop {
self.reset_changed();
self.build(program, ctx);
if !self.changed() {
break;
}
if i > 10 {
debug_assert!(false, "Ran loop more than 10 times.");
break;
}
i += 1;
}
}
}

impl<'a> CompressorPass<'a> for LatePeepholeOptimizations {
fn build(&mut self, program: &mut Program<'a>, ctx: &mut ReusableTraverseCtx<'a>) {
self.changed = false;
traverse_mut_with_ctx(self, program, ctx);
self.changed = self.x0_statement_fusion.changed()
|| self.x1_peephole_remove_dead_code.changed()
|| self.x2_peephole_minimize_conditions.changed()
|| self.x3_peephole_substitute_alternate_syntax.changed()
|| self.x4_peephole_replace_known_methods.changed()
|| self.x5_peephole_fold_constants.changed();
}
}

impl<'a> Traverse<'a> for EarlyPass {
impl<'a> Traverse<'a> for LatePeepholeOptimizations {
fn enter_statement(&mut self, stmt: &mut Statement<'a>, ctx: &mut TraverseCtx<'a>) {
self.x1_peephole_remove_dead_code.enter_statement(stmt, ctx);
}
Expand Down Expand Up @@ -141,80 +199,91 @@ impl<'a> Traverse<'a> for EarlyPass {
}
}

// Passes listed in `getFinalization` in `DefaultPassConfig`
pub struct LatePass {
x0_exploit_assigns: ExploitAssigns,
x1_collapse_variable_declarations: CollapseVariableDeclarations,
x2_peephole_substitute_alternate_syntax: PeepholeSubstituteAlternateSyntax,
changed: bool,
// See `createPeepholeOptimizationsPass`
pub struct PeepholeOptimizations {
// TODO: MinimizeExitPoints
x2_peephole_minimize_conditions: PeepholeMinimizeConditions,
x3_peephole_substitute_alternate_syntax: PeepholeSubstituteAlternateSyntax,
x4_peephole_replace_known_methods: PeepholeReplaceKnownMethods,
x5_peephole_remove_dead_code: PeepholeRemoveDeadCode,
x6_peephole_fold_constants: PeepholeFoldConstants,
}

impl LatePass {
impl PeepholeOptimizations {
pub fn new() -> Self {
Self {
x0_exploit_assigns: ExploitAssigns::new(),
x1_collapse_variable_declarations: CollapseVariableDeclarations::new(),
x2_peephole_substitute_alternate_syntax: PeepholeSubstituteAlternateSyntax::new(
x2_peephole_minimize_conditions: PeepholeMinimizeConditions::new(),
x3_peephole_substitute_alternate_syntax: PeepholeSubstituteAlternateSyntax::new(
/* in_fixed_loop */ false,
),
changed: false,
x4_peephole_replace_known_methods: PeepholeReplaceKnownMethods::new(),
x5_peephole_remove_dead_code: PeepholeRemoveDeadCode::new(),
x6_peephole_fold_constants: PeepholeFoldConstants::new(),
}
}
}

impl<'a> CompressorPass<'a> for LatePass {
fn changed(&self) -> bool {
self.changed
}

impl<'a> CompressorPass<'a> for PeepholeOptimizations {
fn build(&mut self, program: &mut Program<'a>, ctx: &mut ReusableTraverseCtx<'a>) {
self.changed = false;
traverse_mut_with_ctx(self, program, ctx);
self.changed = self.x0_exploit_assigns.changed()
|| self.x0_exploit_assigns.changed()
|| self.x1_collapse_variable_declarations.changed()
|| self.x2_peephole_substitute_alternate_syntax.changed();
}
}

impl<'a> Traverse<'a> for LatePass {
fn enter_statements(&mut self, stmts: &mut Vec<'a, Statement<'a>>, ctx: &mut TraverseCtx<'a>) {
self.x1_collapse_variable_declarations.enter_statements(stmts, ctx);
impl<'a> Traverse<'a> for PeepholeOptimizations {
fn enter_statement(&mut self, stmt: &mut Statement<'a>, ctx: &mut TraverseCtx<'a>) {
self.x5_peephole_remove_dead_code.enter_statement(stmt, ctx);
}

fn exit_statement(&mut self, stmt: &mut Statement<'a>, ctx: &mut TraverseCtx<'a>) {
self.x2_peephole_minimize_conditions.exit_statement(stmt, ctx);
self.x5_peephole_remove_dead_code.exit_statement(stmt, ctx);
}

fn exit_program(&mut self, program: &mut Program<'a>, ctx: &mut TraverseCtx<'a>) {
self.x5_peephole_remove_dead_code.exit_program(program, ctx);
}

fn exit_statements(&mut self, stmts: &mut Vec<'a, Statement<'a>>, ctx: &mut TraverseCtx<'a>) {
self.x5_peephole_remove_dead_code.exit_statements(stmts, ctx);
}

fn exit_return_statement(&mut self, stmt: &mut ReturnStatement<'a>, ctx: &mut TraverseCtx<'a>) {
self.x2_peephole_substitute_alternate_syntax.exit_return_statement(stmt, ctx);
self.x3_peephole_substitute_alternate_syntax.exit_return_statement(stmt, ctx);
}

fn enter_variable_declaration(
&mut self,
decl: &mut VariableDeclaration<'a>,
ctx: &mut TraverseCtx<'a>,
) {
self.x2_peephole_substitute_alternate_syntax.enter_variable_declaration(decl, ctx);
self.x3_peephole_substitute_alternate_syntax.enter_variable_declaration(decl, ctx);
}

fn enter_call_expression(&mut self, expr: &mut CallExpression<'a>, ctx: &mut TraverseCtx<'a>) {
self.x2_peephole_substitute_alternate_syntax.enter_call_expression(expr, ctx);
fn enter_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
self.x3_peephole_substitute_alternate_syntax.enter_expression(expr, ctx);
self.x4_peephole_replace_known_methods.enter_expression(expr, ctx);
}

fn exit_call_expression(&mut self, expr: &mut CallExpression<'a>, ctx: &mut TraverseCtx<'a>) {
self.x2_peephole_substitute_alternate_syntax.exit_call_expression(expr, ctx);
fn exit_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
self.x2_peephole_minimize_conditions.exit_expression(expr, ctx);
self.x3_peephole_substitute_alternate_syntax.exit_expression(expr, ctx);
self.x5_peephole_remove_dead_code.exit_expression(expr, ctx);
self.x6_peephole_fold_constants.exit_expression(expr, ctx);
}

fn enter_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
self.x2_peephole_substitute_alternate_syntax.enter_expression(expr, ctx);
fn enter_call_expression(&mut self, expr: &mut CallExpression<'a>, ctx: &mut TraverseCtx<'a>) {
self.x3_peephole_substitute_alternate_syntax.enter_call_expression(expr, ctx);
}

fn exit_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
self.x2_peephole_substitute_alternate_syntax.exit_expression(expr, ctx);
fn exit_call_expression(&mut self, expr: &mut CallExpression<'a>, ctx: &mut TraverseCtx<'a>) {
self.x3_peephole_substitute_alternate_syntax.exit_call_expression(expr, ctx);
}

fn enter_binary_expression(
&mut self,
expr: &mut BinaryExpression<'a>,
ctx: &mut TraverseCtx<'a>,
) {
self.x2_peephole_substitute_alternate_syntax.enter_binary_expression(expr, ctx);
self.x3_peephole_substitute_alternate_syntax.enter_binary_expression(expr, ctx);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,10 @@ use crate::{node_util::Ctx, CompressorPass};
///
/// <https://github.com/google/closure-compiler/blob/v20240609/src/com/google/javascript/jscomp/PeepholeFoldConstants.java>
pub struct PeepholeFoldConstants {
changed: bool,
pub(crate) changed: bool,
}

impl<'a> CompressorPass<'a> for PeepholeFoldConstants {
fn changed(&self) -> bool {
self.changed
}

fn build(&mut self, program: &mut Program<'a>, ctx: &mut ReusableTraverseCtx<'a>) {
self.changed = false;
traverse_mut_with_ctx(self, program, ctx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,10 @@ use crate::CompressorPass;
///
/// <https://github.com/google/closure-compiler/blob/v20240609/src/com/google/javascript/jscomp/PeepholeMinimizeConditions.java>
pub struct PeepholeMinimizeConditions {
changed: bool,
pub(crate) changed: bool,
}

impl<'a> CompressorPass<'a> for PeepholeMinimizeConditions {
fn changed(&self) -> bool {
self.changed
}

fn build(&mut self, program: &mut Program<'a>, ctx: &mut ReusableTraverseCtx<'a>) {
self.changed = false;
traverse_mut_with_ctx(self, program, ctx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,10 @@ use crate::{keep_var::KeepVar, CompressorPass};
/// 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>
pub struct PeepholeRemoveDeadCode {
changed: bool,
pub(crate) changed: bool,
}

impl<'a> CompressorPass<'a> for PeepholeRemoveDeadCode {
fn changed(&self) -> bool {
self.changed
}

fn build(&mut self, program: &mut Program<'a>, ctx: &mut ReusableTraverseCtx<'a>) {
self.changed = false;
traverse_mut_with_ctx(self, program, ctx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,10 @@ use crate::{node_util::Ctx, CompressorPass};
/// Minimize With Known Methods
/// <https://github.com/google/closure-compiler/blob/v20240609/src/com/google/javascript/jscomp/PeepholeReplaceKnownMethods.java>
pub struct PeepholeReplaceKnownMethods {
changed: bool,
pub(crate) changed: bool,
}

impl<'a> CompressorPass<'a> for PeepholeReplaceKnownMethods {
fn changed(&self) -> bool {
self.changed
}

fn build(&mut self, program: &mut Program<'a>, ctx: &mut ReusableTraverseCtx<'a>) {
self.changed = false;
traverse_mut_with_ctx(self, program, ctx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,10 @@ pub struct PeepholeSubstituteAlternateSyntax {
// states
in_define_export: bool,

changed: bool,
pub(crate) changed: bool,
}

impl<'a> CompressorPass<'a> for PeepholeSubstituteAlternateSyntax {
fn changed(&self) -> bool {
self.changed
}

fn build(&mut self, program: &mut Program<'a>, ctx: &mut ReusableTraverseCtx<'a>) {
self.changed = false;
traverse_mut_with_ctx(self, program, ctx);
Expand Down
4 changes: 0 additions & 4 deletions crates/oxc_minifier/src/ast_passes/remove_syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ pub struct RemoveSyntax {
}

impl<'a> CompressorPass<'a> for RemoveSyntax {
fn changed(&self) -> bool {
false
}

fn build(&mut self, program: &mut Program<'a>, ctx: &mut ReusableTraverseCtx<'a>) {
traverse_mut_with_ctx(self, program, ctx);
}
Expand Down
6 changes: 1 addition & 5 deletions crates/oxc_minifier/src/ast_passes/statement_fusion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,10 @@ use crate::CompressorPass;
///
/// <https://github.com/google/closure-compiler/blob/v20240609/src/com/google/javascript/jscomp/StatementFusion.java>
pub struct StatementFusion {
changed: bool,
pub(crate) changed: bool,
}

impl<'a> CompressorPass<'a> for StatementFusion {
fn changed(&self) -> bool {
self.changed
}

fn build(&mut self, program: &mut Program<'a>, ctx: &mut ReusableTraverseCtx<'a>) {
self.changed = false;
traverse_mut_with_ctx(self, program, ctx);
Expand Down
Loading