Skip to content

Commit f5950f8

Browse files
authored
fix: correctly differentiate static fields before emitting duplicate_class_field (#16526)
* fix: correctly differentiate static fields before emitting `duplicate_class_field` * remove unnecessary `#` concatenation * add test
1 parent a91e015 commit f5950f8

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

.changeset/nine-cups-film.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 differentiate static fields before emitting `duplicate_class_field`

packages/svelte/src/compiler/phases/2-analyze/visitors/ClassBody.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export function ClassBody(node, context) {
5757
e.state_field_duplicate(node, name);
5858
}
5959

60-
const _key = (key.type === 'PrivateIdentifier' ? '#' : '') + name;
60+
const _key = (node.type === 'AssignmentExpression' || !node.static ? '' : '@') + name;
6161
const field = fields.get(_key);
6262

6363
// if there's already a method or assigned field, error
@@ -78,7 +78,7 @@ export function ClassBody(node, context) {
7878
for (const child of node.body) {
7979
if (child.type === 'PropertyDefinition' && !child.computed && !child.static) {
8080
handle(child, child.key, child.value);
81-
const key = (child.key.type === 'PrivateIdentifier' ? '#' : '') + get_name(child.key);
81+
const key = /** @type {string} */ (get_name(child.key));
8282
const field = fields.get(key);
8383
if (!field) {
8484
fields.set(key, [child.value ? 'assigned_prop' : 'prop']);
@@ -91,7 +91,7 @@ export function ClassBody(node, context) {
9191
if (child.kind === 'constructor') {
9292
constructor = child;
9393
} else if (!child.computed) {
94-
const key = (child.key.type === 'PrivateIdentifier' ? '#' : '') + get_name(child.key);
94+
const key = (child.static ? '@' : '') + get_name(child.key);
9595
const field = fields.get(key);
9696
if (!field) {
9797
fields.set(key, [child.kind]);

packages/svelte/tests/validator/samples/class-state-constructor-9/input.svelte.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export class Counter {
22
count = -1;
3-
3+
static count() {}
44
constructor() {
55
this.count = $state(0);
66
}

0 commit comments

Comments
 (0)