Skip to content

Commit

Permalink
fix(minifier): !!x is not idempotent in RemoveDeadCode
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Jan 10, 2025
1 parent 0550e81 commit 3c0501e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1951,6 +1951,7 @@ mod test {

#[test]
fn minimize_duplicate_nots() {
// test("!x", "x"); // TODO: in ExpressionStatement
test("!!x", "x");
test("!!!x", "!x");
test("!!!!x", "x");
Expand Down
18 changes: 10 additions & 8 deletions crates/oxc_minifier/src/ast_passes/peephole_remove_dead_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,12 +341,12 @@ impl<'a, 'b> PeepholeRemoveDeadCode {
{
Some(ctx.ast.statement_empty(SPAN))
}
// `typeof x.y` -> `x`, `!x` -> `x`, `void x` -> `x`...
// `typeof x.y` -> `x.y`, `void x` -> `x`
// `+0n` -> `Uncaught TypeError: Cannot convert a BigInt value to a number`
Expression::UnaryExpression(unary_expr)
if !matches!(
if matches!(
unary_expr.operator,
UnaryOperator::Delete | UnaryOperator::UnaryPlus
UnaryOperator::Typeof | UnaryOperator::Void
) =>
{
Some(ctx.ast.statement_expression(
Expand Down Expand Up @@ -700,11 +700,13 @@ mod test {
fold("void x?.y", "x?.y");
fold("void x.y", "x.y");
fold("void x.y.z()", "x.y.z()");
fold("!x", "x");
fold("!x?.y", "x?.y");
fold("!x.y", "x.y");
fold("!x.y.z()", "x.y.z()");
fold("-x.y.z()", "x.y.z()");

// Removed in `MinimizeConditions`, to keep this pass idempotent for DCE.
fold_same("!x");
fold_same("!x?.y");
fold_same("!x.y");
fold_same("!x.y.z()");
fold_same("-x.y.z()");

fold_same("delete x");
fold_same("delete x.y");
Expand Down

0 comments on commit 3c0501e

Please sign in to comment.