diff --git a/documentation/docs/98-reference/.generated/compile-errors.md b/documentation/docs/98-reference/.generated/compile-errors.md index fb0591b884..2811861680 100644 --- a/documentation/docs/98-reference/.generated/compile-errors.md +++ b/documentation/docs/98-reference/.generated/compile-errors.md @@ -211,10 +211,10 @@ Cannot bind to %thing% ### constructor_state_reassignment ``` -Cannot redeclare stateful field `%name%` in the constructor. The field was originally declared here: `%original_location%` +A state field declaration in a constructor must be the first assignment, and the only one that uses a rune ``` -To create stateful class fields in the constructor, the rune assignment must be the _first_ assignment to the class field. +[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 diff --git a/packages/svelte/messages/compile-errors/script.md b/packages/svelte/messages/compile-errors/script.md index 780c9f4d28..7831be3a42 100644 --- a/packages/svelte/messages/compile-errors/script.md +++ b/packages/svelte/messages/compile-errors/script.md @@ -12,9 +12,9 @@ ## constructor_state_reassignment -> Cannot redeclare stateful field `%name%` in the constructor. The field was originally declared here: `%original_location%` +> A state field declaration in a constructor must be the first assignment, and the only one that uses a rune -To create stateful class fields in the constructor, the rune assignment must be the _first_ assignment to the class field. +[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 diff --git a/packages/svelte/src/compiler/errors.js b/packages/svelte/src/compiler/errors.js index fc814a16d3..2913100f16 100644 --- a/packages/svelte/src/compiler/errors.js +++ b/packages/svelte/src/compiler/errors.js @@ -105,14 +105,12 @@ export function constant_binding(node, thing) { } /** - * Cannot redeclare stateful field `%name%` in the constructor. The field was originally declared here: `%original_location%` + * A state field declaration in a constructor must be the first assignment, and the only one that uses a rune * @param {null | number | NodeLike} node - * @param {string} name - * @param {string} original_location * @returns {never} */ -export function constructor_state_reassignment(node, name, original_location) { - e(node, 'constructor_state_reassignment', `Cannot redeclare stateful field \`${name}\` in the constructor. The field was originally declared here: \`${original_location}\`\nhttps://svelte.dev/e/constructor_state_reassignment`); +export function constructor_state_reassignment(node) { + e(node, 'constructor_state_reassignment', `A state field declaration in a constructor must be the first assignment, and the only one that uses a rune\nhttps://svelte.dev/e/constructor_state_reassignment`); } /** diff --git a/packages/svelte/tests/validator/samples/class-state-constructor-1/errors.json b/packages/svelte/tests/validator/samples/class-state-constructor-1/errors.json index eaff12b896..3eaacfe850 100644 --- a/packages/svelte/tests/validator/samples/class-state-constructor-1/errors.json +++ b/packages/svelte/tests/validator/samples/class-state-constructor-1/errors.json @@ -1,7 +1,7 @@ [ { "code": "constructor_state_reassignment", - "message": "Cannot redeclare stateful field `count` in the constructor. The field was originally declared here: `(unknown):2:1`", + "message": "A state field declaration in a constructor must be the first assignment, and the only one that uses a rune", "start": { "line": 5, "column": 2 diff --git a/packages/svelte/tests/validator/samples/class-state-constructor-2/errors.json b/packages/svelte/tests/validator/samples/class-state-constructor-2/errors.json index a27d7411d1..3eaacfe850 100644 --- a/packages/svelte/tests/validator/samples/class-state-constructor-2/errors.json +++ b/packages/svelte/tests/validator/samples/class-state-constructor-2/errors.json @@ -1,7 +1,7 @@ [ { "code": "constructor_state_reassignment", - "message": "Cannot redeclare stateful field `count` in the constructor. The field was originally declared here: `(unknown):3:2`", + "message": "A state field declaration in a constructor must be the first assignment, and the only one that uses a rune", "start": { "line": 5, "column": 2 diff --git a/packages/svelte/tests/validator/samples/class-state-constructor-3/errors.json b/packages/svelte/tests/validator/samples/class-state-constructor-3/errors.json index 8017794a67..a0f12ba4a6 100644 --- a/packages/svelte/tests/validator/samples/class-state-constructor-3/errors.json +++ b/packages/svelte/tests/validator/samples/class-state-constructor-3/errors.json @@ -1,7 +1,7 @@ [ { "code": "constructor_state_reassignment", - "message": "Cannot redeclare stateful field `count` in the constructor. The field was originally declared here: `(unknown):3:2`", + "message": "A state field declaration in a constructor must be the first assignment, and the only one that uses a rune", "start": { "line": 5, "column": 2 diff --git a/packages/svelte/tests/validator/samples/class-state-constructor-5/errors.json b/packages/svelte/tests/validator/samples/class-state-constructor-5/errors.json index 7942b016cb..82209f6a8c 100644 --- a/packages/svelte/tests/validator/samples/class-state-constructor-5/errors.json +++ b/packages/svelte/tests/validator/samples/class-state-constructor-5/errors.json @@ -1,7 +1,7 @@ [ { "code": "constructor_state_reassignment", - "message": "Cannot redeclare stateful field `count` in the constructor. The field was originally declared here: `(unknown):3:1`", + "message": "A state field declaration in a constructor must be the first assignment, and the only one that uses a rune", "start": { "line": 5, "column": 2 diff --git a/packages/svelte/tests/validator/samples/class-state-constructor-6/errors.json b/packages/svelte/tests/validator/samples/class-state-constructor-6/errors.json index 359274d156..7dd90d4837 100644 --- a/packages/svelte/tests/validator/samples/class-state-constructor-6/errors.json +++ b/packages/svelte/tests/validator/samples/class-state-constructor-6/errors.json @@ -1,7 +1,7 @@ [ { "code": "constructor_state_reassignment", - "message": "Cannot redeclare stateful field `count` in the constructor. The field was originally declared here: `(unknown):2:1`", + "message": "A state field declaration in a constructor must be the first assignment, and the only one that uses a rune", "start": { "line": 4, "column": 2 diff --git a/packages/svelte/tests/validator/samples/class-state-constructor-8/errors.json b/packages/svelte/tests/validator/samples/class-state-constructor-8/errors.json new file mode 100644 index 0000000000..ae0caee616 --- /dev/null +++ b/packages/svelte/tests/validator/samples/class-state-constructor-8/errors.json @@ -0,0 +1,14 @@ +[ + { + "code": "constructor_state_reassignment", + "message": "A state field declaration in a constructor must be the first assignment, and the only one that uses a rune", + "start": { + "line": 4, + "column": 2 + }, + "end": { + "line": 4, + "column": 24 + } + } +] diff --git a/packages/svelte/tests/validator/samples/class-state-constructor-8/input.svelte.js b/packages/svelte/tests/validator/samples/class-state-constructor-8/input.svelte.js new file mode 100644 index 0000000000..0a76c6fec9 --- /dev/null +++ b/packages/svelte/tests/validator/samples/class-state-constructor-8/input.svelte.js @@ -0,0 +1,6 @@ +export class Counter { + constructor() { + this.count = -1; + this.count = $state(0); + } +}