Skip to content

Commit

Permalink
fix(es/minifier): Fix comparison of -0.0 and 0 (#8973)
Browse files Browse the repository at this point in the history
**Related issue:**

 - Closes #8972
  • Loading branch information
kdy1 authored May 24, 2024
1 parent eaffeb8 commit 2a43df4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
18 changes: 15 additions & 3 deletions crates/swc_ecma_minifier/src/compress/util/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::f64;
use std::{cmp::Ordering, f64};

use swc_common::{util::take::Take, DUMMY_SP};
use swc_ecma_ast::*;
Expand Down Expand Up @@ -486,7 +486,7 @@ pub(crate) fn eval_as_number(expr_ctx: &ExprCtx, e: &Expr) -> Option<f64> {
return Some(
numbers
.into_iter()
.max_by(|a, b| a.partial_cmp(b).unwrap())
.max_by(|&a, &b| cmp_num(a, b))
.unwrap_or(f64::NEG_INFINITY),
);
}
Expand All @@ -504,7 +504,7 @@ pub(crate) fn eval_as_number(expr_ctx: &ExprCtx, e: &Expr) -> Option<f64> {
return Some(
numbers
.into_iter()
.min_by(|a, b| a.partial_cmp(b).unwrap())
.min_by(|&a, &b| cmp_num(a, b))
.unwrap_or(f64::INFINITY),
);
}
Expand Down Expand Up @@ -758,3 +758,15 @@ impl Visit for SuperFinder {
self.found = true;
}
}

fn cmp_num(a: f64, b: f64) -> Ordering {
if a == -0.0 && b == 0.0 {
return Ordering::Greater;
}

if a == 0.0 && b == -0.0 {
return Ordering::Less;
}

a.partial_cmp(&b).unwrap()
}
18 changes: 18 additions & 0 deletions crates/swc_ecma_minifier/tests/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11293,3 +11293,21 @@ fn issue_8964() {
",
);
}

#[test]
fn issue_8982_1() {
run_default_exec_test(
"
console.log(Math.max(0, -0));
",
);
}

#[test]
fn issue_8982_2() {
run_default_exec_test(
"
console.log(Math.min(0, -0));
",
);
}

0 comments on commit 2a43df4

Please sign in to comment.