Skip to content

Commit

Permalink
refactor(minifier): use builtin get_number_value.
Browse files Browse the repository at this point in the history
  • Loading branch information
7086cmd committed Oct 9, 2024
1 parent 3e45842 commit 57a723f
Showing 1 changed file with 2 additions and 21 deletions.
23 changes: 2 additions & 21 deletions crates/oxc_minifier/src/ast_passes/peephole_fold_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,25 +71,6 @@ impl<'a> PeepholeFoldConstants {
Self { changed: false }
}

fn try_get_number_literal_value(&self, expr: &mut Expression<'a>) -> Option<f64> {
match expr {
Expression::NumericLiteral(n) => Some(n.value),
Expression::Identifier(id) if id.name.as_str() == "Infinity" => Some(f64::INFINITY),
Expression::UnaryExpression(unary)
if unary.operator == UnaryOperator::UnaryNegation =>
{
match &mut unary.argument {
Expression::NumericLiteral(arg) => Some(-arg.value),
Expression::Identifier(id) if id.name.as_str() == "Infinity" => {
Some(f64::NEG_INFINITY)
}
_ => None,
}
}
_ => None,
}
}

fn try_fold_useless_object_dot_define_properties_call(
&mut self,
_call_expr: &mut CallExpression<'a>,
Expand Down Expand Up @@ -520,8 +501,8 @@ impl<'a> PeepholeFoldConstants {
if !operation.operator.is_arithmetic() {
return None;
};
let left = self.try_get_number_literal_value(&mut operation.left)?;
let right = self.try_get_number_literal_value(&mut operation.right)?;
let left: f64 = ctx.get_number_value(&operation.left)?.try_into().ok()?;
let right: f64 = ctx.get_number_value(&operation.right)?.try_into().ok()?;
if !left.is_finite() || !right.is_finite() {
return self.try_fold_infinity_arithmetic(left, operation.operator, right, ctx);
}
Expand Down

0 comments on commit 57a723f

Please sign in to comment.