feat(minifier): minimize Infinity.toString(radix) to 'Infinity' (#8732)

Co-authored-by: sapphi-red <49056869+sapphi-red@users.noreply.github.com>
This commit is contained in:
Boshen 2025-01-26 22:14:24 +08:00 committed by GitHub
parent e0117db531
commit 29bd215f4f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -303,6 +303,10 @@ impl<'a> PeepholeOptimizations {
}
// Only convert integers for other radix values.
let value = lit.value;
if value.is_infinite() {
let s = if value.is_sign_negative() { "-Infinity" } else { "Infinity" };
return Some(ctx.ast.expression_string_literal(span, s, None));
}
if value.is_nan() {
return Some(ctx.ast.expression_string_literal(span, "NaN", None));
}
@ -1540,6 +1544,8 @@ mod test {
test("x = NaN.toString()", "x = 'NaN';");
test("x = NaN.toString(2)", "x = 'NaN';");
test("x = Infinity.toString()", "x = 'Infinity';");
test("x = Infinity.toString(2)", "x = 'Infinity';");
test("x = (-Infinity).toString(2)", "x = '-Infinity';");
test("x = 1n.toString()", "x = '1'");
test_same("254n.toString(16);"); // unimplemented
// test("/a\\\\b/ig.toString()", "'/a\\\\\\\\b/ig';");