Skip to content

Commit

Permalink
docs: tweak "invalid assignment" compiler error message (#14955)
Browse files Browse the repository at this point in the history
* docs: tweak "invalid assignment" compiler error message

fixes #14702

* tweak wording

---------

Co-authored-by: Rich Harris <rich.harris@vercel.com>
  • Loading branch information
dummdidumm and Rich-Harris authored Jan 8, 2025
1 parent ce4f972 commit a2565ef
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/sharp-dryers-try.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

chore: tweak "invalid assignment" compiler error message
34 changes: 33 additions & 1 deletion documentation/docs/98-reference/.generated/compile-errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,39 @@ The $ prefix is reserved, and cannot be used for variables and imports
### 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`)
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}`)
```

In legacy mode, it was possible to reassign or bind to the each block argument itself:

```svelte
<script>
let array = [1, 2, 3];
</script>
{#each array as entry}
<!-- reassignment -->
<button on:click={() => entry = 4}>change</button>
<!-- binding -->
<input bind:value={entry}>
{/each}
```

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:

```svelte
<script>
let array = $state([1, 2, 3]);
</script>
{#each array as entry, i}
<!-- reassignment -->
<button onclick={() => array[i] = 4}>change</button>
<!-- binding -->
<input bind:value={array[i]}>
{/each}
```

### effect_invalid_placement
Expand Down
34 changes: 33 additions & 1 deletion packages/svelte/messages/compile-errors/script.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,39 @@
## 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`)
> 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}`)
In legacy mode, it was possible to reassign or bind to the each block argument itself:

```svelte
<script>
let array = [1, 2, 3];
</script>
{#each array as entry}
<!-- reassignment -->
<button on:click={() => entry = 4}>change</button>
<!-- binding -->
<input bind:value={entry}>
{/each}
```

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:

```svelte
<script>
let array = $state([1, 2, 3]);
</script>
{#each array as entry, i}
<!-- reassignment -->
<button onclick={() => array[i] = 4}>change</button>
<!-- binding -->
<input bind:value={array[i]}>
{/each}
```

## effect_invalid_placement

Expand Down
4 changes: 2 additions & 2 deletions packages/svelte/src/compiler/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,12 @@ export function dollar_prefix_invalid(node) {
}

/**
* 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`)
* 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}`)
* @param {null | number | NodeLike} node
* @returns {never}
*/
export function each_item_invalid_assignment(node) {
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`);
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`);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ export default test({
error: {
code: 'each_item_invalid_assignment',
message:
'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`)'
'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}`)'
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ export default test({
error: {
code: 'each_item_invalid_assignment',
message:
'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`)'
'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}`)'
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ export default test({
error: {
code: 'each_item_invalid_assignment',
message:
'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`)'
'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}`)'
}
});

0 comments on commit a2565ef

Please sign in to comment.