Skip to content

Commit 84ed961

Browse files
committed
tidy up
1 parent 3a90f07 commit 84ed961

File tree

1 file changed

+21
-22
lines changed

1 file changed

+21
-22
lines changed

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

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,18 @@ export function AssignmentExpression(node, context) {
2222
const should_cache = value.type !== 'Identifier';
2323
const rhs = should_cache ? b.id('$$value') : value;
2424

25-
let unchanged = 0;
25+
let changed = false;
2626

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

31-
return serialize_assignment('=', path.node, value, context, () => {
32-
unchanged += 1;
33-
return assignment;
34-
});
30+
let assignment = serialize_assignment('=', path.node, value, context);
31+
if (assignment !== null) changed = true;
32+
33+
return assignment ?? b.assignment('=', path.node, value);
3534
});
3635

37-
if (unchanged === assignments.length) {
36+
if (!changed) {
3837
// No change to output -> nothing to transform -> we can keep the original assignment
3938
return context.next();
4039
}
@@ -54,18 +53,17 @@ export function AssignmentExpression(node, context) {
5453
return sequence;
5554
}
5655

57-
return serialize_assignment(node.operator, node.left, node.right, context, context.next);
56+
return serialize_assignment(node.operator, node.left, node.right, context) || context.next();
5857
}
5958

6059
/**
6160
* @param {AssignmentOperator} operator
6261
* @param {Pattern} left
6362
* @param {Expression} right
6463
* @param {import('zimmerframe').Context<SvelteNode, ServerTransformState>} context
65-
* @param {() => any} fallback
66-
* @returns {Expression}
64+
* @returns {Expression | null}
6765
*/
68-
function serialize_assignment(operator, left, right, context, fallback) {
66+
function serialize_assignment(operator, left, right, context) {
6967
let object = left;
7068

7169
while (object.type === 'MemberExpression') {
@@ -74,25 +72,26 @@ function serialize_assignment(operator, left, right, context, fallback) {
7472
}
7573

7674
if (object.type !== 'Identifier' || !is_store_name(object.name)) {
77-
return fallback();
75+
return null;
7876
}
7977

8078
const name = object.name.slice(1);
8179

8280
if (!context.state.scope.get(name)) {
83-
return fallback();
81+
return null;
8482
}
8583

8684
if (object === left) {
87-
const value =
88-
operator === '='
89-
? /** @type {Expression} */ (context.visit(right))
90-
: // turn something like x += 1 into x = x + 1
91-
b.binary(
92-
/** @type {BinaryOperator} */ (operator.slice(0, -1)),
93-
serialize_get_binding(left, context.state),
94-
/** @type {Expression} */ (context.visit(right))
95-
);
85+
let value = /** @type {Expression} */ (context.visit(right));
86+
87+
if (operator !== '=') {
88+
// turn `x += 1` into `x = x + 1`
89+
value = b.binary(
90+
/** @type {BinaryOperator} */ (operator.slice(0, -1)),
91+
serialize_get_binding(left, context.state),
92+
value
93+
);
94+
}
9695

9796
return b.call('$.store_set', b.id(name), value);
9897
}

0 commit comments

Comments
 (0)