Skip to content

Commit ec016eb

Browse files
committed
fix: emit each_key_duplicate error in production
1 parent d92fa43 commit ec016eb

File tree

4 files changed

+41
-0
lines changed

4 files changed

+41
-0
lines changed

.changeset/sharp-snakes-poke.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: emit `each_key_duplicate` error in production

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import { active_effect, get } from '../../runtime.js';
4242
import { DEV } from 'esm-env';
4343
import { derived_safe_equal } from '../../reactivity/deriveds.js';
4444
import { current_batch } from '../../reactivity/batch.js';
45+
import { each_key_duplicate } from '../../errors.js';
4546

4647
/**
4748
* The row of a keyed each block that is currently updating. We track this
@@ -473,6 +474,21 @@ function reconcile(
473474
var start = stashed[0];
474475
var j;
475476

477+
// full key uniqueness check is dev-only,
478+
// key duplicates cause crushing only due to `matched` being empty
479+
if (matched.length === 0) {
480+
var map = new Map();
481+
for (j = 0; j < length; j += 1) {
482+
var k = get_key(array[j], j);
483+
if (map.has(k)) {
484+
k = String(k);
485+
if (k.startsWith('[object ')) k = null;
486+
each_key_duplicate(String(j), String(map.get(k)), k);
487+
}
488+
map.set(k, j);
489+
}
490+
}
491+
476492
prev = start.prev;
477493

478494
var a = matched[0];
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { flushSync } from 'svelte';
2+
import { test } from '../../test';
3+
4+
export default test({
5+
test({ assert, target }) {
6+
let button = target.querySelector('button');
7+
8+
button?.click();
9+
10+
assert.throws(flushSync, /each_key_duplicate/);
11+
}
12+
});
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, 3, 1]}>add</button>
6+
{#each data as d (d)}
7+
{d}
8+
{/each}

0 commit comments

Comments
 (0)