feat(minifier): change NaN to f64::NAN (#8191)

This commit is contained in:
Boshen 2024-12-30 08:49:55 +00:00
parent cef8eb8077
commit d0de560f6d
2 changed files with 22 additions and 13 deletions

View file

@ -9,6 +9,8 @@ use crate::{node_util::Ctx, CompressorPass};
/// Make subsequent AST passes easier to analyze:
///
/// * convert whiles to fors
/// * convert `Infinity` to `f64::INFINITY`
/// * convert `NaN` to `f64::NaN`
///
/// <https://github.com/google/closure-compiler/blob/v20240609/src/com/google/javascript/jscomp/Normalize.java>
pub struct Normalize;
@ -28,7 +30,7 @@ impl<'a> Traverse<'a> for Normalize {
fn exit_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
if let Expression::Identifier(_) = expr {
Self::convert_infinity_into_number(expr, ctx);
Self::convert_infinity_or_nan_into_number(expr, ctx);
}
}
}
@ -52,18 +54,18 @@ impl<'a> Normalize {
*stmt = Statement::ForStatement(for_stmt);
}
fn convert_infinity_into_number(expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
let ctx = Ctx(ctx);
match expr {
Expression::Identifier(ident) if ctx.is_identifier_infinity(ident) => {
*expr = ctx.ast.expression_numeric_literal(
ident.span,
f64::INFINITY,
None,
NumberBase::Decimal,
);
}
_ => {}
fn convert_infinity_or_nan_into_number(expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
if let Expression::Identifier(ident) = expr {
let ctx = Ctx(ctx);
let value = if ctx.is_identifier_infinity(ident) {
f64::INFINITY
} else if ctx.is_identifier_nan(ident) {
f64::NAN
} else {
return;
};
*expr =
ctx.ast.expression_numeric_literal(ident.span, value, None, NumberBase::Decimal);
}
}
}

View file

@ -71,4 +71,11 @@ impl<'a> Ctx<'a, '_> {
}
false
}
pub fn is_identifier_nan(self, ident: &IdentifierReference) -> bool {
if ident.name == "Infinity" && ident.is_global_reference(self.symbols()) {
return true;
}
false
}
}