diff --git a/Cargo.toml b/Cargo.toml index af0f220c31619..5a8c6a048b8d6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -200,8 +200,8 @@ oxc_macros.opt-level = 1 oxc_ast_macros.opt-level = 1 # Compile insta and its dependencies in release mode for faster snapshot tests # See: https://insta.rs/docs/quickstart/#optional-faster-runs -insta.opt-level = 3 -similar.opt-level = 3 +insta.opt-level = 3 +similar.opt-level = 3 [profile.release.package.oxc_wasm] opt-level = 'z' diff --git a/crates/oxc_minifier/examples/minifier.rs b/crates/oxc_minifier/examples/minifier.rs index f917626398094..844c9db9073ea 100644 --- a/crates/oxc_minifier/examples/minifier.rs +++ b/crates/oxc_minifier/examples/minifier.rs @@ -2,7 +2,7 @@ use std::path::Path; use oxc_allocator::Allocator; -use oxc_codegen::{CodeGenerator, WhitespaceRemover}; +use oxc_codegen::CodeGenerator; use oxc_minifier::{CompressOptions, Minifier, MinifierOptions}; use oxc_parser::Parser; use oxc_span::SourceType; @@ -17,34 +17,28 @@ fn main() -> std::io::Result<()> { let name = args.subcommand().ok().flatten().unwrap_or_else(|| String::from("test.js")); let mangle = args.contains("--mangle"); - let whitespace = args.contains("--whitespace"); let twice = args.contains("--twice"); let path = Path::new(&name); let source_text = std::fs::read_to_string(path)?; let source_type = SourceType::from_path(path).unwrap(); - let printed = minify(&source_text, source_type, mangle, whitespace); + let printed = minify(&source_text, source_type, mangle); println!("{printed}"); if twice { - let printed = minify(&printed, source_type, mangle, whitespace); + let printed = minify(&printed, source_type, mangle); println!("{printed}"); } Ok(()) } -fn minify(source_text: &str, source_type: SourceType, mangle: bool, whitespace: bool) -> String { +fn minify(source_text: &str, source_type: SourceType, mangle: bool) -> String { let allocator = Allocator::default(); let ret = Parser::new(&allocator, source_text, source_type).parse(); let program = allocator.alloc(ret.program); let options = MinifierOptions { mangle, compress: CompressOptions::all_true() }; let ret = Minifier::new(options).build(&allocator, program); - if whitespace { - CodeGenerator::new().with_mangler(ret.mangler).build(program) - } else { - WhitespaceRemover::new().with_mangler(ret.mangler).build(program) - } - .source_text + CodeGenerator::new().with_mangler(ret.mangler).build(program).source_text } 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 2d6b708235093..4e1b97784effc 100644 --- a/crates/oxc_minifier/src/ast_passes/remove_dead_code.rs +++ b/crates/oxc_minifier/src/ast_passes/remove_dead_code.rs @@ -58,7 +58,7 @@ impl<'a> RemoveDeadCode<'a> { let mut keep_var = KeepVar::new(self.ast); - for stmt in stmts.iter().skip(index) { + for stmt in stmts.iter().skip(index + 1) { keep_var.visit_statement(stmt); } diff --git a/crates/oxc_minifier/src/keep_var.rs b/crates/oxc_minifier/src/keep_var.rs index 9627803afb2d6..7f698345ec3b9 100644 --- a/crates/oxc_minifier/src/keep_var.rs +++ b/crates/oxc_minifier/src/keep_var.rs @@ -21,6 +21,8 @@ impl<'a> Visit<'a> for KeepVar<'a> { /* skip functions */ } + fn visit_arrow_function_expression(&mut self, _it: &ArrowFunctionExpression<'a>) {} + fn visit_class(&mut self, _it: &Class<'a>) { /* skip classes */ } diff --git a/crates/oxc_minifier/tests/oxc/remove_dead_code.rs b/crates/oxc_minifier/tests/oxc/remove_dead_code.rs index 79fd1702f9a60..efd59c59170af 100644 --- a/crates/oxc_minifier/tests/oxc/remove_dead_code.rs +++ b/crates/oxc_minifier/tests/oxc/remove_dead_code.rs @@ -18,12 +18,18 @@ fn print(source_text: &str, remove_dead_code: bool) -> String { .source_text } -pub(crate) fn test(source_text: &str, expected: &str) { +fn test(source_text: &str, expected: &str) { let minified = print(source_text, true); let expected = print(expected, false); assert_eq!(minified, expected, "for source {source_text}"); } +fn test_same(source_text: &str) { + let minified = print(source_text, true); + let expected = print(source_text, false); + assert_eq!(minified, expected, "for source {source_text}"); +} + #[test] fn dce_if_statement() { test("if (true) { foo }", "{ foo }"); @@ -119,6 +125,24 @@ fn dce_logical_expression() { test("const foo = true && bar()", "const foo = bar()"); } +#[test] +fn dce_var_hoisting() { + test_same( + "function f() { + return () => { + var x; + } + }", + ); + test_same( + "function f() { + return function g() { + var x; + } + }", + ); +} + // https://github.com/terser/terser/blob/master/test/compress/dead-code.js #[test] fn dce_from_terser() { diff --git a/tasks/benchmark/Cargo.toml b/tasks/benchmark/Cargo.toml index 925a950a9268a..59b4040f65b93 100644 --- a/tasks/benchmark/Cargo.toml +++ b/tasks/benchmark/Cargo.toml @@ -111,13 +111,7 @@ lexer = ["dep:oxc_allocator", "dep:oxc_parser", "dep:oxc_span", "dep:oxc_tasks_c parser = ["dep:oxc_allocator", "dep:oxc_parser", "dep:oxc_span", "dep:oxc_tasks_common"] transformer = ["dep:oxc_allocator", "dep:oxc_parser", "dep:oxc_span", "dep:oxc_tasks_common", "dep:oxc_transformer"] semantic = ["dep:oxc_allocator", "dep:oxc_parser", "dep:oxc_semantic", "dep:oxc_span", "dep:oxc_tasks_common"] -minifier = [ - "dep:oxc_allocator", - "dep:oxc_minifier", - "dep:oxc_parser", - "dep:oxc_span", - "dep:oxc_tasks_common", -] +minifier = ["dep:oxc_allocator", "dep:oxc_minifier", "dep:oxc_parser", "dep:oxc_span", "dep:oxc_tasks_common"] codegen = ["dep:oxc_allocator", "dep:oxc_codegen", "dep:oxc_parser", "dep:oxc_span", "dep:oxc_tasks_common"] sourcemap = [ "dep:oxc_allocator",