Skip to content

fix: make each items reassignable #12257

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

Merged
merged 11 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/thirty-guests-flow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: make each items reassignable in legacy mode
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,23 @@ export function EachBlock(node, context) {
if (invalidate_store) sequence.push(invalidate_store);

if (node.context.type === 'Identifier') {
const binding = /** @type {Binding} */ (context.state.scope.get(node.context.name));

child_state.transform[node.context.name] = {
read: (flags & EACH_ITEM_REACTIVE) !== 0 ? get_value : (node) => node,
read: (node) => {
if (binding.reassigned) {
// we need to do `array[$$index]` instead of `$$item` or whatever
// TODO 6.0 this only applies in legacy mode, reassignments are
// forbidden in runes mode
return b.member(
each_node_meta.array_name ? b.call(each_node_meta.array_name) : collection,
index,
true
);
}

return (flags & EACH_ITEM_REACTIVE) !== 0 ? get_value(node) : node;
},
assign: (_, value) => {
uses_index = true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function EachBlock(node, context) {
state.init.push(b.const(array_id, b.call('$.ensure_array_like', collection)));

/** @type {Statement[]} */
const each = [b.const(/** @type {Pattern} */ (node.context), b.member(array_id, index, true))];
const each = [b.let(/** @type {Pattern} */ (node.context), b.member(array_id, index, true))];

if (index.name !== node.index && node.index != null) {
each.push(b.let(node.index, index));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script>
export let value;

value += 1;
</script>

<p>{value}</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { test } from '../../test';

export default test({
html: `
<p>2, 3, 4</p>
<p>2</p>
<p>3</p>
<p>4</p>
<p>2, 3, 4</p>
`,

ssrHtml: `
<p>1, 2, 3</p>
<p>2</p>
<p>3</p>
<p>4</p>
<p>1, 2, 3</p>
`
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<script>
import Child from './Child.svelte';

let numbers = [1, 2, 3];
</script>

<p>{numbers.join(', ')}</p>

{#each numbers as n}
<Child bind:value={n} />
{/each}

<p>{numbers.join(', ')}</p>
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default function Each_string_template($$payload) {
$$payload.out += `<!--[-->`;

for (let $$index = 0, $$length = each_array.length; $$index < $$length; $$index++) {
const thing = each_array[$$index];
let thing = each_array[$$index];

$$payload.out += `<!---->${$.escape(thing)}, `;
}
Expand Down