fix: increment private state fields through a non-`this` receiver (#18622)

svelte0/svelte-15053-fix-stop-currenttime-binding-updates-during-outros
Yuichiro Yamashita 4 weeks ago committed by GitHub
parent 20b341f100
commit 135f4439c3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: increment private state fields through a non-`this` receiver

@ -13,7 +13,6 @@ export function UpdateExpression(node, context) {
if (
argument.type === 'MemberExpression' &&
argument.object.type === 'ThisExpression' &&
argument.property.type === 'PrivateIdentifier' &&
context.state.state_fields.has('#' + argument.property.name)
) {

@ -0,0 +1,22 @@
import { flushSync } from 'svelte';
import { test } from '../../test';
export default test({
html: `<button>1</button><button>drop</button>`,
test({ assert, target }) {
const [bump, drop] = target.querySelectorAll('button');
bump?.click();
flushSync();
assert.htmlEqual(target.innerHTML, `<button>2</button><button>drop</button>`);
bump?.click();
flushSync();
assert.htmlEqual(target.innerHTML, `<button>3</button><button>drop</button>`);
drop?.click();
flushSync();
assert.htmlEqual(target.innerHTML, `<button>2</button><button>drop</button>`);
}
});

@ -0,0 +1,23 @@
<script>
class Counter {
#count = $state(1);
bump(other) {
other.#count++;
}
drop(other) {
--other.#count;
}
getCount() {
return this.#count;
}
}
const a = new Counter();
const b = new Counter();
</script>
<button onclick={() => a.bump(b)}>{b.getCount()}</button>
<button onclick={() => a.drop(b)}>drop</button>
Loading…
Cancel
Save