mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 04:08:41 +00:00
refactor(minifier): fold statements on exit (#8057)
This commit is contained in:
parent
98e8a72bad
commit
6123f5e6ff
2 changed files with 18 additions and 17 deletions
|
|
@ -24,7 +24,8 @@ impl<'a> CompressorPass<'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);
|
||||
if let Some(new_stmt) = match stmt {
|
||||
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>) {
|
||||
if stmts.iter().any(|stmt| matches!(stmt, Statement::EmptyStatement(_))) {
|
||||
stmts.retain(|stmt| !matches!(stmt, Statement::EmptyStatement(_)));
|
||||
|
|
@ -159,22 +156,26 @@ impl<'a, 'b> PeepholeRemoveDeadCode {
|
|||
ctx: Ctx<'a, 'b>,
|
||||
) -> Option<Statement<'a>> {
|
||||
// Descend and remove `else` blocks first.
|
||||
if let Some(Statement::IfStatement(alternate)) = &mut if_stmt.alternate {
|
||||
if let Some(new_stmt) = self.try_fold_if(alternate, ctx) {
|
||||
if matches!(new_stmt, Statement::EmptyStatement(_)) {
|
||||
if_stmt.alternate = None;
|
||||
match &mut if_stmt.alternate {
|
||||
Some(Statement::IfStatement(alternate)) => {
|
||||
if let Some(new_stmt) = self.try_fold_if(alternate, ctx) {
|
||||
if matches!(new_stmt, Statement::EmptyStatement(_)) {
|
||||
if_stmt.alternate = None;
|
||||
} else {
|
||||
if_stmt.alternate = Some(new_stmt);
|
||||
}
|
||||
self.changed = true;
|
||||
} else {
|
||||
if_stmt.alternate = Some(new_stmt);
|
||||
}
|
||||
}
|
||||
Some(Statement::EmptyStatement(_)) => {
|
||||
if_stmt.alternate = None;
|
||||
self.changed = true;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
match ctx.get_boolean_value(&if_stmt.test) {
|
||||
Some(true) => {
|
||||
// self.changed = true;
|
||||
Some(ctx.ast.move_statement(&mut if_stmt.consequent))
|
||||
}
|
||||
Some(true) => Some(ctx.ast.move_statement(&mut if_stmt.consequent)),
|
||||
Some(false) => {
|
||||
Some(if let Some(alternate) = &mut if_stmt.alternate {
|
||||
ctx.ast.move_statement(alternate)
|
||||
|
|
@ -186,7 +187,6 @@ impl<'a, 'b> PeepholeRemoveDeadCode {
|
|||
.get_variable_declaration_statement()
|
||||
.unwrap_or_else(|| ctx.ast.statement_empty(SPAN))
|
||||
})
|
||||
// self.changed = true;
|
||||
}
|
||||
None => None,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,9 +13,10 @@ use oxc_allocator::Allocator;
|
|||
use oxc_ast::ast::Program;
|
||||
use oxc_mangler::Mangler;
|
||||
|
||||
pub use crate::{ast_passes::CompressorPass, compressor::Compressor, options::CompressOptions};
|
||||
pub use oxc_mangler::MangleOptions;
|
||||
|
||||
pub use crate::{ast_passes::CompressorPass, compressor::Compressor, options::CompressOptions};
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct MinifierOptions {
|
||||
pub mangle: Option<MangleOptions>,
|
||||
|
|
|
|||
Loading…
Reference in a new issue