refactor(minifier): fold statements on exit (#8057)

This commit is contained in:
Boshen 2024-12-21 10:06:04 +00:00
parent 98e8a72bad
commit 6123f5e6ff
2 changed files with 18 additions and 17 deletions

View file

@ -24,7 +24,8 @@ impl<'a> CompressorPass<'a> for PeepholeRemoveDeadCode {
} }
impl<'a> Traverse<'a> for PeepholeRemoveDeadCode { impl<'a> Traverse<'a> for PeepholeRemoveDeadCode {
fn enter_statement(&mut self, stmt: &mut Statement<'a>, ctx: &mut TraverseCtx<'a>) { fn exit_statement(&mut self, stmt: &mut Statement<'a>, ctx: &mut TraverseCtx<'a>) {
self.compress_block(stmt, Ctx(ctx));
let ctx = Ctx(ctx); let ctx = Ctx(ctx);
if let Some(new_stmt) = match stmt { if let Some(new_stmt) = match stmt {
Statement::IfStatement(if_stmt) => self.try_fold_if(if_stmt, ctx), Statement::IfStatement(if_stmt) => self.try_fold_if(if_stmt, ctx),
@ -40,10 +41,6 @@ impl<'a> Traverse<'a> for PeepholeRemoveDeadCode {
} }
} }
fn exit_statement(&mut self, stmt: &mut Statement<'a>, ctx: &mut TraverseCtx<'a>) {
self.compress_block(stmt, Ctx(ctx));
}
fn exit_statements(&mut self, stmts: &mut Vec<'a, Statement<'a>>, ctx: &mut TraverseCtx<'a>) { fn exit_statements(&mut self, stmts: &mut Vec<'a, Statement<'a>>, ctx: &mut TraverseCtx<'a>) {
if stmts.iter().any(|stmt| matches!(stmt, Statement::EmptyStatement(_))) { if stmts.iter().any(|stmt| matches!(stmt, Statement::EmptyStatement(_))) {
stmts.retain(|stmt| !matches!(stmt, Statement::EmptyStatement(_))); stmts.retain(|stmt| !matches!(stmt, Statement::EmptyStatement(_)));
@ -159,22 +156,26 @@ impl<'a, 'b> PeepholeRemoveDeadCode {
ctx: Ctx<'a, 'b>, ctx: Ctx<'a, 'b>,
) -> Option<Statement<'a>> { ) -> Option<Statement<'a>> {
// Descend and remove `else` blocks first. // Descend and remove `else` blocks first.
if let Some(Statement::IfStatement(alternate)) = &mut if_stmt.alternate { match &mut if_stmt.alternate {
Some(Statement::IfStatement(alternate)) => {
if let Some(new_stmt) = self.try_fold_if(alternate, ctx) { if let Some(new_stmt) = self.try_fold_if(alternate, ctx) {
if matches!(new_stmt, Statement::EmptyStatement(_)) { if matches!(new_stmt, Statement::EmptyStatement(_)) {
if_stmt.alternate = None; if_stmt.alternate = None;
self.changed = true;
} else { } else {
if_stmt.alternate = Some(new_stmt); if_stmt.alternate = Some(new_stmt);
} }
self.changed = true;
} }
} }
Some(Statement::EmptyStatement(_)) => {
if_stmt.alternate = None;
self.changed = true;
}
_ => {}
}
match ctx.get_boolean_value(&if_stmt.test) { match ctx.get_boolean_value(&if_stmt.test) {
Some(true) => { Some(true) => Some(ctx.ast.move_statement(&mut if_stmt.consequent)),
// self.changed = true;
Some(ctx.ast.move_statement(&mut if_stmt.consequent))
}
Some(false) => { Some(false) => {
Some(if let Some(alternate) = &mut if_stmt.alternate { Some(if let Some(alternate) = &mut if_stmt.alternate {
ctx.ast.move_statement(alternate) ctx.ast.move_statement(alternate)
@ -186,7 +187,6 @@ impl<'a, 'b> PeepholeRemoveDeadCode {
.get_variable_declaration_statement() .get_variable_declaration_statement()
.unwrap_or_else(|| ctx.ast.statement_empty(SPAN)) .unwrap_or_else(|| ctx.ast.statement_empty(SPAN))
}) })
// self.changed = true;
} }
None => None, None => None,
} }

View file

@ -13,9 +13,10 @@ use oxc_allocator::Allocator;
use oxc_ast::ast::Program; use oxc_ast::ast::Program;
use oxc_mangler::Mangler; use oxc_mangler::Mangler;
pub use crate::{ast_passes::CompressorPass, compressor::Compressor, options::CompressOptions};
pub use oxc_mangler::MangleOptions; pub use oxc_mangler::MangleOptions;
pub use crate::{ast_passes::CompressorPass, compressor::Compressor, options::CompressOptions};
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct MinifierOptions { pub struct MinifierOptions {
pub mangle: Option<MangleOptions>, pub mangle: Option<MangleOptions>,