mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 04:08:41 +00:00
fix(minifier): fix dce shadowed undefined (#8582)
This commit is contained in:
parent
7421a52f7c
commit
7b219a930f
3 changed files with 12 additions and 22 deletions
|
|
@ -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;
|
||||
}
|
||||
|
|
@ -124,10 +124,7 @@ pub trait ConstantEvaluation<'a> {
|
|||
_ => None,
|
||||
}
|
||||
}
|
||||
expr => {
|
||||
use crate::ToBoolean;
|
||||
expr.to_boolean()
|
||||
}
|
||||
expr => expr.to_boolean(),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
@ -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(
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue