diff --git a/.changeset/lazy-ladybugs-jump.md b/.changeset/lazy-ladybugs-jump.md new file mode 100644 index 000000000000..9bde60378816 --- /dev/null +++ b/.changeset/lazy-ladybugs-jump.md @@ -0,0 +1,6 @@ +--- +swc_ecma_utils: patch +swc_core: patch +--- + +feat(es/minifier): Support unary negate in `cast_to_number` diff --git a/crates/swc_ecma_utils/src/lib.rs b/crates/swc_ecma_utils/src/lib.rs index a93096a861b5..3e5f07b0444d 100644 --- a/crates/swc_ecma_utils/src/lib.rs +++ b/crates/swc_ecma_utils/src/lib.rs @@ -933,17 +933,10 @@ pub trait ExprExt { op: op!(unary, "-"), arg, .. - }) if matches!( - &**arg, - Expr::Ident(Ident { - sym, - ctxt, - .. - }) if &**sym == "Infinity" && *ctxt == ctx.unresolved_ctxt - ) => - { - -f64::INFINITY - } + }) => match arg.cast_to_number(ctx) { + (Pure, Known(v)) => -v, + _ => return (MayBeImpure, Unknown), + }, Expr::Unary(UnaryExpr { op: op!("!"), ref arg,