swc/crates/swc_ecma_utils
Levi 2aef14d34d
feat(es/utils): Support for arrays using cast_to_number (#9212)
**Description:**

This PR allows `ArrayLit`s to be converted to numbers in the
`cast_to_number` function. This allows expressions using arrays to be
converted to numbers. See some example expressions below that were
previously not able to be computed, but are now able to due to this
change.

```js
+[] // 0
+[[]] // 0
+[1] // 1
+[undefined] // 0
+[null] // 0
+[[1]] // 1
+[,] // 0
+[,,] // NaN
```

Regarding the implementation, arrays are converted to strings, and the
string is then parsed as a number. So arrays like `[]` and `[undefined]`
return `""` which then return `0` when parsed as a string. This is also
why arrays with more than one element can't be parsed because e.g. `[1,
2]` returns `"1,2"`. This procedure follows the ECMAScript
specification.
https://262.ecma-international.org/6.0/#sec-tonumber
https://262.ecma-international.org/6.0/#sec-toprimitive
2024-07-12 12:15:12 +09:00
..
src feat(es/utils): Support for arrays using cast_to_number (#9212) 2024-07-12 12:15:12 +09:00
Cargo.toml chore: Bump crates 2024-07-11 16:17:45 +00:00