From 53af138d588f77bb8f4f10f9ad15fd4f798b50ef Mon Sep 17 00:00:00 2001 From: Paolo Ricciuti Date: Thu, 7 Nov 2024 21:18:45 +0100 Subject: [PATCH] fix: read index as a source in legacy keyed each block (#14208) Fixes #14203 --- .changeset/late-dodos-film.md | 5 +++++ .../phases/3-transform/client/visitors/EachBlock.js | 4 ++-- .../keyed-each-bind-read-index/Component.svelte | 5 +++++ .../samples/keyed-each-bind-read-index/_config.js | 9 +++++++++ .../samples/keyed-each-bind-read-index/main.svelte | 10 ++++++++++ 5 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 .changeset/late-dodos-film.md create mode 100644 packages/svelte/tests/runtime-legacy/samples/keyed-each-bind-read-index/Component.svelte create mode 100644 packages/svelte/tests/runtime-legacy/samples/keyed-each-bind-read-index/_config.js create mode 100644 packages/svelte/tests/runtime-legacy/samples/keyed-each-bind-read-index/main.svelte diff --git a/.changeset/late-dodos-film.md b/.changeset/late-dodos-film.md new file mode 100644 index 000000000000..c3636019dab0 --- /dev/null +++ b/.changeset/late-dodos-film.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: read index as a source in legacy keyed each block diff --git a/packages/svelte/src/compiler/phases/3-transform/client/visitors/EachBlock.js b/packages/svelte/src/compiler/phases/3-transform/client/visitors/EachBlock.js index fb6338cae312..55d7ded247ad 100644 --- a/packages/svelte/src/compiler/phases/3-transform/client/visitors/EachBlock.js +++ b/packages/svelte/src/compiler/phases/3-transform/client/visitors/EachBlock.js @@ -196,7 +196,7 @@ export function EachBlock(node, context) { // forbidden in runes mode return b.member( each_node_meta.array_name ? b.call(each_node_meta.array_name) : collection, - index, + (flags & EACH_INDEX_REACTIVE) !== 0 ? get_value(index) : index, true ); } @@ -208,7 +208,7 @@ export function EachBlock(node, context) { const left = b.member( each_node_meta.array_name ? b.call(each_node_meta.array_name) : collection, - index, + (flags & EACH_INDEX_REACTIVE) !== 0 ? get_value(index) : index, true ); diff --git a/packages/svelte/tests/runtime-legacy/samples/keyed-each-bind-read-index/Component.svelte b/packages/svelte/tests/runtime-legacy/samples/keyed-each-bind-read-index/Component.svelte new file mode 100644 index 000000000000..988c2b645752 --- /dev/null +++ b/packages/svelte/tests/runtime-legacy/samples/keyed-each-bind-read-index/Component.svelte @@ -0,0 +1,5 @@ + \ No newline at end of file diff --git a/packages/svelte/tests/runtime-legacy/samples/keyed-each-bind-read-index/_config.js b/packages/svelte/tests/runtime-legacy/samples/keyed-each-bind-read-index/_config.js new file mode 100644 index 000000000000..8b5db4df4090 --- /dev/null +++ b/packages/svelte/tests/runtime-legacy/samples/keyed-each-bind-read-index/_config.js @@ -0,0 +1,9 @@ +import { test } from '../../test'; + +export default test({ + async test({ assert, target, logs }) { + const p = target.querySelector('p'); + assert.equal(p?.innerHTML, '1'); + assert.deepEqual(logs, [0]); + } +}); diff --git a/packages/svelte/tests/runtime-legacy/samples/keyed-each-bind-read-index/main.svelte b/packages/svelte/tests/runtime-legacy/samples/keyed-each-bind-read-index/main.svelte new file mode 100644 index 000000000000..384722f5aa72 --- /dev/null +++ b/packages/svelte/tests/runtime-legacy/samples/keyed-each-bind-read-index/main.svelte @@ -0,0 +1,10 @@ + + +{#each items as item, idx(item)} + +{/each} + +

{items}

\ No newline at end of file