-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(es/utils): Support for arrays using cast_to_number
#9212
Conversation
CodSpeed Performance ReportMerging #9212 will degrade performances by 38.94%Comparing Summary
Benchmarks breakdown
|
ff2749d might be better for performance, and does the same thing as far as I'm aware, but the current code is more correct. The performance change might also simply be because of arrays not being casted to numbers before. |
No worries, those performance issues are not related to this PR |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
swc-bump:
- swc_ecma_utils
- swc_core
Sounds good, also I'm not sure if the return statements are correct regarding purity but I assume you've looked over that already |
Description:
This PR allows
ArrayLit
s to be converted to numbers in thecast_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.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 return0
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
BREAKING CHANGE:
Related issue (if exists):