Skip to content

Commit

Permalink
fix(minifier): move drop_use_strict_directives_if_function_is_empty t…
Browse files Browse the repository at this point in the history
…o PeepholeRemoveDeadCode
  • Loading branch information
sapphi-red authored and Boshen committed Dec 29, 2024
1 parent 48c015e commit ff48152
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 16 deletions.
9 changes: 9 additions & 0 deletions crates/oxc_minifier/src/ast_passes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ impl<'a> Traverse<'a> for LatePeepholeOptimizations {

fn exit_function_body(&mut self, body: &mut FunctionBody<'a>, ctx: &mut TraverseCtx<'a>) {
self.x0_statement_fusion.exit_function_body(body, ctx);
self.x2_peephole_remove_dead_code.exit_function_body(body, ctx);
}

fn exit_statements(&mut self, stmts: &mut Vec<'a, Statement<'a>>, ctx: &mut TraverseCtx<'a>) {
Expand Down Expand Up @@ -232,6 +233,10 @@ impl<'a> Traverse<'a> for PeepholeOptimizations {
self.x5_peephole_remove_dead_code.exit_program(program, ctx);
}

fn exit_function_body(&mut self, body: &mut FunctionBody<'a>, ctx: &mut TraverseCtx<'a>) {
self.x5_peephole_remove_dead_code.exit_function_body(body, ctx);
}

fn exit_statements(&mut self, stmts: &mut Vec<'a, Statement<'a>>, ctx: &mut TraverseCtx<'a>) {
self.x2_peephole_minimize_conditions.exit_statements(stmts, ctx);
self.x5_peephole_remove_dead_code.exit_statements(stmts, ctx);
Expand Down Expand Up @@ -300,6 +305,10 @@ impl<'a> Traverse<'a> for DeadCodeElimination {
self.x2_peephole_remove_dead_code.exit_program(program, ctx);
}

fn exit_function_body(&mut self, body: &mut FunctionBody<'a>, ctx: &mut TraverseCtx<'a>) {
self.x2_peephole_remove_dead_code.exit_function_body(body, ctx);
}

fn exit_statements(&mut self, stmts: &mut Vec<'a, Statement<'a>>, ctx: &mut TraverseCtx<'a>) {
self.x2_peephole_remove_dead_code.exit_statements(stmts, ctx);
}
Expand Down
19 changes: 19 additions & 0 deletions crates/oxc_minifier/src/ast_passes/peephole_remove_dead_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ impl<'a> CompressorPass<'a> for PeepholeRemoveDeadCode {
}

impl<'a> Traverse<'a> for PeepholeRemoveDeadCode {
fn exit_function_body(&mut self, body: &mut FunctionBody<'a>, ctx: &mut TraverseCtx<'a>) {
Self::drop_use_strict_directives_if_function_is_empty(body, ctx);
}

fn exit_statement(&mut self, stmt: &mut Statement<'a>, ctx: &mut TraverseCtx<'a>) {
let ctx = Ctx(ctx);
if let Some(new_stmt) = match stmt {
Expand Down Expand Up @@ -124,6 +128,16 @@ impl<'a, 'b> PeepholeRemoveDeadCode {
}
}

/// Drop `"use strict";` directives if the function is empty.
fn drop_use_strict_directives_if_function_is_empty(
body: &mut FunctionBody<'a>,
_ctx: &mut TraverseCtx<'a>,
) {
if body.statements.is_empty() {
body.directives.retain(|directive| !directive.is_use_strict());
}
}

/// Remove block from single line blocks
/// `{ block } -> block`
fn try_optimize_block(
Expand Down Expand Up @@ -436,6 +450,11 @@ mod test {
test(js, expected);
}

#[test]
fn use_strict() {
test("function foo() { 'use strict';}", "function foo() {}");
}

#[test]
fn test_fold_block() {
fold("{{foo()}}", "foo()");
Expand Down
16 changes: 0 additions & 16 deletions crates/oxc_minifier/src/ast_passes/remove_syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ impl<'a> Traverse<'a> for RemoveSyntax {
Self::drop_use_strict_directives_in_function_body(body, ctx);
}

fn exit_function_body(&mut self, body: &mut FunctionBody<'a>, ctx: &mut TraverseCtx<'a>) {
Self::drop_use_strict_directives_if_function_is_empty(body, ctx);
}

fn exit_statements(&mut self, stmts: &mut Vec<'a, Statement<'a>>, _ctx: &mut TraverseCtx<'a>) {
stmts.retain(|stmt| {
!(matches!(stmt, Statement::EmptyStatement(_))
Expand Down Expand Up @@ -123,16 +119,6 @@ impl<'a> RemoveSyntax {
body.directives.retain(|directive| !directive.is_use_strict());
}
}

/// Drop `"use strict";` directives if the function is empty.
fn drop_use_strict_directives_if_function_is_empty(
body: &mut FunctionBody<'a>,
_ctx: &mut TraverseCtx<'a>,
) {
if body.statements.is_empty() {
body.directives.retain(|directive| !directive.is_use_strict());
}
}
}

#[cfg(test)]
Expand Down Expand Up @@ -205,7 +191,5 @@ mod test {
"const Foo = class { foo() { 'use strict'; alert(1); } } ",
"const Foo = class { foo() { alert(1); } } ",
);

test_script("function foo() { 'use strict';}", "function foo() {}");
}
}

0 comments on commit ff48152

Please sign in to comment.