fix: ensure each key validation occurs for updates (#12836)

* fix: ensure each key validation occurs for updates

* fix: ensure each key validation occurs for updates
pull/12838/head
Dominic Gannaway 4 months ago committed by GitHub
parent 555e90f932
commit 873a184b41
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: ensure each key validation occurs for updates

@ -45,28 +45,30 @@ export function validate_dynamic_component(component_fn) {
* @returns {void} * @returns {void}
*/ */
export function validate_each_keys(collection, key_fn) { export function validate_each_keys(collection, key_fn) {
const keys = new Map(); render_effect(() => {
const maybe_array = untrack(() => collection()); const keys = new Map();
const array = is_array(maybe_array) const maybe_array = collection();
? maybe_array const array = is_array(maybe_array)
: maybe_array == null ? maybe_array
? [] : maybe_array == null
: Array.from(maybe_array); ? []
const length = array.length; : Array.from(maybe_array);
for (let i = 0; i < length; i++) { const length = array.length;
const key = key_fn(array[i], i); for (let i = 0; i < length; i++) {
if (keys.has(key)) { const key = key_fn(array[i], i);
const a = String(keys.get(key)); if (keys.has(key)) {
const b = String(i); const a = String(keys.get(key));
const b = String(i);
/** @type {string | null} */
let k = String(array[i]); /** @type {string | null} */
if (k.startsWith('[object ')) k = null; let k = String(array[i]);
if (k.startsWith('[object ')) k = null;
e.each_key_duplicate(a, b, k);
e.each_key_duplicate(a, b, k);
}
keys.set(key, i);
} }
keys.set(key, i); });
}
} }
/** /**

@ -0,0 +1,16 @@
import { flushSync } from 'svelte';
import { test } from '../../test';
export default test({
compileOptions: {
dev: true
},
test({ assert, target }) {
let button = target.querySelector('button');
button?.click();
assert.throws(flushSync, /each_key_duplicate/);
}
});

@ -0,0 +1,21 @@
<script>
let data = [
[0, 0],
[0, 4],
[1, 4],
];
function add() {
const n = [0, 0]
data.push(n);
data = data;
}
</script>
<button onclick={add}>add</button>
<ul>
{#each data as d (d.join(""))}
<li> {d}</li>
{/each}
</ul>
Loading…
Cancel
Save