Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(minifier): (-Infinity).toString() -> '-Infinity' #8535

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -294,19 +294,10 @@ impl<'a> PeepholeReplaceKnownMethods {
if radix == 0 {
return None;
}
if lit.value.is_nan() {
return Some(ctx.ast.expression_string_literal(ce.span, "NaN", None));
}
if lit.value.is_infinite() {
return Some(ctx.ast.expression_string_literal(ce.span, "Infinity", None));
}
if radix == 10 {
use oxc_syntax::number::ToJsString;
return Some(ctx.ast.expression_string_literal(
ce.span,
lit.value.to_js_string(),
None,
));
let s = lit.value.to_js_string();
return Some(ctx.ast.expression_string_literal(ce.span, s, None));
}
// Only convert integers for other radix values.
let value = lit.value;
Expand Down Expand Up @@ -1244,12 +1235,5 @@ mod test {
test("123 .toString(b)", "123 .toString(b)");
test("1e99.toString(b)", "1e99.toString(b)");
test("/./.toString(b)", "/./.toString(b)");

// Will get constant folded into positive values
test_same("(-0).toString()");
test_same("(-123).toString()");
test_same("(-Infinity).toString()");
test_same("(-1000000).toString(36)");
test_same("(-0).toString(36)");
}
}
9 changes: 9 additions & 0 deletions crates/oxc_minifier/tests/ast_passes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@ fn integration() {
);
}

#[test]
fn fold() {
test("var x = (-0).toString()", "var x = '0'");
test("var x = (-0).toString(36)", "var x = '0'");
test("var x = (-123).toString()", "var x = '-123'");
test("var x = (-Infinity).toString()", "var x = '-Infinity'");
test("var x = (-1000000).toString(36)", "var x = (-1e6).toString(36)");
}

#[test] // https://github.com/oxc-project/oxc/issues/4341
fn tagged_template() {
test_same("(1, o.f)()");
Expand Down
Loading