From a3fdd3d8b783221b04952b9fbeb747714890c941 Mon Sep 17 00:00:00 2001 From: ComputerGuy <63362464+Ocean-OS@users.noreply.github.com> Date: Wed, 30 Jul 2025 12:30:34 -0700 Subject: [PATCH 1/3] fix: correctly differentiate static fields before emitting `duplicate_class_field` --- .changeset/nine-cups-film.md | 5 +++++ .../compiler/phases/2-analyze/visitors/ClassBody.js | 10 ++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) create mode 100644 .changeset/nine-cups-film.md diff --git a/.changeset/nine-cups-film.md b/.changeset/nine-cups-film.md new file mode 100644 index 000000000000..ba72337daca7 --- /dev/null +++ b/.changeset/nine-cups-film.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: correctly differentiate static fields before emitting `duplicate_class_field` diff --git a/packages/svelte/src/compiler/phases/2-analyze/visitors/ClassBody.js b/packages/svelte/src/compiler/phases/2-analyze/visitors/ClassBody.js index 2bfc1dbce36c..ed68dae74e84 100644 --- a/packages/svelte/src/compiler/phases/2-analyze/visitors/ClassBody.js +++ b/packages/svelte/src/compiler/phases/2-analyze/visitors/ClassBody.js @@ -57,7 +57,10 @@ export function ClassBody(node, context) { e.state_field_duplicate(node, name); } - const _key = (key.type === 'PrivateIdentifier' ? '#' : '') + name; + const _key = + (node.type === 'AssignmentExpression' || !node.static ? '' : '@') + + (key.type === 'PrivateIdentifier' ? '#' : '') + + name; const field = fields.get(_key); // if there's already a method or assigned field, error @@ -91,7 +94,10 @@ export function ClassBody(node, context) { if (child.kind === 'constructor') { constructor = child; } else if (!child.computed) { - const key = (child.key.type === 'PrivateIdentifier' ? '#' : '') + get_name(child.key); + const key = + (child.static ? '@' : '') + + (child.key.type === 'PrivateIdentifier' ? '#' : '') + + get_name(child.key); const field = fields.get(key); if (!field) { fields.set(key, [child.kind]); From f56a3ec8af5cb24158f637ff2193c5affae72703 Mon Sep 17 00:00:00 2001 From: ComputerGuy <63362464+Ocean-OS@users.noreply.github.com> Date: Wed, 30 Jul 2025 17:24:02 -0700 Subject: [PATCH 2/3] remove unnecessary `#` concatenation --- .../compiler/phases/2-analyze/visitors/ClassBody.js | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/packages/svelte/src/compiler/phases/2-analyze/visitors/ClassBody.js b/packages/svelte/src/compiler/phases/2-analyze/visitors/ClassBody.js index ed68dae74e84..dd216371740c 100644 --- a/packages/svelte/src/compiler/phases/2-analyze/visitors/ClassBody.js +++ b/packages/svelte/src/compiler/phases/2-analyze/visitors/ClassBody.js @@ -57,10 +57,7 @@ export function ClassBody(node, context) { e.state_field_duplicate(node, name); } - const _key = - (node.type === 'AssignmentExpression' || !node.static ? '' : '@') + - (key.type === 'PrivateIdentifier' ? '#' : '') + - name; + const _key = (node.type === 'AssignmentExpression' || !node.static ? '' : '@') + name; const field = fields.get(_key); // if there's already a method or assigned field, error @@ -81,7 +78,7 @@ export function ClassBody(node, context) { for (const child of node.body) { if (child.type === 'PropertyDefinition' && !child.computed && !child.static) { 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); if (!field) { fields.set(key, [child.value ? 'assigned_prop' : 'prop']); @@ -94,10 +91,7 @@ export function ClassBody(node, context) { if (child.kind === 'constructor') { constructor = child; } else if (!child.computed) { - const key = - (child.static ? '@' : '') + - (child.key.type === 'PrivateIdentifier' ? '#' : '') + - get_name(child.key); + const key = (child.static ? '@' : '') + get_name(child.key); const field = fields.get(key); if (!field) { fields.set(key, [child.kind]); From 536ba7f6833bb9cd251e2feabaa97d3d3167aa72 Mon Sep 17 00:00:00 2001 From: ComputerGuy <63362464+Ocean-OS@users.noreply.github.com> Date: Wed, 30 Jul 2025 17:30:40 -0700 Subject: [PATCH 3/3] add test --- .../validator/samples/class-state-constructor-9/input.svelte.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/svelte/tests/validator/samples/class-state-constructor-9/input.svelte.js b/packages/svelte/tests/validator/samples/class-state-constructor-9/input.svelte.js index a8469e13af46..3806046f3f72 100644 --- a/packages/svelte/tests/validator/samples/class-state-constructor-9/input.svelte.js +++ b/packages/svelte/tests/validator/samples/class-state-constructor-9/input.svelte.js @@ -1,6 +1,6 @@ export class Counter { count = -1; - + static count() {} constructor() { this.count = $state(0); }