diff --git a/.changeset/itchy-games-guess.md b/.changeset/itchy-games-guess.md new file mode 100644 index 0000000000..44516cfc21 --- /dev/null +++ b/.changeset/itchy-games-guess.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: don't clone non-proxies in `$inspect` diff --git a/packages/svelte/src/internal/client/dev/inspect.js b/packages/svelte/src/internal/client/dev/inspect.js index c593f2622c..f79cf47299 100644 --- a/packages/svelte/src/internal/client/dev/inspect.js +++ b/packages/svelte/src/internal/client/dev/inspect.js @@ -26,7 +26,7 @@ export function inspect(get_value, inspector = console.log) { return; } - var snap = snapshot(value, true); + var snap = snapshot(value, true, true); untrack(() => { inspector(initial ? 'init' : 'update', ...snap); }); diff --git a/packages/svelte/src/internal/shared/clone.js b/packages/svelte/src/internal/shared/clone.js index 4632fc3d68..7dc9ffec2a 100644 --- a/packages/svelte/src/internal/shared/clone.js +++ b/packages/svelte/src/internal/shared/clone.js @@ -15,9 +15,10 @@ const empty = []; * @template T * @param {T} value * @param {boolean} [skip_warning] + * @param {boolean} [only_deproxy] * @returns {Snapshot} */ -export function snapshot(value, skip_warning = false) { +export function snapshot(value, skip_warning = false, only_deproxy = false) { if (DEV && !skip_warning) { /** @type {string[]} */ const paths = []; @@ -40,7 +41,7 @@ export function snapshot(value, skip_warning = false) { return copy; } - return clone(value, new Map(), '', empty); + return clone(value, new Map(), '', empty, null, only_deproxy); } /** @@ -49,16 +50,19 @@ export function snapshot(value, skip_warning = false) { * @param {Map>} cloned * @param {string} path * @param {string[]} paths - * @param {null | T} original The original value, if `value` was produced from a `toJSON` call + * @param {null | T} [original] The original value, if `value` was produced from a `toJSON` call + * @param {boolean} [only_deproxy] Don't clone objects that aren't proxies * @returns {Snapshot} */ -function clone(value, cloned, path, paths, original = null) { +function clone(value, cloned, path, paths, original = null, only_deproxy = false) { if (typeof value === 'object' && value !== null) { var 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 (value instanceof Map) + return /** @type {Snapshot} */ (only_deproxy ? value : new Map(value)); + if (value instanceof Set) + return /** @type {Snapshot} */ (only_deproxy ? value : new Set(value)); if (is_array(value)) { var copy = /** @type {Snapshot} */ (Array(value.length)); @@ -71,7 +75,7 @@ function clone(value, cloned, path, paths, original = null) { for (var i = 0; i < value.length; i += 1) { var element = value[i]; if (i in value) { - copy[i] = clone(element, cloned, DEV ? `${path}[${i}]` : path, paths); + copy[i] = clone(element, cloned, DEV ? `${path}[${i}]` : path, paths, null, only_deproxy); } } @@ -88,26 +92,49 @@ function clone(value, cloned, path, paths, original = null) { } for (var key in value) { - // @ts-expect-error - copy[key] = clone(value[key], cloned, DEV ? `${path}.${key}` : path, paths); + copy[key] = clone( + // @ts-expect-error + value[key], + cloned, + DEV ? `${path}.${key}` : path, + paths, + null, + only_deproxy + ); } return copy; } if (value instanceof Date) { - return /** @type {Snapshot} */ (structuredClone(value)); + if (only_deproxy) { + structuredClone(value); + } else { + return /** @type {Snapshot} */ (structuredClone(value)); + } } if (typeof (/** @type {T & { toJSON?: any } } */ (value).toJSON) === 'function') { - return clone( - /** @type {T & { toJSON(): any } } */ (value).toJSON(), - cloned, - DEV ? `${path}.toJSON()` : path, - paths, - // Associate the instance with the toJSON clone - value - ); + if (!only_deproxy) { + return clone( + /** @type {T & { toJSON(): any } } */ (value).toJSON(), + cloned, + DEV ? `${path}.toJSON()` : path, + paths, + // Associate the instance with the toJSON clone + value + ); + } else { + // we still want to read each property + clone( + /** @type {T & { toJSON(): any } } */ (value).toJSON(), + cloned, + DEV ? `${path}.toJSON()` : path, + paths, + // Associate the instance with the toJSON clone + value + ); + } } } @@ -116,6 +143,13 @@ function clone(value, cloned, path, paths, original = null) { return /** @type {Snapshot} */ (value); } + if (only_deproxy) { + try { + structuredClone(value); + } catch {} + return /** @type {Snapshot} */ (value); + } + try { return /** @type {Snapshot} */ (structuredClone(value)); } catch (e) {