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
45 changes: 19 additions & 26 deletions crates/oxc_ecmascript/src/to_number.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use oxc_ast::ast::*;

use crate::{
GlobalContext, to_primitive::maybe_object_with_to_primitive_related_properties_overridden,
GlobalContext, StringToNumber, ToJsString,
to_primitive::maybe_object_with_to_primitive_related_properties_overridden,
};

/// `ToNumber`
Expand All @@ -28,10 +29,7 @@ impl<'a> ToNumber<'a> for Expression<'a> {
"NaN" | "undefined" if ctx.is_global_reference(ident) => Some(f64::NAN),
_ => None,
},
Expression::StringLiteral(lit) => {
use crate::StringToNumber;
Some(lit.value.as_str().string_to_number())
}
Expression::StringLiteral(lit) => Some(lit.value.as_str().string_to_number()),
Expression::UnaryExpression(unary) if unary.operator.is_not() => {
let number = unary.argument.to_number(ctx)?;
Some(if number == 0.0 { 1.0 } else { 0.0 })
Expand All @@ -49,31 +47,26 @@ impl<'a> ToNumber<'a> for Expression<'a> {
// `ToPrimitive` for RegExp object returns `"/regexp/"`
Expression::RegExpLiteral(_) => Some(f64::NAN),
Expression::ArrayExpression(arr) => {
// If the array is empty, `ToPrimitive` returns `""`
if arr.elements.is_empty() {
return Some(0.0);
}
if arr.elements.len() == 1 {
let first_element = arr.elements.first().unwrap();
return match first_element {
ArrayExpressionElement::SpreadElement(_) => None,
// `ToPrimitive` returns `""` for `[,]`
ArrayExpressionElement::Elision(_) => Some(0.0),
match_expression!(ArrayExpressionElement) => {
first_element.to_expression().to_number(ctx)
}
};
}
// ToNumber for arrays:
// 1. ToPrimitive(array, hint Number) -> tries valueOf, then toString
// 2. Array.toString() -> Array.join(",")
// 3. ToNumber(resultString)

let non_spread_element_count = arr
// Fast path: if array has at least 2 non-spread elements,
// the result will contain "," which converts to NaN
if arr
.elements
.iter()
.filter(|e| !matches!(e, ArrayExpressionElement::SpreadElement(_)))
.count();
// If the array has at least 2 elements, `ToPrimitive` returns a string containing
// `,` which is not included in `StringNumericLiteral`
// So `ToNumber` returns `NaN`
if non_spread_element_count >= 2 { Some(f64::NAN) } else { None }
.take(2)
.count()
>= 2
{
return Some(f64::NAN);
}

let array_string = arr.to_js_string(ctx)?;
Some(array_string.as_ref().string_to_number())
}
_ => None,
}
Expand Down
15 changes: 15 additions & 0 deletions crates/oxc_minifier/tests/ecmascript/to_number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,19 @@ fn test() {
assert!(global_undefined_number.is_some_and(f64::is_nan));
assert!(empty_object_number.is_some_and(f64::is_nan));
assert_eq!(object_with_to_string_number, None);

// Test arrays with boolean elements - should convert to NaN
let false_literal = ast.alloc_boolean_literal(SPAN, false);
let true_literal = ast.alloc_boolean_literal(SPAN, true);
let array_with_false =
ast.expression_array(SPAN, ast.vec1(ArrayExpressionElement::BooleanLiteral(false_literal)));
let array_with_true =
ast.expression_array(SPAN, ast.vec1(ArrayExpressionElement::BooleanLiteral(true_literal)));
let array_with_false_number = array_with_false.to_number(&WithoutGlobalReferenceInformation {});
let array_with_true_number = array_with_true.to_number(&WithoutGlobalReferenceInformation {});

// [false].toString() = "false", Number("false") = NaN
assert!(array_with_false_number.is_some_and(f64::is_nan));
// [true].toString() = "true", Number("true") = NaN
assert!(array_with_true_number.is_some_and(f64::is_nan));
}
5 changes: 5 additions & 0 deletions crates/oxc_minifier/tests/peephole/fold_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,11 @@ fn test_fold_unary() {
fold("a=+[0, 1]", "a=NaN");
test_same("var foo; NOOP(a=+[0, ...foo])"); // can be either `a=0` or `a=NaN` (also `...foo` may have a side effect)
test("var foo; NOOP(a=+[0, ...[foo ? 'foo': ''], 1])", "var foo; NOOP(a=NaN)");

fold("a=+[false]", "a=NaN"); // `+"false"`
fold("a=+[true]", "a=NaN"); // `+"true"`
fold("a=+[undefined]", "a=0"); // `+""`
fold("a=+[null]", "a=0"); // `+""`
}

#[test]
Expand Down
Loading