mirror of
https://github.com/swc-project/swc.git
synced 2024-11-25 22:34:04 +03:00
2aef14d34d
**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 |
||
---|---|---|
.. | ||
src | ||
Cargo.toml |