Skip to content

Commit cb89bb8

Browse files
committed
fix: cover other case of duplicate keys
1 parent c208ef0 commit cb89bb8

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

packages/svelte/src/internal/client/dom/blocks/each.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ function reconcile(
370370
var is_animated = (flags & EACH_IS_ANIMATED) !== 0;
371371
var should_update = (flags & (EACH_ITEM_REACTIVE | EACH_INDEX_REACTIVE)) !== 0;
372372

373+
var count = 0;
373374
var length = array.length;
374375
var items = state.items;
375376
var first = state.first;
@@ -458,6 +459,7 @@ function reconcile(
458459
stashed = [];
459460

460461
current = prev.next;
462+
count += 1;
461463
continue;
462464
}
463465

@@ -519,6 +521,7 @@ function reconcile(
519521
link(state, prev, item);
520522

521523
prev = item;
524+
count += 1;
522525
}
523526

524527
continue;
@@ -547,6 +550,14 @@ function reconcile(
547550
matched.push(item);
548551
prev = item;
549552
current = item.next;
553+
count += 1;
554+
}
555+
556+
if (count !== length) {
557+
// full key uniqueness check is dev-only,
558+
// if keys duplication didn't cause a crash,
559+
// the rendered list will be shorter then the array
560+
each_key_duplicate('', '', '');
550561
}
551562

552563
if (current !== null || seen !== undefined) {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { flushSync } from 'svelte';
2+
import { test } from '../../test';
3+
4+
export default test({
5+
compileOptions: {
6+
dev: false
7+
},
8+
test({ assert, target }) {
9+
let button = target.querySelector('button');
10+
11+
button?.click();
12+
13+
assert.throws(flushSync, /each_key_duplicate/);
14+
}
15+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<script>
2+
let data = [1, 2, 3];
3+
</script>
4+
5+
<button onclick={() => data = [1, 1, 1]}>add</button>
6+
{#each data as d (d)}
7+
{d}
8+
{/each}

0 commit comments

Comments
 (0)