Skip to content

fix: correctly serialize object assignment expressions #12175

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/eleven-avocados-walk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

fix: correctly serialize object assignment expressions
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export function serialize_get_binding(node, state) {
* @param {import('estree').AssignmentExpression} node
* @param {import('zimmerframe').Context<import('#compiler').SvelteNode, State>} context
* @param {() => any} fallback
* @param {boolean} prefix
* @param {boolean | null} [prefix] - If the assignment is a transformed update expression, set this. Else `null`
* @param {{skip_proxy_and_freeze?: boolean}} [options]
* @returns {import('estree').Expression}
*/
Expand Down Expand Up @@ -419,6 +419,7 @@ export function serialize_set_binding(node, context, fallback, prefix, options)
}
} else if (
node.right.type === 'Literal' &&
prefix != null &&
(node.operator === '+=' || node.operator === '-=')
) {
return b.update(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const global_visitors = {
next();
},
AssignmentExpression(node, context) {
return serialize_set_binding(node, context, context.next, false);
return serialize_set_binding(node, context, context.next);
},
UpdateExpression(node, context) {
const { state, next, visit } = context;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -791,9 +791,7 @@ function serialize_inline_component(node, component_name, context) {
const assignment = b.assignment('=', attribute.expression, b.id('$$value'));
push_prop(
b.set(attribute.name, [
b.stmt(
serialize_set_binding(assignment, context, () => context.visit(assignment), false)
)
b.stmt(serialize_set_binding(assignment, context, () => context.visit(assignment)))
])
);
}
Expand Down Expand Up @@ -1025,7 +1023,7 @@ function serialize_bind_this(bind_this, context, node) {
const bind_this_id = /** @type {import('estree').Expression} */ (context.visit(bind_this));
const ids = Array.from(each_ids.values()).map((id) => b.id('$$value_' + id[0]));
const assignment = b.assignment('=', bind_this, b.id('$$value'));
const update = serialize_set_binding(assignment, context, () => context.visit(assignment), false);
const update = serialize_set_binding(assignment, context, () => context.visit(assignment));

for (const [binding, [, , expression]] of each_ids) {
// reset expressions to what they were before
Expand Down Expand Up @@ -2399,7 +2397,7 @@ export const template_visitors = {
if (assignment.left.type !== 'Identifier' && assignment.left.type !== 'MemberExpression') {
// serialize_set_binding turns other patterns into IIFEs and separates the assignments
// into separate expressions, at which point this is called again with an identifier or member expression
return serialize_set_binding(assignment, context, () => assignment, false);
return serialize_set_binding(assignment, context, () => assignment);
}
const left = object(assignment.left);
const value = get_assignment_value(assignment, context);
Expand Down Expand Up @@ -2780,7 +2778,7 @@ export const template_visitors = {
assignment,
context,
() => /** @type {import('estree').Expression} */ (visit(assignment)),
false,
null,
{
skip_proxy_and_freeze: true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import { test } from '../../test';

export default test({
test({ assert, logs }) {
assert.deepEqual(logs, [1, 1, 1, 1]);
assert.deepEqual(logs, [1, 1, 1, 1, 4, 4]);
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@
console.log(x++);
console.log(++o.x);
console.log(o.x++);
console.log((o.x += 2));
console.log((x += 2));
</script>
Loading