Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(minifier): align ast pass names with closure compiler #5908

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 @@ -8,21 +8,21 @@ use crate::{CompressOptions, CompressorPass};
///
/// `var a; var b = 1; var c = 2` => `var a, b = 1; c = 2`
/// TODO: `a = null; b = null;` => `a = b = null`
pub struct Collapse {
pub struct CollapseVariableDeclarations {
options: CompressOptions,
}

impl<'a> CompressorPass<'a> for Collapse {}
impl<'a> CompressorPass<'a> for CollapseVariableDeclarations {}

impl<'a> Traverse<'a> for Collapse {
impl<'a> Traverse<'a> for CollapseVariableDeclarations {
fn enter_statements(&mut self, stmts: &mut Vec<'a, Statement<'a>>, ctx: &mut TraverseCtx<'a>) {
if self.options.join_vars {
self.join_vars(stmts, ctx);
}
}
}

impl<'a> Collapse {
impl<'a> CollapseVariableDeclarations {
pub fn new(options: CompressOptions) -> Self {
Self { options }
}
Expand Down
20 changes: 10 additions & 10 deletions crates/oxc_minifier/src/ast_passes/mod.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
mod collapse;
mod fold_constants;
mod minimize_conditions;
mod remove_dead_code;
mod collapse_variable_declarations;
mod peephole_fold_constants;
mod peephole_minimize_conditions;
mod peephole_remove_dead_code;
mod peephole_substitute_alternate_syntax;
mod remove_syntax;
mod substitute_alternate_syntax;

pub use collapse::Collapse;
pub use fold_constants::FoldConstants;
pub use minimize_conditions::MinimizeConditions;
pub use remove_dead_code::RemoveDeadCode;
pub use collapse_variable_declarations::CollapseVariableDeclarations;
pub use peephole_fold_constants::PeepholeFoldConstants;
pub use peephole_minimize_conditions::PeepholeMinimizeConditions;
pub use peephole_remove_dead_code::PeepholeRemoveDeadCode;
pub use peephole_substitute_alternate_syntax::PeepholeSubstituteAlternateSyntax;
pub use remove_syntax::RemoveSyntax;
pub use substitute_alternate_syntax::SubstituteAlternateSyntax;

use oxc_ast::ast::Program;
use oxc_semantic::{ScopeTree, SymbolTable};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@ use crate::{
/// Constant Folding
///
/// <https://github.com/google/closure-compiler/blob/master/src/com/google/javascript/jscomp/PeepholeFoldConstants.java>
pub struct FoldConstants {
pub struct PeepholeFoldConstants {
evaluate: bool,
}

impl<'a> CompressorPass<'a> for FoldConstants {}
impl<'a> CompressorPass<'a> for PeepholeFoldConstants {}

impl<'a> Traverse<'a> for FoldConstants {
impl<'a> Traverse<'a> for PeepholeFoldConstants {
fn exit_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
self.fold_expression(expr, ctx);
}
}

impl<'a> FoldConstants {
impl<'a> PeepholeFoldConstants {
pub fn new() -> Self {
Self { evaluate: false }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ use crate::{node_util::NodeUtil, tri::Tri, CompressorPass};
/// with `? :` and short-circuit binary operators.
///
/// <https://github.com/google/closure-compiler/blob/master/src/com/google/javascript/jscomp/PeepholeMinimizeConditions.java>
pub struct MinimizeConditions;
pub struct PeepholeMinimizeConditions;

impl<'a> CompressorPass<'a> for MinimizeConditions {}
impl<'a> CompressorPass<'a> for PeepholeMinimizeConditions {}

impl<'a> Traverse<'a> for MinimizeConditions {
impl<'a> Traverse<'a> for PeepholeMinimizeConditions {
fn exit_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
self.fold_expression(expr, ctx);
}
}

impl<'a> MinimizeConditions {
impl<'a> PeepholeMinimizeConditions {
pub fn new() -> Self {
Self
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ use crate::{keep_var::KeepVar, node_util::NodeUtil, tri::Tri, CompressorPass};
/// Terser option: `dead_code: true`.
///
/// See `KeepVar` at the end of this file for `var` hoisting logic.
pub struct RemoveDeadCode;
pub struct PeepholeRemoveDeadCode;

impl<'a> CompressorPass<'a> for RemoveDeadCode {}
impl<'a> CompressorPass<'a> for PeepholeRemoveDeadCode {}

impl<'a> Traverse<'a> for RemoveDeadCode {
impl<'a> Traverse<'a> for PeepholeRemoveDeadCode {
fn enter_statement(&mut self, stmt: &mut Statement<'a>, ctx: &mut TraverseCtx<'a>) {
Self::fold_if_statement(stmt, ctx);
}
Expand All @@ -25,7 +25,7 @@ impl<'a> Traverse<'a> for RemoveDeadCode {
}
}

impl<'a> RemoveDeadCode {
impl<'a> PeepholeRemoveDeadCode {
pub fn new() -> Self {
Self {}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ use crate::{node_util::NodeUtil, CompressOptions, CompressorPass};
/// A peephole optimization that minimizes code by simplifying conditional
/// expressions, replacing IFs with HOOKs, replacing object constructors
/// with literals, and simplifying returns.
pub struct SubstituteAlternateSyntax {
pub struct PeepholeSubstituteAlternateSyntax {
options: CompressOptions,
in_define_export: bool,
}

impl<'a> CompressorPass<'a> for SubstituteAlternateSyntax {}
impl<'a> CompressorPass<'a> for PeepholeSubstituteAlternateSyntax {}

impl<'a> Traverse<'a> for SubstituteAlternateSyntax {
impl<'a> Traverse<'a> for PeepholeSubstituteAlternateSyntax {
fn enter_statement(&mut self, stmt: &mut Statement<'a>, _ctx: &mut TraverseCtx<'a>) {
self.compress_block(stmt);
// self.compress_while(stmt);
Expand Down Expand Up @@ -81,7 +81,7 @@ impl<'a> Traverse<'a> for SubstituteAlternateSyntax {
}
}

impl<'a> SubstituteAlternateSyntax {
impl<'a> PeepholeSubstituteAlternateSyntax {
pub fn new(options: CompressOptions) -> Self {
Self { options, in_define_export: false }
}
Expand Down
14 changes: 7 additions & 7 deletions crates/oxc_minifier/src/compressor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use oxc_traverse::TraverseCtx;

use crate::{
ast_passes::{
Collapse, FoldConstants, MinimizeConditions, RemoveDeadCode, RemoveSyntax,
SubstituteAlternateSyntax,
CollapseVariableDeclarations, PeepholeFoldConstants, PeepholeMinimizeConditions,
PeepholeRemoveDeadCode, PeepholeSubstituteAlternateSyntax, RemoveSyntax,
},
CompressOptions, CompressorPass,
};
Expand Down Expand Up @@ -53,31 +53,31 @@ impl<'a> Compressor<'a> {

fn minimize_conditions(&self, program: &mut Program<'a>, ctx: &mut TraverseCtx<'a>) {
if self.options.minimize_conditions {
MinimizeConditions::new().build(program, ctx);
PeepholeMinimizeConditions::new().build(program, ctx);
}
}

fn fold_constants(&self, program: &mut Program<'a>, ctx: &mut TraverseCtx<'a>) {
if self.options.fold_constants {
FoldConstants::new().with_evaluate(self.options.evaluate).build(program, ctx);
PeepholeFoldConstants::new().with_evaluate(self.options.evaluate).build(program, ctx);
}
}

fn substitute_alternate_syntax(&self, program: &mut Program<'a>, ctx: &mut TraverseCtx<'a>) {
if self.options.substitute_alternate_syntax {
SubstituteAlternateSyntax::new(self.options).build(program, ctx);
PeepholeSubstituteAlternateSyntax::new(self.options).build(program, ctx);
}
}

fn remove_dead_code(&self, program: &mut Program<'a>, ctx: &mut TraverseCtx<'a>) {
if self.options.remove_dead_code {
RemoveDeadCode::new().build(program, ctx);
PeepholeRemoveDeadCode::new().build(program, ctx);
}
}

fn collapse(&self, program: &mut Program<'a>, ctx: &mut TraverseCtx<'a>) {
if self.options.collapse {
Collapse::new(self.options).build(program, ctx);
CollapseVariableDeclarations::new(self.options).build(program, ctx);
}
}
}
5 changes: 1 addition & 4 deletions crates/oxc_minifier/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ use oxc_ast::ast::Program;
use oxc_mangler::Mangler;

pub use crate::{
ast_passes::{CompressorPass, RemoveDeadCode, RemoveSyntax},
compressor::Compressor,
options::CompressOptions,
plugins::*,
ast_passes::CompressorPass, compressor::Compressor, options::CompressOptions, plugins::*,
};

#[derive(Debug, Clone, Copy)]
Expand Down
6 changes: 3 additions & 3 deletions crates/oxc_minifier/tests/ast_passes/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
mod collapse_variable_declarations;
mod dead_code_elimination;
mod fold_conditions;
mod fold_constants;
mod minimize_conditions;
mod peephole_fold_constants;
mod peephole_minimize_conditions;
mod peephole_substitute_alternate_syntax;
mod remove_syntax;
mod reorder_constant_expression;
mod substitute_alternate_syntax;

// Oxc Integration Tests

Expand Down