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