Skip to content

Commit 9883af4

Browse files
committed
fix: leave update expressions untransformed unless a transformer is provided
1 parent aaea254 commit 9883af4

File tree

4 files changed

+40
-17
lines changed

4 files changed

+40
-17
lines changed

.changeset/neat-news-dance.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: leave update expressions untransformed unless a transformer is provided

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

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ export function UpdateExpression(node, context) {
3434
}
3535

3636
const left = object(argument);
37-
if (left === null) return context.next();
3837

3938
if (left === argument) {
4039
const transform = context.state.transform;
@@ -45,25 +44,32 @@ export function UpdateExpression(node, context) {
4544
}
4645
}
4746

48-
const assignment = /** @type {Expression} */ (
49-
context.visit(
50-
b.assignment(
51-
node.operator === '++' ? '+=' : '-=',
52-
/** @type {Pattern} */ (argument),
53-
b.literal(1)
47+
/** @type {Expression} */
48+
let result;
49+
50+
if (left === null || !context.state.transform[left.name]) {
51+
result = /** @type {Expression} */ (context.next());
52+
} else {
53+
const assignment = /** @type {Expression} */ (
54+
context.visit(
55+
b.assignment(
56+
node.operator === '++' ? '+=' : '-=',
57+
/** @type {Pattern} */ (argument),
58+
b.literal(1)
59+
)
5460
)
55-
)
56-
);
61+
);
5762

58-
const parent = /** @type {Node} */ (context.path.at(-1));
59-
const is_standalone = parent.type === 'ExpressionStatement'; // TODO and possibly others, but not e.g. the `test` of a WhileStatement
63+
const parent = /** @type {Node} */ (context.path.at(-1));
64+
const is_standalone = parent.type === 'ExpressionStatement'; // TODO and possibly others, but not e.g. the `test` of a WhileStatement
6065

61-
const update =
62-
node.prefix || is_standalone
63-
? assignment
64-
: b.binary(node.operator === '++' ? '-' : '+', assignment, b.literal(1));
66+
result =
67+
node.prefix || is_standalone
68+
? assignment
69+
: b.binary(node.operator === '++' ? '-' : '+', assignment, b.literal(1));
70+
}
6571

6672
return is_ignored(node, 'ownership_invalid_mutation')
67-
? b.call('$.skip_ownership_validation', b.thunk(update))
68-
: update;
73+
? b.call('$.skip_ownership_validation', b.thunk(result))
74+
: result;
6975
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { test } from '../../test';
2+
3+
export default test({
4+
test({ assert, logs }) {
5+
assert.deepEqual(logs, [0n, 1n, 2n, 3n, 4n, 5n]);
6+
}
7+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
for (let i = 0n; i <= 5n; i++) {
3+
console.log(i);
4+
}
5+
</script>

0 commit comments

Comments
 (0)