Skip to content

Commit 539ef75

Browse files
committed
fix(minifier): (-Infinity).toString() -> '-Infinity' (#8535)
1 parent 927f43f commit 539ef75

File tree

2 files changed

+11
-18
lines changed

2 files changed

+11
-18
lines changed

crates/oxc_minifier/src/ast_passes/peephole_replace_known_methods.rs

+2-18
Original file line numberDiff line numberDiff line change
@@ -294,19 +294,10 @@ impl<'a> PeepholeReplaceKnownMethods {
294294
if radix == 0 {
295295
return None;
296296
}
297-
if lit.value.is_nan() {
298-
return Some(ctx.ast.expression_string_literal(ce.span, "NaN", None));
299-
}
300-
if lit.value.is_infinite() {
301-
return Some(ctx.ast.expression_string_literal(ce.span, "Infinity", None));
302-
}
303297
if radix == 10 {
304298
use oxc_syntax::number::ToJsString;
305-
return Some(ctx.ast.expression_string_literal(
306-
ce.span,
307-
lit.value.to_js_string(),
308-
None,
309-
));
299+
let s = lit.value.to_js_string();
300+
return Some(ctx.ast.expression_string_literal(ce.span, s, None));
310301
}
311302
// Only convert integers for other radix values.
312303
let value = lit.value;
@@ -1244,12 +1235,5 @@ mod test {
12441235
test("123 .toString(b)", "123 .toString(b)");
12451236
test("1e99.toString(b)", "1e99.toString(b)");
12461237
test("/./.toString(b)", "/./.toString(b)");
1247-
1248-
// Will get constant folded into positive values
1249-
test_same("(-0).toString()");
1250-
test_same("(-123).toString()");
1251-
test_same("(-Infinity).toString()");
1252-
test_same("(-1000000).toString(36)");
1253-
test_same("(-0).toString(36)");
12541238
}
12551239
}

crates/oxc_minifier/tests/ast_passes/mod.rs

+9
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,15 @@ fn integration() {
7777
);
7878
}
7979

80+
#[test]
81+
fn fold() {
82+
test("var x = (-0).toString()", "var x = '0'");
83+
test("var x = (-0).toString(36)", "var x = '0'");
84+
test("var x = (-123).toString()", "var x = '-123'");
85+
test("var x = (-Infinity).toString()", "var x = '-Infinity'");
86+
test("var x = (-1000000).toString(36)", "var x = (-1e6).toString(36)");
87+
}
88+
8089
#[test] // https://github.com/oxc-project/oxc/issues/4341
8190
fn tagged_template() {
8291
test_same("(1, o.f)()");

0 commit comments

Comments
 (0)