Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions crates/oxc_minifier/src/ast_passes/normalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
}
}
Expand All @@ -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);
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions crates/oxc_minifier/src/node_util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Loading