From 9be29af9d45cb176fa3b3cb10da899c50867cb3d Mon Sep 17 00:00:00 2001 From: Boshen <1430279+Boshen@users.noreply.github.com> Date: Tue, 6 Aug 2024 04:58:39 +0000 Subject: [PATCH] fix(minifier): temporarily fix shadowed `undefined` variable (#4678) --- crates/oxc_minifier/src/ast_util.rs | 15 ++++++--------- crates/oxc_minifier/tests/oxc/remove_dead_code.rs | 9 +++++---- tasks/coverage/minifier_test262.snap | 6 +++++- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/crates/oxc_minifier/src/ast_util.rs b/crates/oxc_minifier/src/ast_util.rs index ba10b60412282..2e1eab349e3c6 100644 --- a/crates/oxc_minifier/src/ast_util.rs +++ b/crates/oxc_minifier/src/ast_util.rs @@ -484,15 +484,12 @@ pub fn get_boolean_value(expr: &Expression) -> Option { .and_then(|quasi| quasi.value.cooked.as_ref()) .map(|cooked| !cooked.is_empty()) } - Expression::Identifier(ident) => { - if expr.is_undefined() || ident.name == "NaN" { - Some(false) - } else if ident.name == "Infinity" { - Some(true) - } else { - None - } - } + Expression::Identifier(ident) => match ident.name.as_str() { + "NaN" => Some(false), + "Infinity" => Some(true), + // "undefined" if ident.reference_id.get().is_none() => Some(false), + _ => None, + }, Expression::AssignmentExpression(assign_expr) => { match assign_expr.operator { AssignmentOperator::LogicalAnd | AssignmentOperator::LogicalOr => None, diff --git a/crates/oxc_minifier/tests/oxc/remove_dead_code.rs b/crates/oxc_minifier/tests/oxc/remove_dead_code.rs index 79456fb6fc4d1..96f17019c5f17 100644 --- a/crates/oxc_minifier/tests/oxc/remove_dead_code.rs +++ b/crates/oxc_minifier/tests/oxc/remove_dead_code.rs @@ -70,10 +70,11 @@ fn dce_if_statement() { test("if ('development' === 'production') { foo } else { bar }", "{ bar }"); // Shadowed `undefined` as a variable should not be erased. - // test( - // "function foo(undefined) { if (!undefined) { } }", - // "function foo(undefined) { if (!undefined) { } }", - // ); + // This is a rollup test. + test( + "function foo(undefined) { if (!undefined) { } }", + "function foo(undefined) { if (!undefined) { } }", + ); test("if (true) { foo; } if (true) { foo; }", "{ foo; } { foo; }"); diff --git a/tasks/coverage/minifier_test262.snap b/tasks/coverage/minifier_test262.snap index 6f4e52f07c749..66336a0659aac 100644 --- a/tasks/coverage/minifier_test262.snap +++ b/tasks/coverage/minifier_test262.snap @@ -2,4 +2,8 @@ commit: a1587416 minifier_test262 Summary: AST Parsed : 46406/46406 (100.00%) -Positive Passed: 46406/46406 (100.00%) +Positive Passed: 46402/46406 (99.99%) +Expect to Parse: "language/expressions/logical-and/S11.11.1_A3_T4.js" +Expect to Parse: "language/expressions/logical-not/S9.2_A1_T2.js" +Expect to Parse: "language/statements/if/S12.5_A1.1_T1.js" +Expect to Parse: "language/statements/if/S12.5_A1.1_T2.js"