diff --git a/packages/svelte/src/compiler/phases/3-transform/server/visitors/AssignmentExpression.js b/packages/svelte/src/compiler/phases/3-transform/server/visitors/AssignmentExpression.js index 49a284d12b..b2ed578662 100644 --- a/packages/svelte/src/compiler/phases/3-transform/server/visitors/AssignmentExpression.js +++ b/packages/svelte/src/compiler/phases/3-transform/server/visitors/AssignmentExpression.js @@ -68,35 +68,48 @@ function build_assignment(operator, left, right, context) { object = object.object; } - if (object.type !== 'Identifier' || !is_store_name(object.name)) { + if (object.type !== 'Identifier') { return null; } - const name = object.name.slice(1); + if (is_store_name(object.name)) { + const name = object.name.slice(1); - if (!context.state.scope.get(name)) { - return null; - } + if (!context.state.scope.get(name)) { + return null; + } - if (object === left) { - let value = /** @type {Expression} */ ( - context.visit(build_assignment_value(operator, left, right)) + if (object === left) { + let value = /** @type {Expression} */ ( + context.visit(build_assignment_value(operator, left, right)) + ); + + return b.call('$.store_set', b.id(name), value); + } + + return b.call( + '$.store_mutate', + b.assignment('??=', b.id('$$store_subs'), b.object([])), + b.literal(object.name), + b.id(name), + b.assignment( + operator, + /** @type {Pattern} */ (context.visit(left)), + /** @type {Expression} */ (context.visit(right)) + ) ); + } + + const binding = context.state.scope.get(object.name); - return b.call('$.store_set', b.id(name), value); + if (binding?.kind === 'derived') { + // TODO pretty sure it's more complicated than this — need to + // handle different operators and mutations. + // (but also: when would writing to a derived during SSR ever be ok?) + return b.call(binding.node, right); } - return b.call( - '$.store_mutate', - b.assignment('??=', b.id('$$store_subs'), b.object([])), - b.literal(object.name), - b.id(name), - b.assignment( - operator, - /** @type {Pattern} */ (context.visit(left)), - /** @type {Expression} */ (context.visit(right)) - ) - ); + return null; } /** diff --git a/packages/svelte/src/compiler/phases/3-transform/shared/assignments.js b/packages/svelte/src/compiler/phases/3-transform/shared/assignments.js index 0853757775..4678d88ec5 100644 --- a/packages/svelte/src/compiler/phases/3-transform/shared/assignments.js +++ b/packages/svelte/src/compiler/phases/3-transform/shared/assignments.js @@ -3,7 +3,6 @@ /** @import { Context as ServerContext } from '../server/types.js' */ import { extract_paths, is_expression_async } from '../../../utils/ast.js'; import * as b from '#compiler/builders'; -import { get_value } from '../client/visitors/shared/declarations.js'; /** * @template {ClientContext | ServerContext} Context