From 994b60b4205b8549ecf6e27258ea87a8158af0d0 Mon Sep 17 00:00:00 2001 From: 7086cmd <54303040+7086cmd@users.noreply.github.com> Date: Wed, 9 Oct 2024 12:55:33 +0000 Subject: [PATCH] refactor(minifier): use builtin get_number_value. (#6335) --- .../src/ast_passes/peephole_fold_constants.rs | 23 ++----------------- 1 file changed, 2 insertions(+), 21 deletions(-) diff --git a/crates/oxc_minifier/src/ast_passes/peephole_fold_constants.rs b/crates/oxc_minifier/src/ast_passes/peephole_fold_constants.rs index 96c5eb53cee24..7e5b13cc6bfa0 100644 --- a/crates/oxc_minifier/src/ast_passes/peephole_fold_constants.rs +++ b/crates/oxc_minifier/src/ast_passes/peephole_fold_constants.rs @@ -71,25 +71,6 @@ impl<'a> PeepholeFoldConstants { Self { changed: false } } - fn try_get_number_literal_value(&self, expr: &mut Expression<'a>) -> Option { - 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>, @@ -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); }