Skip to content

Commit 29bd215

Browse files
Boshensapphi-red
andauthored
feat(minifier): minimize Infinity.toString(radix) to 'Infinity' (#8732)
Co-authored-by: sapphi-red <49056869+sapphi-red@users.noreply.github.com>
1 parent e0117db commit 29bd215

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

crates/oxc_minifier/src/peephole/replace_known_methods.rs

+6
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,10 @@ impl<'a> PeepholeOptimizations {
303303
}
304304
// Only convert integers for other radix values.
305305
let value = lit.value;
306+
if value.is_infinite() {
307+
let s = if value.is_sign_negative() { "-Infinity" } else { "Infinity" };
308+
return Some(ctx.ast.expression_string_literal(span, s, None));
309+
}
306310
if value.is_nan() {
307311
return Some(ctx.ast.expression_string_literal(span, "NaN", None));
308312
}
@@ -1540,6 +1544,8 @@ mod test {
15401544
test("x = NaN.toString()", "x = 'NaN';");
15411545
test("x = NaN.toString(2)", "x = 'NaN';");
15421546
test("x = Infinity.toString()", "x = 'Infinity';");
1547+
test("x = Infinity.toString(2)", "x = 'Infinity';");
1548+
test("x = (-Infinity).toString(2)", "x = '-Infinity';");
15431549
test("x = 1n.toString()", "x = '1'");
15441550
test_same("254n.toString(16);"); // unimplemented
15451551
// test("/a\\\\b/ig.toString()", "'/a\\\\\\\\b/ig';");

0 commit comments

Comments
 (0)