From 7b219a930fa34d9b55c8a9817831d90ab265ff8f Mon Sep 17 00:00:00 2001 From: Boshen <1430279+Boshen@users.noreply.github.com> Date: Sat, 18 Jan 2025 09:04:28 +0000 Subject: [PATCH] fix(minifier): fix dce shadowed undefined (#8582) --- .../src/constant_evaluation/mod.rs | 7 ++---- .../tests/ast_passes/dead_code_elimination.rs | 23 +++++++------------ tasks/minsize/minsize.snap | 4 ++-- 3 files changed, 12 insertions(+), 22 deletions(-) diff --git a/crates/oxc_ecmascript/src/constant_evaluation/mod.rs b/crates/oxc_ecmascript/src/constant_evaluation/mod.rs index 17fb393c33ee5..22ba2b37e9da0 100644 --- a/crates/oxc_ecmascript/src/constant_evaluation/mod.rs +++ b/crates/oxc_ecmascript/src/constant_evaluation/mod.rs @@ -52,7 +52,7 @@ pub trait ConstantEvaluation<'a> { } fn get_side_free_boolean_value(&self, expr: &Expression<'a>) -> Option { - let value = expr.to_boolean(); + let value = self.get_boolean_value(expr); if value.is_some() && !expr.may_have_side_effects() { return value; } @@ -124,10 +124,7 @@ pub trait ConstantEvaluation<'a> { _ => None, } } - expr => { - use crate::ToBoolean; - expr.to_boolean() - } + expr => expr.to_boolean(), } } diff --git a/crates/oxc_minifier/tests/ast_passes/dead_code_elimination.rs b/crates/oxc_minifier/tests/ast_passes/dead_code_elimination.rs index 595bcabc721d9..5a1e43ff6e16e 100644 --- a/crates/oxc_minifier/tests/ast_passes/dead_code_elimination.rs +++ b/crates/oxc_minifier/tests/ast_passes/dead_code_elimination.rs @@ -7,11 +7,13 @@ use oxc_minifier::Compressor; use oxc_parser::Parser; use oxc_span::SourceType; -fn run(source_text: &str, source_type: SourceType) -> String { +fn run(source_text: &str, source_type: SourceType, options: Option) -> String { let allocator = Allocator::default(); let mut ret = Parser::new(&allocator, source_text, source_type).parse(); let program = &mut ret.program; - Compressor::new(&allocator, CompressOptions::default()).dead_code_elimination(program); + if let Some(options) = options { + Compressor::new(&allocator, options).dead_code_elimination(program); + } Codegen::new().build(program).code } @@ -22,8 +24,8 @@ fn test(source_text: &str, expected: &str) { let source_text = source_text.cow_replace("false", f); let source_type = SourceType::default(); - let result = run(&source_text, source_type); - let expected = run(expected, source_type); + let result = run(&source_text, source_type, Some(CompressOptions::default())); + let expected = run(expected, source_type, None); assert_eq!(result, expected, "\nfor source\n{source_text}\nexpect\n{expected}\ngot\n{result}"); } @@ -78,22 +80,13 @@ fn dce_if_statement() { // Shadowed `undefined` as a variable should not be erased. // This is a rollup test. - test_same("function foo(undefined) { if (!undefined) { } }"); + test_same("function foo(undefined) { if (!undefined) foo }"); test("function foo() { if (undefined) { bar } }", "function foo() { }"); test("function foo() { { bar } }", "function foo() { bar }"); test("if (true) { foo; } if (true) { foo; }", "foo; foo;"); - - test( - " - if (true) { foo; return } - foo; - if (true) { bar; return } - bar; - ", - "{foo; return }", - ); + test("if (true) { foo; return } foo; if (true) { bar; return } bar;", "{ foo; return }"); // nested expression test( diff --git a/tasks/minsize/minsize.snap b/tasks/minsize/minsize.snap index 302461b5413ba..33dbadc624ac7 100644 --- a/tasks/minsize/minsize.snap +++ b/tasks/minsize/minsize.snap @@ -21,7 +21,7 @@ Original | minified | minified | gzip | gzip | Fixture 3.20 MB | 1.01 MB | 1.01 MB | 332.01 kB | 331.56 kB | echarts.js -6.69 MB | 2.32 MB | 2.31 MB | 492.44 kB | 488.28 kB | antd.js +6.69 MB | 2.31 MB | 2.31 MB | 492.51 kB | 488.28 kB | antd.js -10.95 MB | 3.49 MB | 3.49 MB | 907.07 kB | 915.50 kB | typescript.js +10.95 MB | 3.49 MB | 3.49 MB | 907.24 kB | 915.50 kB | typescript.js