diff --git a/Cargo.lock b/Cargo.lock index e698b322232e4..006762326f7ee 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1551,6 +1551,7 @@ dependencies = [ name = "oxc_minifier" version = "0.16.3" dependencies = [ + "insta", "itertools 0.13.0", "num-bigint", "num-traits", @@ -1564,6 +1565,7 @@ dependencies = [ "oxc_span", "oxc_syntax", "pico-args", + "walkdir", ] [[package]] diff --git a/crates/oxc_minifier/Cargo.toml b/crates/oxc_minifier/Cargo.toml index bfbe2d6f73688..c66741fb59dce 100644 --- a/crates/oxc_minifier/Cargo.toml +++ b/crates/oxc_minifier/Cargo.toml @@ -38,6 +38,6 @@ num-traits = { workspace = true } oxc_parser = { workspace = true } oxc_codegen = { workspace = true } -# insta = { workspace = true } -# walkdir = { workspace = true } +insta = { workspace = true } +walkdir = { workspace = true } pico-args = { workspace = true } diff --git a/crates/oxc_minifier/src/ast_passes/remove_dead_code.rs b/crates/oxc_minifier/src/ast_passes/remove_dead_code.rs index 9115e0ac1ecaf..bac62aa64a9ff 100644 --- a/crates/oxc_minifier/src/ast_passes/remove_dead_code.rs +++ b/crates/oxc_minifier/src/ast_passes/remove_dead_code.rs @@ -2,25 +2,34 @@ use oxc_allocator::Allocator; use oxc_ast::{ast::*, visit::walk_mut, AstBuilder, VisitMut}; use oxc_span::SPAN; +use crate::{compressor::ast_util::get_boolean_value, folder::Folder}; + /// Remove Dead Code from the AST. /// /// Terser option: `dead_code: true`. pub struct RemoveDeadCode<'a> { ast: AstBuilder<'a>, + folder: Folder<'a>, } impl<'a> RemoveDeadCode<'a> { pub fn new(allocator: &'a Allocator) -> Self { - Self { ast: AstBuilder::new(allocator) } + let ast = AstBuilder::new(allocator); + Self { ast, folder: Folder::new(ast) } } pub fn build(&mut self, program: &mut Program<'a>) { self.visit_program(program); } + fn test_expression(&mut self, expr: &mut Expression<'a>) -> Option { + self.folder.fold_expression(expr); + get_boolean_value(expr) + } + pub fn remove_if(&mut self, stmt: &mut Statement<'a>) { let Statement::IfStatement(if_stmt) = stmt else { return }; - match if_stmt.test.get_boolean_value() { + match self.test_expression(&mut if_stmt.test) { Some(true) => { *stmt = self.ast.move_statement(&mut if_stmt.consequent); } @@ -41,7 +50,7 @@ impl<'a> RemoveDeadCode<'a> { else { return; }; - match conditional_expr.test.get_boolean_value() { + match self.test_expression(&mut conditional_expr.test) { Some(true) => { expression_stmt.expression = self.ast.move_expression(&mut conditional_expr.consequent); diff --git a/crates/oxc_minifier/src/folder/mod.rs b/crates/oxc_minifier/src/folder/mod.rs index 03cc24701d2ed..cf9cf32eb241a 100644 --- a/crates/oxc_minifier/src/folder/mod.rs +++ b/crates/oxc_minifier/src/folder/mod.rs @@ -40,7 +40,7 @@ impl<'a> Folder<'a> { self } - pub(crate) fn fold_expression<'b>(&mut self, expr: &'b mut Expression<'a>) { + pub fn fold_expression<'b>(&mut self, expr: &'b mut Expression<'a>) { let folded_expr = match expr { Expression::BinaryExpression(binary_expr) => match binary_expr.operator { BinaryOperator::Equality diff --git a/crates/oxc_minifier/tests/mod.rs.off b/crates/oxc_minifier/tests/mod.rs similarity index 97% rename from crates/oxc_minifier/tests/mod.rs.off rename to crates/oxc_minifier/tests/mod.rs index 94578b542d502..c156e0408cb0c 100644 --- a/crates/oxc_minifier/tests/mod.rs.off +++ b/crates/oxc_minifier/tests/mod.rs @@ -1,8 +1,9 @@ -mod closure; -mod esbuild; +#![allow(unused)] +// mod closure; +// mod esbuild; mod oxc; -mod tdewolff; -mod terser; +// mod tdewolff; +// mod terser; use oxc_allocator::Allocator; use oxc_codegen::WhitespaceRemover; diff --git a/crates/oxc_minifier/tests/oxc/mod.rs b/crates/oxc_minifier/tests/oxc/mod.rs index ecb9973b78d86..6542a6737ee32 100644 --- a/crates/oxc_minifier/tests/oxc/mod.rs +++ b/crates/oxc_minifier/tests/oxc/mod.rs @@ -1,5 +1,5 @@ mod code_removal; mod folding; -mod precedence; +// mod precedence; mod remove_dead_code; mod replace_global_defines; diff --git a/crates/oxc_minifier/tests/oxc/remove_dead_code.rs b/crates/oxc_minifier/tests/oxc/remove_dead_code.rs index 09f50d3fce834..fec6508ecef46 100644 --- a/crates/oxc_minifier/tests/oxc/remove_dead_code.rs +++ b/crates/oxc_minifier/tests/oxc/remove_dead_code.rs @@ -24,6 +24,21 @@ fn remove_dead_code() { test("if (true) { foo } else { bar }", "{foo}"); test("if (false) { foo } else { bar }", "{bar}"); + test("if (!false) { foo }", "{foo}"); + test("if (!true) { foo } else { bar }", "{bar}"); + + test("if ('production' == 'production') { foo } else { bar }", "{foo}"); + test("if ('development' == 'production') { foo } else { bar }", "{bar}"); + + test("if ('production' === 'production') { foo } else { bar }", "{foo}"); + test("if ('development' === 'production') { foo } else { bar }", "{bar}"); + test("false ? foo : bar;", "bar"); test("true ? foo : bar;", "foo"); + + test("!true ? foo : bar;", "bar"); + test("!false ? foo : bar;", "foo"); + + test("!!false ? foo : bar;", "bar"); + test("!!true ? foo : bar;", "foo"); }