Skip to content

Commit

Permalink
fix(minifier): omit dce undefined which can be a shadowed variable (#…
Browse files Browse the repository at this point in the history
…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`?
  • Loading branch information
Boshen authored Jul 6, 2024
1 parent adee728 commit 719fb96
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
3 changes: 2 additions & 1 deletion crates/oxc_minifier/src/compressor/ast_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions crates/oxc_minifier/tests/oxc/remove_dead_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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){}}",
);
}

0 comments on commit 719fb96

Please sign in to comment.