mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
refactor(minifier): clean up try_optimize_block (#8139)
This commit is contained in:
parent
2b2a37347c
commit
75264ed7d6
1 changed files with 21 additions and 22 deletions
|
|
@ -25,9 +25,9 @@ impl<'a> CompressorPass<'a> for PeepholeRemoveDeadCode {
|
||||||
|
|
||||||
impl<'a> Traverse<'a> for PeepholeRemoveDeadCode {
|
impl<'a> Traverse<'a> for PeepholeRemoveDeadCode {
|
||||||
fn exit_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::BlockStatement(block_stmt) => Self::try_optimize_block(block_stmt, ctx),
|
||||||
Statement::IfStatement(if_stmt) => self.try_fold_if(if_stmt, ctx),
|
Statement::IfStatement(if_stmt) => self.try_fold_if(if_stmt, ctx),
|
||||||
Statement::ForStatement(for_stmt) => self.try_fold_for(for_stmt, ctx),
|
Statement::ForStatement(for_stmt) => self.try_fold_for(for_stmt, ctx),
|
||||||
Statement::ExpressionStatement(expr_stmt) => {
|
Statement::ExpressionStatement(expr_stmt) => {
|
||||||
|
|
@ -126,17 +126,16 @@ impl<'a, 'b> PeepholeRemoveDeadCode {
|
||||||
|
|
||||||
/// Remove block from single line blocks
|
/// Remove block from single line blocks
|
||||||
/// `{ block } -> block`
|
/// `{ block } -> block`
|
||||||
fn compress_block(&mut self, stmt: &mut Statement<'a>, ctx: Ctx<'a, 'b>) {
|
fn try_optimize_block(
|
||||||
if let Statement::BlockStatement(block) = stmt {
|
stmt: &mut BlockStatement<'a>,
|
||||||
|
ctx: Ctx<'a, 'b>,
|
||||||
|
) -> Option<Statement<'a>> {
|
||||||
// Avoid compressing `if (x) { var x = 1 }` to `if (x) var x = 1` due to different
|
// Avoid compressing `if (x) { var x = 1 }` to `if (x) var x = 1` due to different
|
||||||
// semantics according to AnnexB, which lead to different semantics.
|
// semantics according to AnnexB, which lead to different semantics.
|
||||||
if block.body.len() == 1 && !block.body[0].is_declaration() {
|
if stmt.body.len() == 1 && !stmt.body[0].is_declaration() {
|
||||||
*stmt = block.body.remove(0);
|
return Some(stmt.body.remove(0));
|
||||||
self.compress_block(stmt, ctx);
|
|
||||||
self.changed = true;
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
if block.body.len() == 0
|
if stmt.body.len() == 0
|
||||||
&& (ctx.parent().is_while_statement()
|
&& (ctx.parent().is_while_statement()
|
||||||
|| ctx.parent().is_for_statement()
|
|| ctx.parent().is_for_statement()
|
||||||
|| ctx.parent().is_for_in_statement()
|
|| ctx.parent().is_for_in_statement()
|
||||||
|
|
@ -145,9 +144,9 @@ impl<'a, 'b> PeepholeRemoveDeadCode {
|
||||||
|| ctx.parent().is_program())
|
|| ctx.parent().is_program())
|
||||||
{
|
{
|
||||||
// Remove the block if it is empty and the parent is a block statement.
|
// Remove the block if it is empty and the parent is a block statement.
|
||||||
*stmt = ctx.ast.statement_empty(SPAN);
|
return Some(ctx.ast.statement_empty(stmt.span));
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
fn try_fold_if(
|
fn try_fold_if(
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue