fix(es/minifier): Fix comparison of -0.0 and 0 (#8973)

**Related issue:**

 - Closes #8972
This commit is contained in:
Donny/강동윤 2024-05-24 09:16:42 +09:00 committed by GitHub
parent eaffeb8dab
commit 2a43df4984
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 33 additions and 3 deletions

View File

@ -1,4 +1,4 @@
use std::f64;
use std::{cmp::Ordering, f64};
use swc_common::{util::take::Take, DUMMY_SP};
use swc_ecma_ast::*;
@ -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),
);
}
@ -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),
);
}
@ -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()
}

View File

@ -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));
",
);
}