-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
fix: rewrite destructuring logic to handle iterators #15813
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
Conversation
🦋 Changeset detectedLatest commit: aebef7d The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
This may produce multiple spreadings: example // source
let [first, second, third] = $state(array);
// output
let tmp = array,
first = $.state($.proxy([...tmp][0])),
second = $.state($.proxy([...tmp][1])),
third = $.state($.proxy([...tmp][2])); while it would be better let tmp = [...array],
first = $.state($.proxy(tmp[0])),
second = $.state($.proxy(tmp[1])),
third = $.state($.proxy(tmp[2])); or even function to_array(iterable) {
return Array.isArray(iterable) ? iterable : [...iterable]
}
let tmp = to_array(array),
... though I'm not sure about TypedArrays |
I'll try the second one, and see how well it works. The only issue is assignments, which don't always result in a temporary variable being made. |
Still the same behavior 🤔 |
Just realized that since generator functions are iterators, then this sort of thing (which I use very often) would break. |
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.
this is a great fix! slight shame we need to reintroduce IIFEs but it's such a rare case that I don't think it matters
* revert #15813 * failing test * tweak * tweak * WIP * WIP * WIP * WIP * WIP * working * tweak * changeset * tweak description * Update packages/svelte/src/compiler/utils/ast.js Co-authored-by: Simon H <5968653+dummdidumm@users.noreply.github.com> --------- Co-authored-by: Simon H <5968653+dummdidumm@users.noreply.github.com>
Currently, the compiler assumes that whenever you use array destructuring that the object you're destructuring is an array, and can be interpreted as this:
However, due to iterators, and anything else using
Symbol.iterator
, this breaks things. This fixes that by reusing the original destructuring structure, like so:Before submitting the PR, please make sure you do the following
feat:
,fix:
,chore:
, ordocs:
.packages/svelte/src
, add a changeset (npx changeset
).Tests and linting
pnpm test
and lint the project withpnpm lint