From 4092b7cbde8b74c88af709166a2fcd1d88c84c2a Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Thu, 29 Aug 2024 09:55:50 -0400 Subject: [PATCH] WIP --- .../svelte/src/internal/client/dev/log.js | 43 +++++++++++++++++++ packages/svelte/src/internal/client/index.js | 7 +++ 2 files changed, 50 insertions(+) create mode 100644 packages/svelte/src/internal/client/dev/log.js diff --git a/packages/svelte/src/internal/client/dev/log.js b/packages/svelte/src/internal/client/dev/log.js new file mode 100644 index 0000000000..0e5c028896 --- /dev/null +++ b/packages/svelte/src/internal/client/dev/log.js @@ -0,0 +1,43 @@ +import { STATE_SYMBOL } from '../constants.js'; + +export function monkey_patch_console() { + for (const method of Object.keys(console)) { + // @ts-expect-error + const original = console[method]; + + // @ts-expect-error + console[method] = (...args) => { + for (const arg of args) { + if (contains_state_proxy(arg)) { + // TODO make this a proper warning + console.warn('contains state proxy!!!!'); + break; + } + } + + return original.apply(console, args); + }; + } +} + +/** + * @param {any} value + * @param {Set} seen + * @returns {boolean} + */ +function contains_state_proxy(value, seen = new Set()) { + if (typeof value !== 'object' || value === null) return false; + + if (seen.has(value)) return false; + seen.add(value); + + if (STATE_SYMBOL in value) { + return true; + } + + for (const key in value) { + if (contains_state_proxy(value[key], seen)) { + return true; + } + } +} diff --git a/packages/svelte/src/internal/client/index.js b/packages/svelte/src/internal/client/index.js index 519a412486..6fc9033805 100644 --- a/packages/svelte/src/internal/client/index.js +++ b/packages/svelte/src/internal/client/index.js @@ -1,3 +1,6 @@ +import { DEV } from 'esm-env'; +import { monkey_patch_console } from './dev/log.js'; + export { FILENAME, HMR } from '../../constants.js'; export { add_locations } from './dev/elements.js'; export { hmr } from './dev/hmr.js'; @@ -164,3 +167,7 @@ export { validate_void_dynamic_element } from '../shared/validate.js'; export { strict_equals, equals } from './dev/equality.js'; + +if (DEV) { + monkey_patch_console(); +}