From cf63220a0eac8ccb4f1e7fdd8bea4044dbf39f8e Mon Sep 17 00:00:00 2001 From: Dominic Gannaway Date: Tue, 14 Nov 2023 12:37:49 +0000 Subject: [PATCH] fix: ensure keyed fallback to indexed (#9441) Fixes #9408. Ensures that if we have a key that matches the index, that we fallback to using an indexed each block. --- .changeset/honest-icons-change.md | 5 +++++ .../compiler/phases/3-transform/client/visitors/template.js | 5 ++++- .../runtime-legacy/samples/keyed-each-index-same/_config.js | 5 +++++ .../runtime-legacy/samples/keyed-each-index-same/main.svelte | 3 +++ 4 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 .changeset/honest-icons-change.md create mode 100644 packages/svelte/tests/runtime-legacy/samples/keyed-each-index-same/_config.js create mode 100644 packages/svelte/tests/runtime-legacy/samples/keyed-each-index-same/main.svelte diff --git a/.changeset/honest-icons-change.md b/.changeset/honest-icons-change.md new file mode 100644 index 0000000000..6791a930d5 --- /dev/null +++ b/.changeset/honest-icons-change.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +patch: ensure keyed each block fallback to indexed each block diff --git a/packages/svelte/src/compiler/phases/3-transform/client/visitors/template.js b/packages/svelte/src/compiler/phases/3-transform/client/visitors/template.js index 66ec08d782..02483544ad 100644 --- a/packages/svelte/src/compiler/phases/3-transform/client/visitors/template.js +++ b/packages/svelte/src/compiler/phases/3-transform/client/visitors/template.js @@ -2066,7 +2066,10 @@ export const template_visitors = { /** @type {number} */ let each_type; - if (node.key) { + if ( + node.key && + (node.key.type !== 'Identifier' || !node.index || node.key.name !== node.index) + ) { each_type = EACH_KEYED; if ( node.key.type === 'Identifier' && diff --git a/packages/svelte/tests/runtime-legacy/samples/keyed-each-index-same/_config.js b/packages/svelte/tests/runtime-legacy/samples/keyed-each-index-same/_config.js new file mode 100644 index 0000000000..a1d9fe1b26 --- /dev/null +++ b/packages/svelte/tests/runtime-legacy/samples/keyed-each-index-same/_config.js @@ -0,0 +1,5 @@ +import { test } from '../../test'; + +export default test({ + html: '1' +}); diff --git a/packages/svelte/tests/runtime-legacy/samples/keyed-each-index-same/main.svelte b/packages/svelte/tests/runtime-legacy/samples/keyed-each-index-same/main.svelte new file mode 100644 index 0000000000..7a6f0e08fa --- /dev/null +++ b/packages/svelte/tests/runtime-legacy/samples/keyed-each-index-same/main.svelte @@ -0,0 +1,3 @@ +{#each [1] as item, i (i)} + {item} +{/each}