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
pull/16532/head
ComputerGuy 1 month ago committed by GitHub
parent a91e0154d4
commit f5950f866c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: correctly differentiate static fields before emitting `duplicate_class_field`

@ -57,7 +57,7 @@ export function ClassBody(node, context) {
e.state_field_duplicate(node, name); e.state_field_duplicate(node, name);
} }
const _key = (key.type === 'PrivateIdentifier' ? '#' : '') + name; const _key = (node.type === 'AssignmentExpression' || !node.static ? '' : '@') + name;
const field = fields.get(_key); const field = fields.get(_key);
// if there's already a method or assigned field, error // if there's already a method or assigned field, error
@ -78,7 +78,7 @@ export function ClassBody(node, context) {
for (const child of node.body) { for (const child of node.body) {
if (child.type === 'PropertyDefinition' && !child.computed && !child.static) { if (child.type === 'PropertyDefinition' && !child.computed && !child.static) {
handle(child, child.key, child.value); handle(child, child.key, child.value);
const key = (child.key.type === 'PrivateIdentifier' ? '#' : '') + get_name(child.key); const key = /** @type {string} */ (get_name(child.key));
const field = fields.get(key); const field = fields.get(key);
if (!field) { if (!field) {
fields.set(key, [child.value ? 'assigned_prop' : 'prop']); fields.set(key, [child.value ? 'assigned_prop' : 'prop']);
@ -91,7 +91,7 @@ export function ClassBody(node, context) {
if (child.kind === 'constructor') { if (child.kind === 'constructor') {
constructor = child; constructor = child;
} else if (!child.computed) { } else if (!child.computed) {
const key = (child.key.type === 'PrivateIdentifier' ? '#' : '') + get_name(child.key); const key = (child.static ? '@' : '') + get_name(child.key);
const field = fields.get(key); const field = fields.get(key);
if (!field) { if (!field) {
fields.set(key, [child.kind]); fields.set(key, [child.kind]);

@ -1,6 +1,6 @@
export class Counter { export class Counter {
count = -1; count = -1;
static count() {}
constructor() { constructor() {
this.count = $state(0); this.count = $state(0);
} }

Loading…
Cancel
Save