Skip to content

Commit 219ba6a

Browse files
authored
fix: error on bind:this to each block parameter (#12638)
* chore: remove outdated comment * fix: error on `bind:this` to each block parameter
1 parent 7af0e60 commit 219ba6a

File tree

4 files changed

+38
-15
lines changed

4 files changed

+38
-15
lines changed

.changeset/sharp-spies-live.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+
fix: error on `bind:this` to each block parameter

packages/svelte/src/compiler/phases/2-analyze/validation.js

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -385,33 +385,31 @@ const validation = {
385385

386386
const binding = context.state.scope.get(left.name);
387387

388-
if (
389-
assignee.type === 'Identifier' &&
390-
node.name !== 'this' // bind:this also works for regular variables
391-
) {
388+
if (assignee.type === 'Identifier') {
392389
// reassignment
393390
if (
394-
!binding ||
395-
(binding.kind !== 'state' &&
396-
binding.kind !== 'frozen_state' &&
397-
binding.kind !== 'prop' &&
398-
binding.kind !== 'bindable_prop' &&
399-
binding.kind !== 'each' &&
400-
binding.kind !== 'store_sub' &&
401-
!binding.mutated)
391+
node.name !== 'this' && // bind:this also works for regular variables
392+
(!binding ||
393+
(binding.kind !== 'state' &&
394+
binding.kind !== 'frozen_state' &&
395+
binding.kind !== 'prop' &&
396+
binding.kind !== 'bindable_prop' &&
397+
binding.kind !== 'each' &&
398+
binding.kind !== 'store_sub' &&
399+
!binding.mutated))
402400
) {
403401
e.bind_invalid_value(node.expression);
404402
}
405403

406-
if (binding.kind === 'derived') {
404+
if (binding?.kind === 'derived') {
407405
e.constant_binding(node.expression, 'derived state');
408406
}
409407

410-
if (context.state.analysis.runes && binding.kind === 'each') {
408+
if (context.state.analysis.runes && binding?.kind === 'each') {
411409
e.each_item_invalid_assignment(node);
412410
}
413411

414-
if (binding.kind === 'snippet') {
412+
if (binding?.kind === 'snippet') {
415413
e.snippet_parameter_assignment(node);
416414
}
417415
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { test } from '../../test';
2+
3+
export default test({
4+
error: {
5+
code: 'each_item_invalid_assignment',
6+
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`)'
8+
}
9+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<script lang="ts">
2+
let array: Array<{ id: number; element: HTMLElement | null }> = $state([
3+
{ id: 1, element: null },
4+
{ id: 2, element: null },
5+
{ id: 3, element: null }
6+
]);
7+
</script>
8+
9+
{#each array as { id, element } (id)}
10+
<input bind:this={element} />
11+
{/each}

0 commit comments

Comments
 (0)