-
Notifications
You must be signed in to change notification settings - Fork 67
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
Error transpiling spread Array factory #81
Comments
Another example for this (not correctly transpiled currently): var a = Array(...[,,]);
return "0" in a && "1" in a && '' + a[0] + a[1] === "undefinedundefined"; |
Similarly, I just found that trying to spread a [ ...new Set([ 1, 2, 3 ]) ]
// expected => [ 1, 2, 3 ]
// actual => [ Set{1, 2, 3} ] For spreading iterables, would an acceptable solution be to check if the value being spread is an iterable, and if so, call |
And then there are strings, which aren't split by our spread implementation either. The same issues also apply to destructuring: const [a, b] = new Set([1, 2, 3]); Apparently these buggy behaviors are rarely used in the wild, otherwise I would have expected some bug reports about it. I think an acceptable solution should
|
Related: Spread operator and NodeLists Support iterables in dangerous for-of loops Strict mode proposal |
@adrianheine Shouldn't |
We don't know at transpile-time if we're spreading a |
By any chance, is there any workaround for using spread on NodeList such as Coincidentally, we had issue 81 on Gitlab as @kzc referenced. |
@zanona |
@nathancahill yes, but then it will break on unsupported browsers isn't that correct? I am assuming that for using bublé, anyone using the spread on array-like structures will need to refactor at the moment? Is there a solution for this use case at the moment? |
No, because Bublé has no knowledge of the type, it can't transpile differently based on the type. The solution is to inject a helper function like Babel does, which Bublé doesn't support (at all?) yet. All transforms are done in place. |
Thanks, @nathancahill that makes sense. Currently Buble transpiles Array spreads to I agree and am completely in favour of reducing the size of the generated code using shims to sustain that before running. But wouldn't translating array spreads into It still works the same way whenever an array is passed |
Only modern browsers support |
Yes, but you can use a shim for that can't you? |
I understand that this is really bad. Adding a shim to bublé seems not in line with the project's current design, though. Is |
@adrianheine Amazing Idea! It works great. |
Sounds great. 🙌🏻 |
Looking in to this a little more after some coffee. NodeList works fine. Set/Map do not since I'm think @m59peacemaker's suggestion to have a config option like It can default to |
That would mean it would silently switch to producing the current, broken code if you change your compile target, right? If the set of engines with Set/Map support were a subset of those with const toApplyParam = function(v) {
if (typeof Symbol !== "undefined" && Symbol.iterator && v[Symbol.iterator]) {
var iterator = v[Symbol.iterator]()
var res = []
while (!(v = iterator.next()).done) res.push(v.value)
return res
}
return v
}; |
As a shim or inline? |
I was thinking of something like https://github.com/Rich-Harris/buble/blob/8f4155682bb803f4edb2c932182a0ba3bd7f6096/src/program/Program.js#L63, i. e. a helper function that gets added to the output if necessary. |
I think #8 is relevant for this as well. |
Hey guys, sorry to bump this issue. But just checking if we could agree on the best way to implement this. It's something that there's no workaround at the moment unless changing the input code. Quite a few possible solutions have been presented already, so hopefully, we could think about solving a problem that exists in a graceful way? Thanks again |
At a glance |
@julienetie this wouldn't solve my initial problem. [].slice.call(Array(2))
//=> (2) [empty × 2]
[...Array(2)]
//=> (2) [undefined, undefined] Maybe [].concat.apply([], Array(2))
//=> (2) [undefined, undefined]
[...Array(2)]
//=> (2) [undefined, undefined] |
I'm here for Suggestions? |
No progress on this yet? Trying to process Vue.js and this file is failing: https://fossies.org/linux/vue-next/packages/runtime-core/src/scheduler.ts
Please let me know if there are any means of applying a workaround or anything |
Bublé parses
[ ...Array(length) ]
as[].concat( Array(length) )
witch is not equivalent.Spreading
Array
factory returns an array with undefined values.Concating
Array
factory returns an empty array with defined length.What I get?
What I expected?
OR
OR
The text was updated successfully, but these errors were encountered: