Skip to content

Commit 3a90f07

Browse files
committed
tweak
1 parent bbdad4b commit 3a90f07

File tree

1 file changed

+24
-21
lines changed

1 file changed

+24
-21
lines changed

packages/svelte/src/compiler/phases/3-transform/server/visitors/AssignmentExpression.js

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/** @import { AssignmentExpression, BinaryOperator, Expression, Node, Pattern } from 'estree' */
1+
/** @import { AssignmentExpression, AssignmentOperator, BinaryOperator, Expression, Node, Pattern } from 'estree' */
22
/** @import { SvelteNode } from '#compiler' */
33
/** @import { Context, ServerTransformState } from '../types.js' */
44
import * as b from '../../../../utils/builders.js';
@@ -25,9 +25,10 @@ export function AssignmentExpression(node, context) {
2525
let unchanged = 0;
2626

2727
const assignments = extract_paths(node.left).map((path) => {
28-
const assignment = b.assignment('=', path.node, path.expression?.(rhs));
28+
const value = path.expression?.(rhs);
29+
const assignment = b.assignment('=', path.node, value);
2930

30-
return serialize_assignment(assignment, context, () => {
31+
return serialize_assignment('=', path.node, value, context, () => {
3132
unchanged += 1;
3233
return assignment;
3334
});
@@ -53,42 +54,44 @@ export function AssignmentExpression(node, context) {
5354
return sequence;
5455
}
5556

56-
return serialize_assignment(node, context, context.next);
57+
return serialize_assignment(node.operator, node.left, node.right, context, context.next);
5758
}
5859

5960
/**
60-
* @param {AssignmentExpression} node
61+
* @param {AssignmentOperator} operator
62+
* @param {Pattern} left
63+
* @param {Expression} right
6164
* @param {import('zimmerframe').Context<SvelteNode, ServerTransformState>} context
6265
* @param {() => any} fallback
6366
* @returns {Expression}
6467
*/
65-
function serialize_assignment(node, context, fallback) {
66-
let left = node.left;
68+
function serialize_assignment(operator, left, right, context, fallback) {
69+
let object = left;
6770

68-
while (left.type === 'MemberExpression') {
71+
while (object.type === 'MemberExpression') {
6972
// @ts-expect-error
70-
left = left.object;
73+
object = object.object;
7174
}
7275

73-
if (left.type !== 'Identifier' || !is_store_name(left.name)) {
76+
if (object.type !== 'Identifier' || !is_store_name(object.name)) {
7477
return fallback();
7578
}
7679

77-
const name = left.name.slice(1);
80+
const name = object.name.slice(1);
7881

7982
if (!context.state.scope.get(name)) {
8083
return fallback();
8184
}
8285

83-
if (left === node.left) {
86+
if (object === left) {
8487
const value =
85-
node.operator === '='
86-
? /** @type {Expression} */ (context.visit(node.right))
88+
operator === '='
89+
? /** @type {Expression} */ (context.visit(right))
8790
: // turn something like x += 1 into x = x + 1
8891
b.binary(
89-
/** @type {BinaryOperator} */ (node.operator.slice(0, -1)),
90-
serialize_get_binding(node.left, context.state),
91-
/** @type {Expression} */ (context.visit(node.right))
92+
/** @type {BinaryOperator} */ (operator.slice(0, -1)),
93+
serialize_get_binding(left, context.state),
94+
/** @type {Expression} */ (context.visit(right))
9295
);
9396

9497
return b.call('$.store_set', b.id(name), value);
@@ -97,12 +100,12 @@ function serialize_assignment(node, context, fallback) {
97100
return b.call(
98101
'$.mutate_store',
99102
b.assignment('??=', b.id('$$store_subs'), b.object([])),
100-
b.literal(left.name),
103+
b.literal(object.name),
101104
b.id(name),
102105
b.assignment(
103-
node.operator,
104-
/** @type {Pattern} */ (context.visit(node.left)),
105-
/** @type {Expression} */ (context.visit(node.right))
106+
operator,
107+
/** @type {Pattern} */ (context.visit(left)),
108+
/** @type {Expression} */ (context.visit(right))
106109
)
107110
);
108111
}

0 commit comments

Comments
 (0)