Skip to content

Commit 2ac038c

Browse files
committed
fix: ensure $state.snapshot clones holey arrays correctly
1 parent ab1f7f4 commit 2ac038c

File tree

4 files changed

+24
-2
lines changed

4 files changed

+24
-2
lines changed

.changeset/clean-bobcats-fail.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: ensure $state.snapshot clones holey arrays correctly

packages/svelte/src/internal/shared/clone.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,17 @@ function clone(value, cloned, path, paths, original = null) {
6161
if (value instanceof Set) return /** @type {Snapshot<T>} */ (new Set(value));
6262

6363
if (is_array(value)) {
64-
const copy = /** @type {Snapshot<any>} */ ([]);
64+
const copy = /** @type {Snapshot<any>} */ (Array(value.length));
6565
cloned.set(value, copy);
6666

6767
if (original !== null) {
6868
cloned.set(original, copy);
6969
}
7070

7171
for (let i = 0; i < value.length; i += 1) {
72-
copy.push(clone(value[i], cloned, DEV ? `${path}[${i}]` : path, paths));
72+
if (value[i] !== undefined) {
73+
copy[i] = clone(value[i], cloned, DEV ? `${path}[${i}]` : path, paths);
74+
}
7375
}
7476

7577
return copy;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { test } from '../../test';
2+
3+
export default test({
4+
html: `<div>false</div><div>true</div>`
5+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<script>
2+
let arr = []
3+
arr[5] = true
4+
5+
let state = $state([])
6+
state[5] = true
7+
</script>
8+
9+
<div>{2 in $state.snapshot(state)}</div>
10+
<div>{5 in $state.snapshot(state)}</div>

0 commit comments

Comments
 (0)