fix(minifier): omit dce undefined which can be a shadowed variable (#4073)

```
 // Shadowed `undefined` as a variable should not be erased.
    test(
        "function foo(undefined) { if (!undefined) { } }",
        "function foo(undefined){if(!undefined){}}",
    );
```

I'm not using the cheap `ident.reference_id.get().is_some()` here yet
because I don't know what I'm doing - how should minifier consume
`Semantic`?
This commit is contained in:
Boshen 2024-07-07 01:25:54 +08:00 committed by GitHub
parent adee7280d7
commit 719fb9672b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 8 additions and 1 deletions

View file

@ -485,7 +485,8 @@ pub fn get_boolean_value(expr: &Expression) -> Option<bool> {
.map(|cooked| !cooked.is_empty())
}
Expression::Identifier(ident) => {
if expr.is_undefined() || ident.name == "NaN" {
/* `undefined` can be a shadowed variable expr.is_undefined() || */
if ident.name == "NaN" {
Some(false)
} else if ident.name == "Infinity" {
Some(true)

View file

@ -41,4 +41,10 @@ fn remove_dead_code() {
test("!!false ? foo : bar;", "bar");
test("!!true ? foo : bar;", "foo");
// Shadowed `undefined` as a variable should not be erased.
test(
"function foo(undefined) { if (!undefined) { } }",
"function foo(undefined){if(!undefined){}}",
);
}