From c82ede5cf6ea339ce822401bef044848b2e806ec Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Mon, 19 May 2025 09:26:57 -0400 Subject: [PATCH] adjust message to accommodate more cases --- .vscode/launch.json | 1 + .../98-reference/.generated/compile-errors.md | 57 +++++++++---------- .../svelte/messages/compile-errors/script.md | 53 ++++++++--------- packages/svelte/src/compiler/errors.js | 7 ++- .../phases/2-analyze/visitors/ClassBody.js | 2 +- .../class-state-constructor-1/errors.json | 4 +- .../class-state-constructor-2/errors.json | 4 +- .../class-state-constructor-3/errors.json | 4 +- .../class-state-constructor-5/errors.json | 4 +- .../class-state-constructor-6/errors.json | 4 +- .../class-state-constructor-8/errors.json | 4 +- .../class-state-constructor-9/errors.json | 4 +- 12 files changed, 70 insertions(+), 78 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 142965ada2..6d24f97c2a 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -6,6 +6,7 @@ "request": "launch", "name": "Run sandbox", "program": "${workspaceFolder}/playgrounds/sandbox/run.js", + "runtimeExecutable": "node", "env": { "NODE_OPTIONS": "--stack-trace-limit=10000" } diff --git a/documentation/docs/98-reference/.generated/compile-errors.md b/documentation/docs/98-reference/.generated/compile-errors.md index 2811861680..8fdbe87fb0 100644 --- a/documentation/docs/98-reference/.generated/compile-errors.md +++ b/documentation/docs/98-reference/.generated/compile-errors.md @@ -208,37 +208,6 @@ Cannot assign to %thing% Cannot bind to %thing% ``` -### constructor_state_reassignment - -``` -A state field declaration in a constructor must be the first assignment, and the only one that uses a rune -``` - -[State fields]($state#Classes) can be declared as normal class fields or inside the constructor, in which case the declaration must be the _first_ assignment. -Assignments thereafter must not use the rune. - -```ts -constructor() { - this.count = $state(0); - this.count = $state(1); // invalid, assigning to the same property with `$state` again -} - -constructor() { - this.count = $state(0); - this.count = $state.raw(1); // invalid, assigning to the same property with a different rune -} - -constructor() { - this.count = 0; - this.count = $state(1); // invalid, this property was created as a regular property, not state -} - -constructor() { - this.count = $state(0); - this.count = 1; // valid, this is setting the state that has already been declared -} -``` - ### css_empty_declaration ``` @@ -877,6 +846,32 @@ Cannot reassign or bind to snippet parameter This snippet is shadowing the prop `%prop%` with the same name ``` +### state_field_duplicate + +``` +`%name%` has already been declared on this class +``` + +An assignment to a class field that uses a `$state` or `$derived` rune is considered a _state field declaration_. The declaration can happen in the class body... + +```js +class Counter { + count = $state(0); +} +``` + +...or inside the constructor... + +```js +class Counter { + constructor() { + this.count = $state(0); + } +} +``` + +...but it can only happen once. + ### state_invalid_export ``` diff --git a/packages/svelte/messages/compile-errors/script.md b/packages/svelte/messages/compile-errors/script.md index 7831be3a42..cfd6a2ae00 100644 --- a/packages/svelte/messages/compile-errors/script.md +++ b/packages/svelte/messages/compile-errors/script.md @@ -10,35 +10,6 @@ > Cannot bind to %thing% -## constructor_state_reassignment - -> A state field declaration in a constructor must be the first assignment, and the only one that uses a rune - -[State fields]($state#Classes) can be declared as normal class fields or inside the constructor, in which case the declaration must be the _first_ assignment. -Assignments thereafter must not use the rune. - -```ts -constructor() { - this.count = $state(0); - this.count = $state(1); // invalid, assigning to the same property with `$state` again -} - -constructor() { - this.count = $state(0); - this.count = $state.raw(1); // invalid, assigning to the same property with a different rune -} - -constructor() { - this.count = 0; - this.count = $state(1); // invalid, this property was created as a regular property, not state -} - -constructor() { - this.count = $state(0); - this.count = 1; // valid, this is setting the state that has already been declared -} -``` - ## declaration_duplicate > `%name%` has already been declared @@ -241,6 +212,30 @@ It's possible to export a snippet from a `