-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Babel loose mode is unsafe with array spread operator #8298
Comments
Hey @TrySound! We really appreciate you taking the time to report an issue. The collaborators If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack |
Well, this is a cost of using loose mode. It's not always spec-compliant and such cases might bite you. I don't think holey arrays are popular enough to warrant introducing Maybe we could try to track if array might be holey and use this helper IF we know that it is - it would be limited only to the simplest situations though - analyzing runtime values with babel is not easy. We could similarly special case string literals and convert them to |
Loose mode assumes that your code behaves "in the most obvious way": Babel assumes that you are only using array spread with "normal" (e.g. not holey) arrays. |
Spread was meant for all Iterable's, not only Array's. It should work with String, Set, Map, Generator etc. export const locationChange = ({ pathname, search }) => ({
type: LOCATION_CHANGE,
mutation: {
pathname,
search,
params: new URLSearchParams(search)
|> map(([key, value]) => ({ [key]: value }))
// Array.from is needed because we use babel in loose mode
|> it => Object.assign({}, ...Array.from(it)),
},
}); Here Array.from is needed in loose mode, because map returns a Generator: export const map = transform =>
function*(iterable) {
for (const item of iterable) {
yield transform(item);
}
}; I wouldn't call it obvious that spread doesn't work for all Iterable's. |
@kasperpeulen Agreed. Just hit this myself upgrading to Babel v7, where |
I'm closing this for two reasons:
|
Bug Report
I just went though two bugs with array spread operator.
Spread of array constructor call
Spread of string
Expected behavior/code
I think this loose mode is too loose and could be slightly improved with small helpers. This things are the only problems I had with loose mode in a year.
Babel Configuration (.babelrc, package.json, cli command)
Environment
Possible Solution
Make loose mode for spread operator slightly stricter.
The text was updated successfully, but these errors were encountered: