Skip to content

Commit 07662d4

Browse files
committed
fix: correctly serialize object assignment expressions
fixes #12174 #12109 didn't take into account actual object assignments and couldn't differentiate them from our "fake" assignments, this fixes that
1 parent 879b011 commit 07662d4

File tree

6 files changed

+15
-9
lines changed

6 files changed

+15
-9
lines changed

.changeset/eleven-avocados-walk.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"svelte": patch
3+
---
4+
5+
fix: correctly serialize object assignment expressions

packages/svelte/src/compiler/phases/3-transform/client/utils.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ export function serialize_get_binding(node, state) {
120120
* @param {import('estree').AssignmentExpression} node
121121
* @param {import('zimmerframe').Context<import('#compiler').SvelteNode, State>} context
122122
* @param {() => any} fallback
123-
* @param {boolean} prefix
123+
* @param {boolean | null} [prefix] - If the assignment is a transformed update expression, set this. Else `null`
124124
* @param {{skip_proxy_and_freeze?: boolean}} [options]
125125
* @returns {import('estree').Expression}
126126
*/
@@ -419,6 +419,7 @@ export function serialize_set_binding(node, context, fallback, prefix, options)
419419
}
420420
} else if (
421421
node.right.type === 'Literal' &&
422+
prefix != null &&
422423
(node.operator === '+=' || node.operator === '-=')
423424
) {
424425
return b.update(

packages/svelte/src/compiler/phases/3-transform/client/visitors/global.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export const global_visitors = {
3232
next();
3333
},
3434
AssignmentExpression(node, context) {
35-
return serialize_set_binding(node, context, context.next, false);
35+
return serialize_set_binding(node, context, context.next);
3636
},
3737
UpdateExpression(node, context) {
3838
const { state, next, visit } = context;

packages/svelte/src/compiler/phases/3-transform/client/visitors/template.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -791,9 +791,7 @@ function serialize_inline_component(node, component_name, context) {
791791
const assignment = b.assignment('=', attribute.expression, b.id('$$value'));
792792
push_prop(
793793
b.set(attribute.name, [
794-
b.stmt(
795-
serialize_set_binding(assignment, context, () => context.visit(assignment), false)
796-
)
794+
b.stmt(serialize_set_binding(assignment, context, () => context.visit(assignment)))
797795
])
798796
);
799797
}
@@ -1025,7 +1023,7 @@ function serialize_bind_this(bind_this, context, node) {
10251023
const bind_this_id = /** @type {import('estree').Expression} */ (context.visit(bind_this));
10261024
const ids = Array.from(each_ids.values()).map((id) => b.id('$$value_' + id[0]));
10271025
const assignment = b.assignment('=', bind_this, b.id('$$value'));
1028-
const update = serialize_set_binding(assignment, context, () => context.visit(assignment), false);
1026+
const update = serialize_set_binding(assignment, context, () => context.visit(assignment));
10291027

10301028
for (const [binding, [, , expression]] of each_ids) {
10311029
// reset expressions to what they were before
@@ -2399,7 +2397,7 @@ export const template_visitors = {
23992397
if (assignment.left.type !== 'Identifier' && assignment.left.type !== 'MemberExpression') {
24002398
// serialize_set_binding turns other patterns into IIFEs and separates the assignments
24012399
// into separate expressions, at which point this is called again with an identifier or member expression
2402-
return serialize_set_binding(assignment, context, () => assignment, false);
2400+
return serialize_set_binding(assignment, context, () => assignment);
24032401
}
24042402
const left = object(assignment.left);
24052403
const value = get_assignment_value(assignment, context);
@@ -2780,7 +2778,7 @@ export const template_visitors = {
27802778
assignment,
27812779
context,
27822780
() => /** @type {import('estree').Expression} */ (visit(assignment)),
2783-
false,
2781+
null,
27842782
{
27852783
skip_proxy_and_freeze: true
27862784
}

packages/svelte/tests/runtime-runes/samples/state-update/_config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ import { test } from '../../test';
22

33
export default test({
44
test({ assert, logs }) {
5-
assert.deepEqual(logs, [1, 1, 1, 1]);
5+
assert.deepEqual(logs, [1, 1, 1, 1, 4, 4]);
66
}
77
});

packages/svelte/tests/runtime-runes/samples/state-update/main.svelte

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@
66
console.log(x++);
77
console.log(++o.x);
88
console.log(o.x++);
9+
console.log((o.x += 2));
10+
console.log((x += 2));
911
</script>

0 commit comments

Comments
 (0)