Skip to content

Commit cc960d3

Browse files
committed
tweak wording
1 parent 1ad7aef commit cc960d3

File tree

6 files changed

+19
-15
lines changed

6 files changed

+19
-15
lines changed

documentation/docs/98-reference/.generated/compile-errors.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ The $ prefix is reserved, and cannot be used for variables and imports
331331
### each_item_invalid_assignment
332332

333333
```
334-
Cannot reassign or bind to the each block argument itself in runes mode. Use the array and index variables instead: For reassignments `array[i] = value` instead of `entry = value`, for bindings `bind:prop={array[i]}` instead of `bind:prop={value}`
334+
Cannot reassign or bind to each block argument in runes mode. Use the array and index variables instead (e.g. `array[i] = value` instead of `entry = value`, or `bind:value={array[i]}` instead of `bind:value={entry}`)
335335
```
336336

337337
In legacy mode, it was possible to reassign or bind to the each block argument itself:
@@ -344,23 +344,25 @@ In legacy mode, it was possible to reassign or bind to the each block argument i
344344
{#each array as entry}
345345
<!-- reassignment -->
346346
<button on:click={() => entry = 4}>change</button>
347+
347348
<!-- binding -->
348349
<input bind:value={entry}>
349350
{/each}
350351
```
351352

352-
This is no longer possible in runes mode, because the array could also be a computed value such as `{#each array.filter((i) => i % 2)}`, at which point things get unstable. Svelte therefore requires you to be explicit about what to bind to, requiring a stable reference. Therefore, use the array and index variables instead:
353+
This turned out to be buggy and unpredictable, particularly when working with derived values (such as `array.map(...)`), and as such is forbidden in runes mode. You can achieve the same outcome by using the index instead:
353354

354355
```svelte
355356
<script>
356357
let array = $state([1, 2, 3]);
357358
</script>
358359
359-
{#each array as entry, idx}
360+
{#each array as entry, i}
360361
<!-- reassignment -->
361-
<button onclick={() => array[idx] = 4}>change</button>
362+
<button onclick={() => array[i] = 4}>change</button>
363+
362364
<!-- binding -->
363-
<input bind:value={array[idx]}>
365+
<input bind:value={array[i]}>
364366
{/each}
365367
```
366368

packages/svelte/messages/compile-errors/script.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
3333
## each_item_invalid_assignment
3434

35-
> Cannot reassign or bind to the each block argument itself in runes mode. Use the array and index variables instead: For reassignments `array[i] = value` instead of `entry = value`, for bindings `bind:prop={array[i]}` instead of `bind:prop={value}`
35+
> Cannot reassign or bind to each block argument in runes mode. Use the array and index variables instead (e.g. `array[i] = value` instead of `entry = value`, or `bind:value={array[i]}` instead of `bind:value={entry}`)
3636
3737
In legacy mode, it was possible to reassign or bind to the each block argument itself:
3838

@@ -44,23 +44,25 @@ In legacy mode, it was possible to reassign or bind to the each block argument i
4444
{#each array as entry}
4545
<!-- reassignment -->
4646
<button on:click={() => entry = 4}>change</button>
47+
4748
<!-- binding -->
4849
<input bind:value={entry}>
4950
{/each}
5051
```
5152

52-
This is no longer possible in runes mode, because the array could also be a computed value such as `{#each array.filter((i) => i % 2)}`, at which point things get unstable. Svelte therefore requires you to be explicit about what to bind to, requiring a stable reference. Therefore, use the array and index variables instead:
53+
This turned out to be buggy and unpredictable, particularly when working with derived values (such as `array.map(...)`), and as such is forbidden in runes mode. You can achieve the same outcome by using the index instead:
5354

5455
```svelte
5556
<script>
5657
let array = $state([1, 2, 3]);
5758
</script>
5859
59-
{#each array as entry, idx}
60+
{#each array as entry, i}
6061
<!-- reassignment -->
61-
<button onclick={() => array[idx] = 4}>change</button>
62+
<button onclick={() => array[i] = 4}>change</button>
63+
6264
<!-- binding -->
63-
<input bind:value={array[idx]}>
65+
<input bind:value={array[i]}>
6466
{/each}
6567
```
6668

packages/svelte/src/compiler/errors.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,12 @@ export function dollar_prefix_invalid(node) {
151151
}
152152

153153
/**
154-
* Cannot reassign or bind to the each block argument itself in runes mode. Use the array and index variables instead: For reassignments `array[i] = value` instead of `entry = value`, for bindings `bind:prop={array[i]}` instead of `bind:prop={value}`
154+
* Cannot reassign or bind to each block argument in runes mode. Use the array and index variables instead (e.g. `array[i] = value` instead of `entry = value`, or `bind:value={array[i]}` instead of `bind:value={entry}`)
155155
* @param {null | number | NodeLike} node
156156
* @returns {never}
157157
*/
158158
export function each_item_invalid_assignment(node) {
159-
e(node, "each_item_invalid_assignment", `Cannot reassign or bind to the each block argument itself in runes mode. Use the array and index variables instead: For reassignments \`array[i] = value\` instead of \`entry = value\`, for bindings \`bind:prop={array[i]}\` instead of \`bind:prop={value}\`\nhttps://svelte.dev/e/each_item_invalid_assignment`);
159+
e(node, "each_item_invalid_assignment", `Cannot reassign or bind to each block argument in runes mode. Use the array and index variables instead (e.g. \`array[i] = value\` instead of \`entry = value\`, or \`bind:value={array[i]}\` instead of \`bind:value={entry}\`)\nhttps://svelte.dev/e/each_item_invalid_assignment`);
160160
}
161161

162162
/**

packages/svelte/tests/compiler-errors/samples/runes-invalid-each-binding-this/_config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ export default test({
44
error: {
55
code: 'each_item_invalid_assignment',
66
message:
7-
'Cannot reassign or bind to each block argument in runes mode. Use the array and index variables instead (e.g. `array[i] = value` instead of `entry = value`)'
7+
'Cannot reassign or bind to each block argument in runes mode. Use the array and index variables instead (e.g. `array[i] = value` instead of `entry = value`, or `bind:value={array[i]}` instead of `bind:value={entry}`)'
88
}
99
});

packages/svelte/tests/compiler-errors/samples/runes-invalid-each-binding/_config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ export default test({
44
error: {
55
code: 'each_item_invalid_assignment',
66
message:
7-
'Cannot reassign or bind to each block argument in runes mode. Use the array and index variables instead (e.g. `array[i] = value` instead of `entry = value`)'
7+
'Cannot reassign or bind to each block argument in runes mode. Use the array and index variables instead (e.g. `array[i] = value` instead of `entry = value`, or `bind:value={array[i]}` instead of `bind:value={entry}`)'
88
}
99
});

packages/svelte/tests/compiler-errors/samples/runes-invalid-each-mutation/_config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ export default test({
44
error: {
55
code: 'each_item_invalid_assignment',
66
message:
7-
'Cannot reassign or bind to each block argument in runes mode. Use the array and index variables instead (e.g. `array[i] = value` instead of `entry = value`)'
7+
'Cannot reassign or bind to each block argument in runes mode. Use the array and index variables instead (e.g. `array[i] = value` instead of `entry = value`, or `bind:value={array[i]}` instead of `bind:value={entry}`)'
88
}
99
});

0 commit comments

Comments
 (0)