fix: ensure $state.snapshot clones holey arrays correctly

pull/14657/head
Dominic Gannaway 2 years ago
parent ab1f7f437e
commit 2ac038c05a

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: ensure $state.snapshot clones holey arrays correctly

@ -61,7 +61,7 @@ function clone(value, cloned, path, paths, original = null) {
if (value instanceof Set) return /** @type {Snapshot<T>} */ (new Set(value));
if (is_array(value)) {
const copy = /** @type {Snapshot<any>} */ ([]);
const copy = /** @type {Snapshot<any>} */ (Array(value.length));
cloned.set(value, copy);
if (original !== null) {
@ -69,7 +69,9 @@ function clone(value, cloned, path, paths, original = null) {
}
for (let i = 0; i < value.length; i += 1) {
copy.push(clone(value[i], cloned, DEV ? `${path}[${i}]` : path, paths));
if (value[i] !== undefined) {
copy[i] = clone(value[i], cloned, DEV ? `${path}[${i}]` : path, paths);
}
}
return copy;

@ -0,0 +1,5 @@
import { test } from '../../test';
export default test({
html: `<div>false</div><div>true</div>`
});

@ -0,0 +1,10 @@
<script>
let arr = []
arr[5] = true
let state = $state([])
state[5] = true
</script>
<div>{2 in $state.snapshot(state)}</div>
<div>{5 in $state.snapshot(state)}</div>
Loading…
Cancel
Save