From 21081d0fa55363e5cdf5c6c2bea9b5353661db11 Mon Sep 17 00:00:00 2001 From: Dominic Gannaway Date: Fri, 23 Aug 2024 20:10:10 +0100 Subject: [PATCH] fix: ensure $inspect works with SvelteMap and SvelteSet (#12994) * fix: ensure $inspect works with SvelteMap and SvelteSet * build * dev only * dev only * lint * lint * lint * alternative * Update packages/svelte/src/reactivity/map.js --------- Co-authored-by: Rich Harris --- .changeset/green-baboons-sip.md | 5 ++++ packages/svelte/src/internal/shared/clone.js | 3 +++ .../samples/inspect-map-set/_config.js | 26 +++++++++++++++++++ .../samples/inspect-map-set/main.svelte | 12 +++++++++ 4 files changed, 46 insertions(+) create mode 100644 .changeset/green-baboons-sip.md create mode 100644 packages/svelte/tests/runtime-runes/samples/inspect-map-set/_config.js create mode 100644 packages/svelte/tests/runtime-runes/samples/inspect-map-set/main.svelte diff --git a/.changeset/green-baboons-sip.md b/.changeset/green-baboons-sip.md new file mode 100644 index 0000000000..91d7902867 --- /dev/null +++ b/.changeset/green-baboons-sip.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: ensure $inspect works with SvelteMap and SvelteSet diff --git a/packages/svelte/src/internal/shared/clone.js b/packages/svelte/src/internal/shared/clone.js index 9027c15c4f..e00f3e0926 100644 --- a/packages/svelte/src/internal/shared/clone.js +++ b/packages/svelte/src/internal/shared/clone.js @@ -56,6 +56,9 @@ function clone(value, cloned, path, paths) { const unwrapped = cloned.get(value); if (unwrapped !== undefined) return unwrapped; + if (value instanceof Map) return /** @type {Snapshot} */ (new Map(value)); + if (value instanceof Set) return /** @type {Snapshot} */ (new Set(value)); + if (is_array(value)) { const copy = /** @type {Snapshot} */ ([]); cloned.set(value, copy); diff --git a/packages/svelte/tests/runtime-runes/samples/inspect-map-set/_config.js b/packages/svelte/tests/runtime-runes/samples/inspect-map-set/_config.js new file mode 100644 index 0000000000..2052cb7f13 --- /dev/null +++ b/packages/svelte/tests/runtime-runes/samples/inspect-map-set/_config.js @@ -0,0 +1,26 @@ +import { flushSync } from 'svelte'; +import { test } from '../../test'; + +export default test({ + compileOptions: { + dev: true + }, + + async test({ assert, target, logs }) { + const [btn, btn2] = target.querySelectorAll('button'); + btn.click(); + btn2.click(); + flushSync(); + + assert.deepEqual(logs, [ + 'init', + new Map(), + 'init', + new Set(), + 'update', + new Map([['a', 'a']]), + 'update', + new Set(['a']) + ]); + } +}); diff --git a/packages/svelte/tests/runtime-runes/samples/inspect-map-set/main.svelte b/packages/svelte/tests/runtime-runes/samples/inspect-map-set/main.svelte new file mode 100644 index 0000000000..313b259384 --- /dev/null +++ b/packages/svelte/tests/runtime-runes/samples/inspect-map-set/main.svelte @@ -0,0 +1,12 @@ + + + +