Skip to content

Commit a2565ef

Browse files
docs: tweak "invalid assignment" compiler error message (#14955)
* docs: tweak "invalid assignment" compiler error message fixes #14702 * tweak wording --------- Co-authored-by: Rich Harris <[email protected]>
1 parent ce4f972 commit a2565ef

File tree

7 files changed

+76
-7
lines changed

7 files changed

+76
-7
lines changed

.changeset/sharp-dryers-try.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
chore: tweak "invalid assignment" compiler error message

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

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,39 @@ 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 each block argument in runes mode. Use the array and index variables instead (e.g. `array[i] = value` instead of `entry = 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}`)
335+
```
336+
337+
In legacy mode, it was possible to reassign or bind to the each block argument itself:
338+
339+
```svelte
340+
<script>
341+
let array = [1, 2, 3];
342+
</script>
343+
344+
{#each array as entry}
345+
<!-- reassignment -->
346+
<button on:click={() => entry = 4}>change</button>
347+
348+
<!-- binding -->
349+
<input bind:value={entry}>
350+
{/each}
351+
```
352+
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:
354+
355+
```svelte
356+
<script>
357+
let array = $state([1, 2, 3]);
358+
</script>
359+
360+
{#each array as entry, i}
361+
<!-- reassignment -->
362+
<button onclick={() => array[i] = 4}>change</button>
363+
364+
<!-- binding -->
365+
<input bind:value={array[i]}>
366+
{/each}
335367
```
336368

337369
### effect_invalid_placement

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

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,39 @@
3232
3333
## each_item_invalid_assignment
3434

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`)
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}`)
36+
37+
In legacy mode, it was possible to reassign or bind to the each block argument itself:
38+
39+
```svelte
40+
<script>
41+
let array = [1, 2, 3];
42+
</script>
43+
44+
{#each array as entry}
45+
<!-- reassignment -->
46+
<button on:click={() => entry = 4}>change</button>
47+
48+
<!-- binding -->
49+
<input bind:value={entry}>
50+
{/each}
51+
```
52+
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:
54+
55+
```svelte
56+
<script>
57+
let array = $state([1, 2, 3]);
58+
</script>
59+
60+
{#each array as entry, i}
61+
<!-- reassignment -->
62+
<button onclick={() => array[i] = 4}>change</button>
63+
64+
<!-- binding -->
65+
<input bind:value={array[i]}>
66+
{/each}
67+
```
3668

3769
## effect_invalid_placement
3870

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 each block argument in runes mode. Use the array and index variables instead (e.g. `array[i] = value` instead of `entry = 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 each block argument in runes mode. Use the array and index variables instead (e.g. \`array[i] = value\` instead of \`entry = 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)