From 0d526ce5b82c7fd2f2776dffe12b140261f2b9e7 Mon Sep 17 00:00:00 2001 From: ComputerGuy <63362464+Ocean-OS@users.noreply.github.com> Date: Fri, 21 Mar 2025 18:59:49 -0700 Subject: [PATCH 01/11] init --- .changeset/sharp-rings-march.md | 5 +++++ .../phases/2-analyze/visitors/CallExpression.js | 9 +++++++++ .../3-transform/client/visitors/CallExpression.js | 15 +++++++++++++++ 3 files changed, 29 insertions(+) create mode 100644 .changeset/sharp-rings-march.md diff --git a/.changeset/sharp-rings-march.md b/.changeset/sharp-rings-march.md new file mode 100644 index 000000000000..1fb3ec079656 --- /dev/null +++ b/.changeset/sharp-rings-march.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +feat: allow `$state` in return statements diff --git a/packages/svelte/src/compiler/phases/2-analyze/visitors/CallExpression.js b/packages/svelte/src/compiler/phases/2-analyze/visitors/CallExpression.js index 6ef323725b3f..9f713c5d0660 100644 --- a/packages/svelte/src/compiler/phases/2-analyze/visitors/CallExpression.js +++ b/packages/svelte/src/compiler/phases/2-analyze/visitors/CallExpression.js @@ -104,6 +104,15 @@ export function CallExpression(node, context) { } case '$state': + if ( + (!(parent.type === 'VariableDeclarator' || parent.type === 'ReturnStatement') || + get_parent(context.path, -3).type === 'ConstTag') && + !(parent.type === 'PropertyDefinition' && !parent.static && !parent.computed) + ) { + e.state_invalid_placement(node, rune); + } + + break; case '$state.raw': case '$derived': case '$derived.by': diff --git a/packages/svelte/src/compiler/phases/3-transform/client/visitors/CallExpression.js b/packages/svelte/src/compiler/phases/3-transform/client/visitors/CallExpression.js index fda43ad7911a..1cd35e2ec0d9 100644 --- a/packages/svelte/src/compiler/phases/3-transform/client/visitors/CallExpression.js +++ b/packages/svelte/src/compiler/phases/3-transform/client/visitors/CallExpression.js @@ -4,6 +4,7 @@ import { dev, is_ignored } from '../../../../state.js'; import * as b from '../../../../utils/builders.js'; import { get_rune } from '../../../scope.js'; import { transform_inspect_rune } from '../../utils.js'; +import { should_proxy } from '../utils.js'; /** * @param {CallExpression} node @@ -33,6 +34,20 @@ export function CallExpression(node, context) { case '$inspect': case '$inspect().with': return transform_inspect_rune(node, context); + case '$state': + if (context.path.at(-1)?.type === 'ReturnStatement') { + if ( + node.arguments[0] && + should_proxy( + /** @type {Expression} */ (context.visit(node.arguments[0])), + context.state.scope + ) + ) { + return b.call('$.proxy', node.arguments[0]); + } else { + return node.arguments[0] ?? b.void0; + } + } } if ( From 5c3a1534cc524dabc04d4168c800c987d7be092d Mon Sep 17 00:00:00 2001 From: ComputerGuy <63362464+Ocean-OS@users.noreply.github.com> Date: Fri, 21 Mar 2025 19:50:49 -0700 Subject: [PATCH 02/11] oops --- .../src/compiler/phases/2-analyze/visitors/CallExpression.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/svelte/src/compiler/phases/2-analyze/visitors/CallExpression.js b/packages/svelte/src/compiler/phases/2-analyze/visitors/CallExpression.js index 2c4928a30d8c..8be793c4a50b 100644 --- a/packages/svelte/src/compiler/phases/2-analyze/visitors/CallExpression.js +++ b/packages/svelte/src/compiler/phases/2-analyze/visitors/CallExpression.js @@ -120,6 +120,10 @@ export function CallExpression(node, context) { e.state_invalid_placement(node, rune); } + if (node.arguments.length > 1) { + e.rune_invalid_arguments_length(node, rune, 'zero or one arguments'); + } + break; case '$state.raw': case '$derived': From 7adad75e7f973136cb35afd939d51d538bd6b106 Mon Sep 17 00:00:00 2001 From: ComputerGuy <63362464+Ocean-OS@users.noreply.github.com> Date: Sat, 22 Mar 2025 20:20:35 -0700 Subject: [PATCH 03/11] support implicit returns in arrow functions --- .../compiler/phases/2-analyze/visitors/CallExpression.js | 3 ++- .../phases/3-transform/client/visitors/CallExpression.js | 6 +++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/svelte/src/compiler/phases/2-analyze/visitors/CallExpression.js b/packages/svelte/src/compiler/phases/2-analyze/visitors/CallExpression.js index 8be793c4a50b..995b1d79f831 100644 --- a/packages/svelte/src/compiler/phases/2-analyze/visitors/CallExpression.js +++ b/packages/svelte/src/compiler/phases/2-analyze/visitors/CallExpression.js @@ -115,7 +115,8 @@ export function CallExpression(node, context) { if ( (!(parent.type === 'VariableDeclarator' || parent.type === 'ReturnStatement') || get_parent(context.path, -3).type === 'ConstTag') && - !(parent.type === 'PropertyDefinition' && !parent.static && !parent.computed) + !(parent.type === 'PropertyDefinition' && !parent.static && !parent.computed) && + !(parent.type === 'ArrowFunctionExpression' && parent.body === node) ) { e.state_invalid_placement(node, rune); } diff --git a/packages/svelte/src/compiler/phases/3-transform/client/visitors/CallExpression.js b/packages/svelte/src/compiler/phases/3-transform/client/visitors/CallExpression.js index 1cd35e2ec0d9..aa718b9988a8 100644 --- a/packages/svelte/src/compiler/phases/3-transform/client/visitors/CallExpression.js +++ b/packages/svelte/src/compiler/phases/3-transform/client/visitors/CallExpression.js @@ -11,6 +11,7 @@ import { should_proxy } from '../utils.js'; * @param {Context} context */ export function CallExpression(node, context) { + const parent = context.path.at(-1); switch (get_rune(node, context.state.scope)) { case '$host': return b.id('$$props.$$host'); @@ -35,7 +36,10 @@ export function CallExpression(node, context) { case '$inspect().with': return transform_inspect_rune(node, context); case '$state': - if (context.path.at(-1)?.type === 'ReturnStatement') { + if ( + parent?.type === 'ReturnStatement' || + (parent?.type === 'ArrowFunctionExpression' && parent.body === node) + ) { if ( node.arguments[0] && should_proxy( From 4d11b5656e0aeec3c5964c290193908e2afdd8ca Mon Sep 17 00:00:00 2001 From: ComputerGuy <63362464+Ocean-OS@users.noreply.github.com> Date: Fri, 28 Mar 2025 19:43:09 -0700 Subject: [PATCH 04/11] add tests, add to server --- .../3-transform/server/visitors/CallExpression.js | 14 +++++++++++++- .../_expected/client/index.svelte.js | 14 ++++++++++++++ .../_expected/server/index.svelte.js | 14 ++++++++++++++ .../samples/state-in-return/index.svelte.js | 8 ++++++++ 4 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 packages/svelte/tests/snapshot/samples/state-in-return/_expected/client/index.svelte.js create mode 100644 packages/svelte/tests/snapshot/samples/state-in-return/_expected/server/index.svelte.js create mode 100644 packages/svelte/tests/snapshot/samples/state-in-return/index.svelte.js diff --git a/packages/svelte/src/compiler/phases/3-transform/server/visitors/CallExpression.js b/packages/svelte/src/compiler/phases/3-transform/server/visitors/CallExpression.js index a425bc5ec430..ad573b03295b 100644 --- a/packages/svelte/src/compiler/phases/3-transform/server/visitors/CallExpression.js +++ b/packages/svelte/src/compiler/phases/3-transform/server/visitors/CallExpression.js @@ -1,4 +1,4 @@ -/** @import { CallExpression, Expression } from 'estree' */ +/** @import { ArrowFunctionExpression, CallExpression, Expression } from 'estree' */ /** @import { Context } from '../types.js' */ import { is_ignored } from '../../../../state.js'; import * as b from '../../../../utils/builders.js'; @@ -37,5 +37,17 @@ export function CallExpression(node, context) { return transform_inspect_rune(node, context); } + if ( + rune === '$state' && + (context.path.at(-1)?.type === 'ReturnStatement' || + (context.path.at(-1)?.type === 'ArrowFunctionExpression' && + /** @type {ArrowFunctionExpression} */ (context.path.at(-1)).body === node)) + ) { + if (node.arguments[0]) { + return context.visit(node.arguments[0]); + } + return b.void0; + } + context.next(); } diff --git a/packages/svelte/tests/snapshot/samples/state-in-return/_expected/client/index.svelte.js b/packages/svelte/tests/snapshot/samples/state-in-return/_expected/client/index.svelte.js new file mode 100644 index 000000000000..55f38f174e19 --- /dev/null +++ b/packages/svelte/tests/snapshot/samples/state-in-return/_expected/client/index.svelte.js @@ -0,0 +1,14 @@ +/* index.svelte.js generated by Svelte VERSION */ +import * as $ from 'svelte/internal/client'; + +export default function proxy(object) { + return $.proxy(object); +} + +export function createCounter() { + let count = $.state(0); + + $.update(count); +} + +export const proxy_in_arrow = (object) => $.proxy(object); \ No newline at end of file diff --git a/packages/svelte/tests/snapshot/samples/state-in-return/_expected/server/index.svelte.js b/packages/svelte/tests/snapshot/samples/state-in-return/_expected/server/index.svelte.js new file mode 100644 index 000000000000..de52cde883be --- /dev/null +++ b/packages/svelte/tests/snapshot/samples/state-in-return/_expected/server/index.svelte.js @@ -0,0 +1,14 @@ +/* index.svelte.js generated by Svelte VERSION */ +import * as $ from 'svelte/internal/server'; + +export default function proxy(object) { + return object; +} + +export function createCounter() { + let count = 0; + + count++; +} + +export const proxy_in_arrow = (object) => object; \ No newline at end of file diff --git a/packages/svelte/tests/snapshot/samples/state-in-return/index.svelte.js b/packages/svelte/tests/snapshot/samples/state-in-return/index.svelte.js new file mode 100644 index 000000000000..f30cdd0408b9 --- /dev/null +++ b/packages/svelte/tests/snapshot/samples/state-in-return/index.svelte.js @@ -0,0 +1,8 @@ +export default function proxy(object) { + return $state(object); +} +export function createCounter() { + let count = $state(0); + count++; +} +export const proxy_in_arrow = object => $state(object); \ No newline at end of file From e13c7d9b22c89e598dabcece31f2dd480520f7c8 Mon Sep 17 00:00:00 2001 From: ComputerGuy <63362464+Ocean-OS@users.noreply.github.com> Date: Fri, 28 Mar 2025 19:46:47 -0700 Subject: [PATCH 05/11] lint --- .../snapshot/samples/state-in-return/index.svelte.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/svelte/tests/snapshot/samples/state-in-return/index.svelte.js b/packages/svelte/tests/snapshot/samples/state-in-return/index.svelte.js index f30cdd0408b9..66d143f4096e 100644 --- a/packages/svelte/tests/snapshot/samples/state-in-return/index.svelte.js +++ b/packages/svelte/tests/snapshot/samples/state-in-return/index.svelte.js @@ -1,8 +1,8 @@ export default function proxy(object) { - return $state(object); + return $state(object); } export function createCounter() { - let count = $state(0); - count++; + let count = $state(0); + count++; } -export const proxy_in_arrow = object => $state(object); \ No newline at end of file +export const proxy_in_arrow = (object) => $state(object); From 8f715843a35a2a372fe15fbff6349a3f0f0c61eb Mon Sep 17 00:00:00 2001 From: ComputerGuy <63362464+Ocean-OS@users.noreply.github.com> Date: Tue, 8 Apr 2025 15:13:05 -0700 Subject: [PATCH 06/11] tweak changeset --- .changeset/sharp-rings-march.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/sharp-rings-march.md b/.changeset/sharp-rings-march.md index 1fb3ec079656..bd3a5e531210 100644 --- a/.changeset/sharp-rings-march.md +++ b/.changeset/sharp-rings-march.md @@ -1,5 +1,5 @@ --- -'svelte': patch +'svelte': minor --- feat: allow `$state` in return statements From 967ccc5d86a71f319f265d725d7eee327352b810 Mon Sep 17 00:00:00 2001 From: ComputerGuy <63362464+Ocean-OS@users.noreply.github.com> Date: Tue, 29 Apr 2025 17:08:20 -0700 Subject: [PATCH 07/11] add warning in dev, add docs --- documentation/docs/02-runes/02-$state.md | 13 +++++++++++++ .../98-reference/.generated/client-warnings.md | 6 ++++++ .../svelte/messages/client-warnings/warnings.md | 4 ++++ .../client/visitors/CallExpression.js | 15 +++++---------- packages/svelte/src/internal/client/proxy.js | 16 ++++++++++++++++ packages/svelte/src/internal/client/warnings.js | 11 +++++++++++ .../_expected/client/index.svelte.js | 4 ++-- 7 files changed, 57 insertions(+), 12 deletions(-) diff --git a/documentation/docs/02-runes/02-$state.md b/documentation/docs/02-runes/02-$state.md index 16630a977b62..e98c66f08ffb 100644 --- a/documentation/docs/02-runes/02-$state.md +++ b/documentation/docs/02-runes/02-$state.md @@ -65,6 +65,19 @@ let { done, text } = todos[0]; todos[0].done = !todos[0].done; ``` +You can also use `$state` in return statements to proxy their argument: + +```js +function createCounter() { + return $state({ + count: 0, + increment() { + this.count++; + } + }); +} +``` + ### Classes You can also use `$state` in class fields (whether public or private): diff --git a/documentation/docs/98-reference/.generated/client-warnings.md b/documentation/docs/98-reference/.generated/client-warnings.md index 77d1df4cdde2..92b25f806c85 100644 --- a/documentation/docs/98-reference/.generated/client-warnings.md +++ b/documentation/docs/98-reference/.generated/client-warnings.md @@ -219,6 +219,12 @@ Reactive `$state(...)` proxies and the values they proxy have different identiti To resolve this, ensure you're comparing values where both values were created with `$state(...)`, or neither were. Note that `$state.raw(...)` will _not_ create a state proxy. +### state_return_not_proxyable + +``` +The argument passed to a `$state` in a return statement must be a plain object or array. Otherwise, the `$state` call will have no effect +``` + ### transition_slide_display ``` diff --git a/packages/svelte/messages/client-warnings/warnings.md b/packages/svelte/messages/client-warnings/warnings.md index f8e9ebd8a047..7c644409ce43 100644 --- a/packages/svelte/messages/client-warnings/warnings.md +++ b/packages/svelte/messages/client-warnings/warnings.md @@ -185,6 +185,10 @@ To fix it, either create callback props to communicate changes, or mark `person` To resolve this, ensure you're comparing values where both values were created with `$state(...)`, or neither were. Note that `$state.raw(...)` will _not_ create a state proxy. +## state_return_not_proxyable + +> The argument passed to a `$state` call in a return statement must be a plain object or array. Otherwise, the `$state` call will have no effect + ## transition_slide_display > The `slide` transition does not work correctly for elements with `display: %value%` diff --git a/packages/svelte/src/compiler/phases/3-transform/client/visitors/CallExpression.js b/packages/svelte/src/compiler/phases/3-transform/client/visitors/CallExpression.js index b381f56504ef..5e16a5055060 100644 --- a/packages/svelte/src/compiler/phases/3-transform/client/visitors/CallExpression.js +++ b/packages/svelte/src/compiler/phases/3-transform/client/visitors/CallExpression.js @@ -40,16 +40,11 @@ export function CallExpression(node, context) { parent?.type === 'ReturnStatement' || (parent?.type === 'ArrowFunctionExpression' && parent.body === node) ) { - if ( - node.arguments[0] && - should_proxy( - /** @type {Expression} */ (context.visit(node.arguments[0])), - context.state.scope - ) - ) { - return b.call('$.proxy', node.arguments[0]); - } else { - return node.arguments[0] ?? b.void0; + if (node.arguments[0]) { + return b.call( + '$.return_proxy', + /** @type {Expression} */ (context.visit(node.arguments[0] ?? b.void0)) + ); } } } diff --git a/packages/svelte/src/internal/client/proxy.js b/packages/svelte/src/internal/client/proxy.js index fd5706eaf270..515531e87671 100644 --- a/packages/svelte/src/internal/client/proxy.js +++ b/packages/svelte/src/internal/client/proxy.js @@ -12,6 +12,7 @@ import { state as source, set } from './reactivity/sources.js'; import { STATE_SYMBOL } from '#client/constants'; import { UNINITIALIZED } from '../../constants.js'; import * as e from './errors.js'; +import * as w from './warnings.js'; import { get_stack } from './dev/tracing.js'; import { tracing_mode_flag } from '../flags/index.js'; @@ -282,6 +283,21 @@ export function proxy(value) { }); } +/** + * @template T + * @param {T} value + * @param {Source} [prev] + * @returns {T | void} + */ +export function return_proxy(value, prev) { + const res = proxy(value, prev); + if (res !== value || (typeof value === 'object' && value !== null && STATE_SYMBOL in value)) { + // if the argument passed was already a proxy, we don't warn + return res; + } + w.state_return_not_proxyable(); +} + /** * @param {Source} signal * @param {1 | -1} [d] diff --git a/packages/svelte/src/internal/client/warnings.js b/packages/svelte/src/internal/client/warnings.js index c84b487e280d..5d41c201b43f 100644 --- a/packages/svelte/src/internal/client/warnings.js +++ b/packages/svelte/src/internal/client/warnings.js @@ -170,6 +170,17 @@ export function state_proxy_equality_mismatch(operator) { } } +/** + * The argument passed to a `$state` in a return statement must be a plain object or array. Otherwise, the `$state` call will have no effect + */ +export function state_return_not_proxyable() { + if (DEV) { + console.warn(`%c[svelte] state_return_not_proxyable\n%cThe argument passed to a \`$state\` in a return statement must be a plain object or array. Otherwise, the \`$state\` call will have no effect\nhttps://svelte.dev/e/state_return_not_proxyable`, bold, normal); + } else { + console.warn(`https://svelte.dev/e/state_return_not_proxyable`); + } +} + /** * The `slide` transition does not work correctly for elements with `display: %value%` * @param {string} value diff --git a/packages/svelte/tests/snapshot/samples/state-in-return/_expected/client/index.svelte.js b/packages/svelte/tests/snapshot/samples/state-in-return/_expected/client/index.svelte.js index 55f38f174e19..de55e87b536a 100644 --- a/packages/svelte/tests/snapshot/samples/state-in-return/_expected/client/index.svelte.js +++ b/packages/svelte/tests/snapshot/samples/state-in-return/_expected/client/index.svelte.js @@ -2,7 +2,7 @@ import * as $ from 'svelte/internal/client'; export default function proxy(object) { - return $.proxy(object); + return $.return_proxy(object); } export function createCounter() { @@ -11,4 +11,4 @@ export function createCounter() { $.update(count); } -export const proxy_in_arrow = (object) => $.proxy(object); \ No newline at end of file +export const proxy_in_arrow = (object) => $.return_proxy(object); \ No newline at end of file From d28fe7e1520aea39fab7125f9524fa3ccd196e3a Mon Sep 17 00:00:00 2001 From: ComputerGuy <63362464+Ocean-OS@users.noreply.github.com> Date: Tue, 29 Apr 2025 17:11:03 -0700 Subject: [PATCH 08/11] doh --- packages/svelte/src/internal/client/index.js | 2 +- packages/svelte/src/internal/client/proxy.js | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/packages/svelte/src/internal/client/index.js b/packages/svelte/src/internal/client/index.js index 14d6e29f5bb4..eb5321c9409e 100644 --- a/packages/svelte/src/internal/client/index.js +++ b/packages/svelte/src/internal/client/index.js @@ -141,7 +141,7 @@ export { } from './runtime.js'; export { validate_binding, validate_each_keys } from './validate.js'; export { raf } from './timing.js'; -export { proxy } from './proxy.js'; +export { proxy, return_proxy } from './proxy.js'; export { create_custom_element } from './dom/elements/custom-element.js'; export { child, diff --git a/packages/svelte/src/internal/client/proxy.js b/packages/svelte/src/internal/client/proxy.js index 515531e87671..51692f3ce947 100644 --- a/packages/svelte/src/internal/client/proxy.js +++ b/packages/svelte/src/internal/client/proxy.js @@ -286,11 +286,10 @@ export function proxy(value) { /** * @template T * @param {T} value - * @param {Source} [prev] * @returns {T | void} */ -export function return_proxy(value, prev) { - const res = proxy(value, prev); +export function return_proxy(value) { + const res = proxy(value); if (res !== value || (typeof value === 'object' && value !== null && STATE_SYMBOL in value)) { // if the argument passed was already a proxy, we don't warn return res; From 8ebcd913ec831562d2d343eeaf3c4ed84a66c7d7 Mon Sep 17 00:00:00 2001 From: ComputerGuy <63362464+Ocean-OS@users.noreply.github.com> Date: Tue, 29 Apr 2025 17:17:57 -0700 Subject: [PATCH 09/11] rewrite logic --- packages/svelte/src/internal/client/proxy.js | 31 +++++++++++++------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/packages/svelte/src/internal/client/proxy.js b/packages/svelte/src/internal/client/proxy.js index 51692f3ce947..f9afaef3aa98 100644 --- a/packages/svelte/src/internal/client/proxy.js +++ b/packages/svelte/src/internal/client/proxy.js @@ -16,6 +16,21 @@ import * as w from './warnings.js'; import { get_stack } from './dev/tracing.js'; import { tracing_mode_flag } from '../flags/index.js'; +/** + * @param {unknown} value + * @returns {boolean} + */ +function should_proxy(value) { + if (typeof value !== 'object' || value === null || STATE_SYMBOL in value) { + return false; + } + const prototype = get_prototype_of(value); + if (prototype !== object_prototype && prototype !== array_prototype) { + return false; + } + return true; +} + /** * @template T * @param {T} value @@ -23,13 +38,7 @@ import { tracing_mode_flag } from '../flags/index.js'; */ export function proxy(value) { // if non-proxyable, or is already a proxy, return `value` - if (typeof value !== 'object' || value === null || STATE_SYMBOL in value) { - return value; - } - - const prototype = get_prototype_of(value); - - if (prototype !== object_prototype && prototype !== array_prototype) { + if (!should_proxy(value)) { return value; } @@ -289,10 +298,12 @@ export function proxy(value) { * @returns {T | void} */ export function return_proxy(value) { - const res = proxy(value); - if (res !== value || (typeof value === 'object' && value !== null && STATE_SYMBOL in value)) { + if ( + !should_proxy(value) && + !(typeof value === 'object' && value !== null && STATE_SYMBOL in value) + ) { // if the argument passed was already a proxy, we don't warn - return res; + return proxy(value); } w.state_return_not_proxyable(); } From b64fb09028a9d35b089b80522278fce93f9c4266 Mon Sep 17 00:00:00 2001 From: ComputerGuy <63362464+Ocean-OS@users.noreply.github.com> Date: Tue, 29 Apr 2025 17:23:09 -0700 Subject: [PATCH 10/11] `node scripts/process-messages` --- documentation/docs/98-reference/.generated/client-warnings.md | 2 +- packages/svelte/src/internal/client/warnings.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/documentation/docs/98-reference/.generated/client-warnings.md b/documentation/docs/98-reference/.generated/client-warnings.md index 92b25f806c85..7bfc20e58905 100644 --- a/documentation/docs/98-reference/.generated/client-warnings.md +++ b/documentation/docs/98-reference/.generated/client-warnings.md @@ -222,7 +222,7 @@ To resolve this, ensure you're comparing values where both values were created w ### state_return_not_proxyable ``` -The argument passed to a `$state` in a return statement must be a plain object or array. Otherwise, the `$state` call will have no effect +The argument passed to a `$state` call in a return statement must be a plain object or array. Otherwise, the `$state` call will have no effect ``` ### transition_slide_display diff --git a/packages/svelte/src/internal/client/warnings.js b/packages/svelte/src/internal/client/warnings.js index 5d41c201b43f..acb9f82c0cb6 100644 --- a/packages/svelte/src/internal/client/warnings.js +++ b/packages/svelte/src/internal/client/warnings.js @@ -171,11 +171,11 @@ export function state_proxy_equality_mismatch(operator) { } /** - * The argument passed to a `$state` in a return statement must be a plain object or array. Otherwise, the `$state` call will have no effect + * The argument passed to a `$state` call in a return statement must be a plain object or array. Otherwise, the `$state` call will have no effect */ export function state_return_not_proxyable() { if (DEV) { - console.warn(`%c[svelte] state_return_not_proxyable\n%cThe argument passed to a \`$state\` in a return statement must be a plain object or array. Otherwise, the \`$state\` call will have no effect\nhttps://svelte.dev/e/state_return_not_proxyable`, bold, normal); + console.warn(`%c[svelte] state_return_not_proxyable\n%cThe argument passed to a \`$state\` call in a return statement must be a plain object or array. Otherwise, the \`$state\` call will have no effect\nhttps://svelte.dev/e/state_return_not_proxyable`, bold, normal); } else { console.warn(`https://svelte.dev/e/state_return_not_proxyable`); } From 5193f2c28922ce56ff89a73910fea22e269fad4f Mon Sep 17 00:00:00 2001 From: ComputerGuy <63362464+Ocean-OS@users.noreply.github.com> Date: Sun, 4 May 2025 12:37:26 -0700 Subject: [PATCH 11/11] fix, only warn in dev --- packages/svelte/src/internal/client/proxy.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/packages/svelte/src/internal/client/proxy.js b/packages/svelte/src/internal/client/proxy.js index f9afaef3aa98..9499e7f9ddb9 100644 --- a/packages/svelte/src/internal/client/proxy.js +++ b/packages/svelte/src/internal/client/proxy.js @@ -298,14 +298,13 @@ export function proxy(value) { * @returns {T | void} */ export function return_proxy(value) { - if ( - !should_proxy(value) && - !(typeof value === 'object' && value !== null && STATE_SYMBOL in value) - ) { - // if the argument passed was already a proxy, we don't warn + if (should_proxy(value)) { return proxy(value); + } else if (DEV && !(typeof value === 'object' && value !== null && STATE_SYMBOL in value)) { + // if the argument passed was already a proxy, we don't warn + w.state_return_not_proxyable(); } - w.state_return_not_proxyable(); + return value; } /**