Skip to content

Commit 504c543

Browse files
committed
feat(minifier): minimize Infinity.toString(radix) to 'Infinity'
1 parent 9a39965 commit 504c543

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
@@ -290,6 +290,10 @@ impl<'a> PeepholeOptimizations {
290290
}
291291
// Only convert integers for other radix values.
292292
let value = lit.value;
293+
if value.is_infinite() {
294+
let s = if value.is_sign_negative() { "-Infinity" } else { "Infinity" };
295+
return Some(ctx.ast.expression_string_literal(ce.span, s, None));
296+
}
293297
if value.is_nan() {
294298
return Some(ctx.ast.expression_string_literal(ce.span, "NaN", None));
295299
}
@@ -1427,6 +1431,8 @@ mod test {
14271431
test("x = NaN.toString()", "x = 'NaN';");
14281432
test("x = NaN.toString(2)", "x = 'NaN';");
14291433
test("x = Infinity.toString()", "x = 'Infinity';");
1434+
test("x = Infinity.toString(2)", "x = 'Infinity';");
1435+
test("x = (-Infinity).toString(2)", "x = '-Infinity';");
14301436
test("x = 1n.toString()", "x = '1'");
14311437
test_same("254n.toString(16);"); // unimplemented
14321438
// test("/a\\\\b/ig.toString()", "'/a\\\\\\\\b/ig';");

0 commit comments

Comments
 (0)