Skip to content

Commit f5a020d

Browse files
authored
fix: correctly tag private class state fields (#16132)
1 parent e8a7c42 commit f5a020d

File tree

4 files changed

+65
-1
lines changed

4 files changed

+65
-1
lines changed

.changeset/three-steaks-wash.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: correctly tag private class state fields

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export function ClassBody(node, context) {
7878
? /** @type {CallExpression} */ (context.visit(definition.value, child_state))
7979
: undefined;
8080

81-
if (dev) {
81+
if (dev && field.node === definition) {
8282
value = b.call('$.tag', value, b.literal(`${declaration.id?.name ?? '[class]'}.${name}`));
8383
}
8484

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { flushSync } from 'svelte';
2+
import { test } from '../../test';
3+
import { normalise_trace_logs } from '../../../helpers.js';
4+
5+
export default test({
6+
compileOptions: {
7+
dev: true
8+
},
9+
10+
test({ assert, target, logs }) {
11+
assert.deepEqual(normalise_trace_logs(logs), [
12+
{ log: 'effect' },
13+
{ log: '$state', highlighted: true },
14+
{ log: 'Counter.#count', highlighted: false },
15+
{ log: 0 }
16+
]);
17+
18+
logs.length = 0;
19+
20+
const button = target.querySelector('button');
21+
button?.click();
22+
flushSync();
23+
24+
assert.deepEqual(normalise_trace_logs(logs), [
25+
{ log: 'effect' },
26+
{ log: '$state', highlighted: true },
27+
{ log: 'Counter.#count', highlighted: false },
28+
{ log: 1 }
29+
]);
30+
}
31+
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<script>
2+
class Counter {
3+
#count;
4+
5+
constructor() {
6+
this.#count = $state(0);
7+
}
8+
9+
get count() {
10+
return this.#count;
11+
}
12+
13+
increment = () => {
14+
this.#count += 1;
15+
}
16+
}
17+
18+
const counter = new Counter();
19+
20+
$effect(() => {
21+
$inspect.trace('effect');
22+
counter.count;
23+
});
24+
</script>
25+
26+
<button onclick={counter.increment}>
27+
clicks: {counter.count}
28+
</button>

0 commit comments

Comments
 (0)