Skip to content

Commit

Permalink
fix(minifier): fix dce shadowed undefined (#8582)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Jan 18, 2025
1 parent 7421a52 commit 7b219a9
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 22 deletions.
7 changes: 2 additions & 5 deletions crates/oxc_ecmascript/src/constant_evaluation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub trait ConstantEvaluation<'a> {
}

fn get_side_free_boolean_value(&self, expr: &Expression<'a>) -> Option<bool> {
let value = expr.to_boolean();
let value = self.get_boolean_value(expr);
if value.is_some() && !expr.may_have_side_effects() {
return value;
}
Expand Down Expand Up @@ -124,10 +124,7 @@ pub trait ConstantEvaluation<'a> {
_ => None,
}
}
expr => {
use crate::ToBoolean;
expr.to_boolean()
}
expr => expr.to_boolean(),
}
}

Expand Down
23 changes: 8 additions & 15 deletions crates/oxc_minifier/tests/ast_passes/dead_code_elimination.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<CompressOptions>) -> 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
}

Expand All @@ -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}");
}

Expand Down Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions tasks/minsize/minsize.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 7b219a9

Please sign in to comment.