Skip to content

Commit

Permalink
fix(ecmascript): incorrect to_int_32 value for Infinity (#8144)
Browse files Browse the repository at this point in the history
https://tc39.es/ecma262/#sec-toint32
Infinity should return `0` when passed to `ToInt32` abstract operation.

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
  • Loading branch information
sapphi-red and autofix-ci[bot] authored Dec 27, 2024
1 parent 6615e1e commit 74572de
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
5 changes: 5 additions & 0 deletions crates/oxc_ecmascript/src/to_int_32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ impl ToInt32 for f64 {

let number = *self;

// NOTE: this also matches with negative zero
if !number.is_finite() || number == 0.0 {
return 0;
}

if number.is_finite() && number <= f64::from(i32::MAX) && number >= f64::from(i32::MIN) {
let i = number as i32;
if f64::from(i) == number {
Expand Down
6 changes: 3 additions & 3 deletions crates/oxc_minifier/src/ast_passes/peephole_fold_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1545,9 +1545,9 @@ mod test {

#[test]
fn test_fold_left_child_op() {
test_same("x & infinity & 2"); // FIXME: want x & 0
test_same("x - infinity - 2"); // FIXME: want "x-infinity"
test_same("x - 1 + infinity");
test("x & Infinity & 2", "x & 0");
test_same("x - Infinity - 2"); // FIXME: want "x-Infinity"
test_same("x - 1 + Infinity");
test_same("x - 2 + 1");
test_same("x - 2 + 3");
test_same("1 + x - 2 + 1");
Expand Down

0 comments on commit 74572de

Please sign in to comment.