Skip to content

Commit 3bb1af9

Browse files
7nik7nik
and
7nik
authored
fix: proxify the value in assignment shorthands to the private field (#15862)
Co-authored-by: 7nik <[email protected]>
1 parent 326b329 commit 3bb1af9

File tree

4 files changed

+56
-1
lines changed

4 files changed

+56
-1
lines changed

.changeset/shaggy-toys-arrive.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: proxify the value in assignment shorthands to the private field

packages/svelte/src/compiler/utils/ast.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -580,5 +580,7 @@ export function build_assignment_value(operator, left, right) {
580580
return operator === '='
581581
? right
582582
: // turn something like x += 1 into x = x + 1
583-
b.binary(/** @type {ESTree.BinaryOperator} */ (operator.slice(0, -1)), left, right);
583+
['||=', '&&=', '??='].includes(operator)
584+
? b.logical(/** @type {ESTree.LogicalOperator} */ (operator.slice(0, -1)), left, right)
585+
: b.binary(/** @type {ESTree.BinaryOperator} */ (operator.slice(0, -1)), left, right);
584586
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { flushSync } from 'svelte';
2+
import { test } from '../../test';
3+
4+
export default test({
5+
async test({ assert, target }) {
6+
const btn = target.querySelector('button');
7+
8+
btn?.click();
9+
flushSync();
10+
assert.htmlEqual(
11+
target.innerHTML,
12+
`
13+
<button>inc</button>
14+
<p>a:1</p>
15+
<p>b:2</p>
16+
<p>c:3</p>
17+
`
18+
);
19+
}
20+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<script>
2+
class Counter {
3+
#a = $state();
4+
#b = $state({ val: -1 });
5+
#c = $state();
6+
constructor() {
7+
this.#a ||= {val: 0}
8+
this.#b &&= {val: 0}
9+
this.#c ??= {val: 0}
10+
}
11+
inc() {
12+
this.#a.val += 1;
13+
this.#b.val += 2;
14+
this.#c.val += 3;
15+
}
16+
get a() { return this.#a?.val; }
17+
get b() { return this.#b?.val; }
18+
get c() { return this.#c?.val; }
19+
}
20+
21+
let counter = new Counter();
22+
</script>
23+
24+
<button onclick={() => counter.inc()}>inc</button>
25+
<!-- prevent updating outputs in a common effect -->
26+
{#key 1}<p>a:{counter.a}</p>{/key}
27+
{#key 2}<p>b:{counter.b}</p>{/key}
28+
{#key 3}<p>c:{counter.c}</p>{/key}

0 commit comments

Comments
 (0)