Skip to content

fix: error on bind:this to each block parameter #12638

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 3 commits into from
Jul 28, 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/sharp-spies-live.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: error on `bind:this` to each block parameter
28 changes: 13 additions & 15 deletions packages/svelte/src/compiler/phases/2-analyze/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -385,33 +385,31 @@ const validation = {

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

if (
assignee.type === 'Identifier' &&
node.name !== 'this' // bind:this also works for regular variables
) {
if (assignee.type === 'Identifier') {
// reassignment
if (
!binding ||
(binding.kind !== 'state' &&
binding.kind !== 'frozen_state' &&
binding.kind !== 'prop' &&
binding.kind !== 'bindable_prop' &&
binding.kind !== 'each' &&
binding.kind !== 'store_sub' &&
!binding.mutated)
node.name !== 'this' && // bind:this also works for regular variables
(!binding ||
(binding.kind !== 'state' &&
binding.kind !== 'frozen_state' &&
binding.kind !== 'prop' &&
binding.kind !== 'bindable_prop' &&
binding.kind !== 'each' &&
binding.kind !== 'store_sub' &&
!binding.mutated))
) {
e.bind_invalid_value(node.expression);
}

if (binding.kind === 'derived') {
if (binding?.kind === 'derived') {
e.constant_binding(node.expression, 'derived state');
}

if (context.state.analysis.runes && binding.kind === 'each') {
if (context.state.analysis.runes && binding?.kind === 'each') {
e.each_item_invalid_assignment(node);
}

if (binding.kind === 'snippet') {
if (binding?.kind === 'snippet') {
e.snippet_parameter_assignment(node);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { test } from '../../test';

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`)'
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<script lang="ts">
let array: Array<{ id: number; element: HTMLElement | null }> = $state([
{ id: 1, element: null },
{ id: 2, element: null },
{ id: 3, element: null }
]);
</script>

{#each array as { id, element } (id)}
<input bind:this={element} />
{/each}
Loading