mirror of
https://github.com/danbulant/oxc
synced 2026-05-20 20:58:48 +00:00
fix(minifier): do not remove undefined in var x = undefined (#8526)
This commit is contained in:
parent
06f14d526b
commit
92e44cba6b
1 changed files with 7 additions and 5 deletions
|
|
@ -665,7 +665,9 @@ impl<'a, 'b> PeepholeSubstituteAlternateSyntax {
|
||||||
if decl.kind.is_const() || decl.id.kind.is_destructuring_pattern() {
|
if decl.kind.is_const() || decl.id.kind.is_destructuring_pattern() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if decl.init.as_ref().is_some_and(|init| ctx.is_expression_undefined(init)) {
|
if !decl.kind.is_var()
|
||||||
|
&& decl.init.as_ref().is_some_and(|init| ctx.is_expression_undefined(init))
|
||||||
|
{
|
||||||
decl.init = None;
|
decl.init = None;
|
||||||
self.changed = true;
|
self.changed = true;
|
||||||
}
|
}
|
||||||
|
|
@ -1281,17 +1283,17 @@ mod test {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_undefined() {
|
fn test_undefined() {
|
||||||
test("var x = undefined", "var x");
|
test("let x = undefined", "let x");
|
||||||
|
test("const x = undefined", "const x = void 0");
|
||||||
|
test("var x = undefined", "var x = void 0");
|
||||||
test_same("var undefined = 1;function f() {var undefined=2;var x;}");
|
test_same("var undefined = 1;function f() {var undefined=2;var x;}");
|
||||||
test_same("function f(undefined) {}");
|
test_same("function f(undefined) {}");
|
||||||
test_same("try {} catch(undefined) {foo(undefined)}");
|
test_same("try {} catch(undefined) {foo(undefined)}");
|
||||||
test("for (undefined in {}) {}", "for(undefined in {}){}");
|
test("for (undefined in {}) {}", "for(undefined in {}){}");
|
||||||
test("undefined++;", "undefined++");
|
test("undefined++;", "undefined++");
|
||||||
test("undefined += undefined;", "undefined+=void 0");
|
test("undefined += undefined;", "undefined+=void 0");
|
||||||
|
// shadowed
|
||||||
// shadowd
|
|
||||||
test_same("(function(undefined) { let x = typeof undefined; })()");
|
test_same("(function(undefined) { let x = typeof undefined; })()");
|
||||||
|
|
||||||
// destructuring throw error side effect
|
// destructuring throw error side effect
|
||||||
test_same("var {} = void 0");
|
test_same("var {} = void 0");
|
||||||
test_same("var [] = void 0");
|
test_same("var [] = void 0");
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue