-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
fix: snapshot destructured values in #each blocks to prevent mutation… #17232
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
base: main
Are you sure you want to change the base?
fix: snapshot destructured values in #each blocks to prevent mutation… #17232
Conversation
… issues Fixes sveltejs#17227 When using destructuring in #each blocks (e.g., {#each gen() as [item]}), if the generator mutates the yielded object, all items would end up with the same final value because they all reference the same object. This fix adds snapshotting for destructured patterns in each blocks, ensuring that each iteration gets a snapshot of the value at the time it was yielded, matching the behavior of a standard for...of loop. - Add snapshot_each_value, snapshot_array, and snapshot_object utilities - Apply snapshotting in both client and server EachBlock visitors - Add test case to prevent regression
|
|
Why would you want this behaviour? It's wrong. If you don't want the array to be mutated, don't mutate it. |
|
…for...of behavior Fixes sveltejs#17227 When using array destructuring in #each blocks (e.g., {#each gen() as [item]}), if the generator mutates the yielded array object, all items would end up with the same final value because destructuring happened after the collection was converted to an array, rather than immediately during iteration. This fix ensures that array destructuring happens immediately during iteration, capturing values at the time they are yielded, matching the behavior of a standard for...of loop with destructuring. - Add to_array_destructured utility that destructures during iteration - Apply immediate destructuring for array patterns in both client and server EachBlock visitors - Keep object destructuring using snapshotting (shallow copy) - Add test case to prevent regression
I've updated the fix to match for...of behavior. Instead of snapshotting, array destructuring now happens immediately during iteration, capturing values at the time they are yielded.
|
… issues
Fixes #17227
When using destructuring in #each blocks (e.g., {#each gen() as [item]}), if the generator mutates the yielded object, all items would end up with the same final value because they all reference the same object.
This fix adds snapshotting for destructured patterns in each blocks, ensuring that each iteration gets a snapshot of the value at the time it was yielded, matching the behavior of a standard for...of loop.
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 testand lint the project withpnpm lint