Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(minifier): move drop_use_strict_directives_if_function_is_empty to PeepholeRemoveDeadCode #8174

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() {}");
}
}
Loading