mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
refactor(minifier): align ast pass names with closure compiler (#5908)
This commit is contained in:
parent
f4fac0f488
commit
144611ef49
12 changed files with 41 additions and 44 deletions
|
|
@ -8,13 +8,13 @@ use crate::{CompressOptions, CompressorPass};
|
||||||
///
|
///
|
||||||
/// `var a; var b = 1; var c = 2` => `var a, b = 1; c = 2`
|
/// `var a; var b = 1; var c = 2` => `var a, b = 1; c = 2`
|
||||||
/// TODO: `a = null; b = null;` => `a = b = null`
|
/// TODO: `a = null; b = null;` => `a = b = null`
|
||||||
pub struct Collapse {
|
pub struct CollapseVariableDeclarations {
|
||||||
options: CompressOptions,
|
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>) {
|
fn enter_statements(&mut self, stmts: &mut Vec<'a, Statement<'a>>, ctx: &mut TraverseCtx<'a>) {
|
||||||
if self.options.join_vars {
|
if self.options.join_vars {
|
||||||
self.join_vars(stmts, ctx);
|
self.join_vars(stmts, ctx);
|
||||||
|
|
@ -22,7 +22,7 @@ impl<'a> Traverse<'a> for Collapse {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Collapse {
|
impl<'a> CollapseVariableDeclarations {
|
||||||
pub fn new(options: CompressOptions) -> Self {
|
pub fn new(options: CompressOptions) -> Self {
|
||||||
Self { options }
|
Self { options }
|
||||||
}
|
}
|
||||||
|
|
@ -1,16 +1,16 @@
|
||||||
mod collapse;
|
mod collapse_variable_declarations;
|
||||||
mod fold_constants;
|
mod peephole_fold_constants;
|
||||||
mod minimize_conditions;
|
mod peephole_minimize_conditions;
|
||||||
mod remove_dead_code;
|
mod peephole_remove_dead_code;
|
||||||
|
mod peephole_substitute_alternate_syntax;
|
||||||
mod remove_syntax;
|
mod remove_syntax;
|
||||||
mod substitute_alternate_syntax;
|
|
||||||
|
|
||||||
pub use collapse::Collapse;
|
pub use collapse_variable_declarations::CollapseVariableDeclarations;
|
||||||
pub use fold_constants::FoldConstants;
|
pub use peephole_fold_constants::PeepholeFoldConstants;
|
||||||
pub use minimize_conditions::MinimizeConditions;
|
pub use peephole_minimize_conditions::PeepholeMinimizeConditions;
|
||||||
pub use remove_dead_code::RemoveDeadCode;
|
pub use peephole_remove_dead_code::PeepholeRemoveDeadCode;
|
||||||
|
pub use peephole_substitute_alternate_syntax::PeepholeSubstituteAlternateSyntax;
|
||||||
pub use remove_syntax::RemoveSyntax;
|
pub use remove_syntax::RemoveSyntax;
|
||||||
pub use substitute_alternate_syntax::SubstituteAlternateSyntax;
|
|
||||||
|
|
||||||
use oxc_ast::ast::Program;
|
use oxc_ast::ast::Program;
|
||||||
use oxc_semantic::{ScopeTree, SymbolTable};
|
use oxc_semantic::{ScopeTree, SymbolTable};
|
||||||
|
|
|
||||||
|
|
@ -21,19 +21,19 @@ use crate::{
|
||||||
/// Constant Folding
|
/// Constant Folding
|
||||||
///
|
///
|
||||||
/// <https://github.com/google/closure-compiler/blob/master/src/com/google/javascript/jscomp/PeepholeFoldConstants.java>
|
/// <https://github.com/google/closure-compiler/blob/master/src/com/google/javascript/jscomp/PeepholeFoldConstants.java>
|
||||||
pub struct FoldConstants {
|
pub struct PeepholeFoldConstants {
|
||||||
evaluate: bool,
|
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>) {
|
fn exit_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
|
||||||
self.fold_expression(expr, ctx);
|
self.fold_expression(expr, ctx);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> FoldConstants {
|
impl<'a> PeepholeFoldConstants {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self { evaluate: false }
|
Self { evaluate: false }
|
||||||
}
|
}
|
||||||
|
|
@ -10,17 +10,17 @@ use crate::{node_util::NodeUtil, tri::Tri, CompressorPass};
|
||||||
/// with `? :` and short-circuit binary operators.
|
/// with `? :` and short-circuit binary operators.
|
||||||
///
|
///
|
||||||
/// <https://github.com/google/closure-compiler/blob/master/src/com/google/javascript/jscomp/PeepholeMinimizeConditions.java>
|
/// <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>) {
|
fn exit_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
|
||||||
self.fold_expression(expr, ctx);
|
self.fold_expression(expr, ctx);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> MinimizeConditions {
|
impl<'a> PeepholeMinimizeConditions {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self
|
Self
|
||||||
}
|
}
|
||||||
|
|
@ -10,11 +10,11 @@ use crate::{keep_var::KeepVar, node_util::NodeUtil, tri::Tri, CompressorPass};
|
||||||
/// Terser option: `dead_code: true`.
|
/// Terser option: `dead_code: true`.
|
||||||
///
|
///
|
||||||
/// See `KeepVar` at the end of this file for `var` hoisting logic.
|
/// 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>) {
|
fn enter_statement(&mut self, stmt: &mut Statement<'a>, ctx: &mut TraverseCtx<'a>) {
|
||||||
Self::fold_if_statement(stmt, ctx);
|
Self::fold_if_statement(stmt, ctx);
|
||||||
}
|
}
|
||||||
|
|
@ -25,7 +25,7 @@ impl<'a> Traverse<'a> for RemoveDeadCode {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> RemoveDeadCode {
|
impl<'a> PeepholeRemoveDeadCode {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {}
|
Self {}
|
||||||
}
|
}
|
||||||
|
|
@ -11,14 +11,14 @@ use crate::{node_util::NodeUtil, CompressOptions, CompressorPass};
|
||||||
/// A peephole optimization that minimizes code by simplifying conditional
|
/// A peephole optimization that minimizes code by simplifying conditional
|
||||||
/// expressions, replacing IFs with HOOKs, replacing object constructors
|
/// expressions, replacing IFs with HOOKs, replacing object constructors
|
||||||
/// with literals, and simplifying returns.
|
/// with literals, and simplifying returns.
|
||||||
pub struct SubstituteAlternateSyntax {
|
pub struct PeepholeSubstituteAlternateSyntax {
|
||||||
options: CompressOptions,
|
options: CompressOptions,
|
||||||
in_define_export: bool,
|
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>) {
|
fn enter_statement(&mut self, stmt: &mut Statement<'a>, _ctx: &mut TraverseCtx<'a>) {
|
||||||
self.compress_block(stmt);
|
self.compress_block(stmt);
|
||||||
// self.compress_while(stmt);
|
// self.compress_while(stmt);
|
||||||
|
|
@ -81,7 +81,7 @@ impl<'a> Traverse<'a> for SubstituteAlternateSyntax {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> SubstituteAlternateSyntax {
|
impl<'a> PeepholeSubstituteAlternateSyntax {
|
||||||
pub fn new(options: CompressOptions) -> Self {
|
pub fn new(options: CompressOptions) -> Self {
|
||||||
Self { options, in_define_export: false }
|
Self { options, in_define_export: false }
|
||||||
}
|
}
|
||||||
|
|
@ -5,8 +5,8 @@ use oxc_traverse::TraverseCtx;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
ast_passes::{
|
ast_passes::{
|
||||||
Collapse, FoldConstants, MinimizeConditions, RemoveDeadCode, RemoveSyntax,
|
CollapseVariableDeclarations, PeepholeFoldConstants, PeepholeMinimizeConditions,
|
||||||
SubstituteAlternateSyntax,
|
PeepholeRemoveDeadCode, PeepholeSubstituteAlternateSyntax, RemoveSyntax,
|
||||||
},
|
},
|
||||||
CompressOptions, CompressorPass,
|
CompressOptions, CompressorPass,
|
||||||
};
|
};
|
||||||
|
|
@ -53,31 +53,31 @@ impl<'a> Compressor<'a> {
|
||||||
|
|
||||||
fn minimize_conditions(&self, program: &mut Program<'a>, ctx: &mut TraverseCtx<'a>) {
|
fn minimize_conditions(&self, program: &mut Program<'a>, ctx: &mut TraverseCtx<'a>) {
|
||||||
if self.options.minimize_conditions {
|
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>) {
|
fn fold_constants(&self, program: &mut Program<'a>, ctx: &mut TraverseCtx<'a>) {
|
||||||
if self.options.fold_constants {
|
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>) {
|
fn substitute_alternate_syntax(&self, program: &mut Program<'a>, ctx: &mut TraverseCtx<'a>) {
|
||||||
if self.options.substitute_alternate_syntax {
|
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>) {
|
fn remove_dead_code(&self, program: &mut Program<'a>, ctx: &mut TraverseCtx<'a>) {
|
||||||
if self.options.remove_dead_code {
|
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>) {
|
fn collapse(&self, program: &mut Program<'a>, ctx: &mut TraverseCtx<'a>) {
|
||||||
if self.options.collapse {
|
if self.options.collapse {
|
||||||
Collapse::new(self.options).build(program, ctx);
|
CollapseVariableDeclarations::new(self.options).build(program, ctx);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,10 +16,7 @@ use oxc_ast::ast::Program;
|
||||||
use oxc_mangler::Mangler;
|
use oxc_mangler::Mangler;
|
||||||
|
|
||||||
pub use crate::{
|
pub use crate::{
|
||||||
ast_passes::{CompressorPass, RemoveDeadCode, RemoveSyntax},
|
ast_passes::CompressorPass, compressor::Compressor, options::CompressOptions, plugins::*,
|
||||||
compressor::Compressor,
|
|
||||||
options::CompressOptions,
|
|
||||||
plugins::*,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,11 @@
|
||||||
mod collapse_variable_declarations;
|
mod collapse_variable_declarations;
|
||||||
mod dead_code_elimination;
|
mod dead_code_elimination;
|
||||||
mod fold_conditions;
|
mod fold_conditions;
|
||||||
mod fold_constants;
|
mod peephole_fold_constants;
|
||||||
mod minimize_conditions;
|
mod peephole_minimize_conditions;
|
||||||
|
mod peephole_substitute_alternate_syntax;
|
||||||
mod remove_syntax;
|
mod remove_syntax;
|
||||||
mod reorder_constant_expression;
|
mod reorder_constant_expression;
|
||||||
mod substitute_alternate_syntax;
|
|
||||||
|
|
||||||
// Oxc Integration Tests
|
// Oxc Integration Tests
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue