mirror of
https://github.com/swc-project/swc.git
synced 2024-12-24 22:22:34 +03:00
fix(es/minifier): Fix comparison of -0.0
and 0
(#8973)
**Related issue:** - Closes #8972
This commit is contained in:
parent
eaffeb8dab
commit
2a43df4984
@ -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()
|
||||
}
|
||||
|
@ -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));
|
||||
",
|
||||
);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user