fix: correctly tag private class state fields (#16132)

pull/16133/head
Rich Harris 3 months ago committed by GitHub
parent e8a7c426d4
commit f5a020d56b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: correctly tag private class state fields

@ -78,7 +78,7 @@ export function ClassBody(node, context) {
? /** @type {CallExpression} */ (context.visit(definition.value, child_state))
: undefined;
if (dev) {
if (dev && field.node === definition) {
value = b.call('$.tag', value, b.literal(`${declaration.id?.name ?? '[class]'}.${name}`));
}

@ -0,0 +1,31 @@
import { flushSync } from 'svelte';
import { test } from '../../test';
import { normalise_trace_logs } from '../../../helpers.js';
export default test({
compileOptions: {
dev: true
},
test({ assert, target, logs }) {
assert.deepEqual(normalise_trace_logs(logs), [
{ log: 'effect' },
{ log: '$state', highlighted: true },
{ log: 'Counter.#count', highlighted: false },
{ log: 0 }
]);
logs.length = 0;
const button = target.querySelector('button');
button?.click();
flushSync();
assert.deepEqual(normalise_trace_logs(logs), [
{ log: 'effect' },
{ log: '$state', highlighted: true },
{ log: 'Counter.#count', highlighted: false },
{ log: 1 }
]);
}
});

@ -0,0 +1,28 @@
<script>
class Counter {
#count;
constructor() {
this.#count = $state(0);
}
get count() {
return this.#count;
}
increment = () => {
this.#count += 1;
}
}
const counter = new Counter();
$effect(() => {
$inspect.trace('effect');
counter.count;
});
</script>
<button onclick={counter.increment}>
clicks: {counter.count}
</button>
Loading…
Cancel
Save