You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: State declarations in class constructors (#15820)
* feat: State declarations in class constructors
* feat: Analysis phase
* misc
* feat: client
* improvements
* feat: It is now at least backwards compatible. though the new stuff may still be wrong
* feat: It works I think?
* final cleanup??
* tests
* test for better types
* changeset
* rename functions (the function doesn't test call-expression-ness)
* small readability tweak
* failing test
* fix
* disallow computed state fields
* tweak message to better accommodate the case in which state is declared after a regular property
* failing test
* wildly confusing to have so many things called 'class analysis' - rename the transform-phrase ones to transformers
* missed a spot
* and another
* store analysis for use during transformation
* move code to where it is used
* do the analysis upfront, it's way simpler
* skip failing test for now
* simplify
* get rid of the class
* on second thoughts
* reduce indirection
* make analysis available at transform time
* WIP
* WIP
* WIP
* fix
* remove unused stuff
* revert snapshot tests
* unused
* note to self
* fix
* unused
* unused
* remove some unused stuff
* unused
* lint, tidy up
* reuse helper
* tweak
* simplify/DRY
* unused
* tweak
* unused
* more
* tweak
* tweak
* fix proxying logic
* tweak
* tweak
* adjust message to accommodate more cases
* unskip and fix test
* fix
* move
* revert unneeded drive-by change
* fix
* fix
* update docs
---------
Co-authored-by: Rich Harris <[email protected]>
Copy file name to clipboardExpand all lines: documentation/docs/98-reference/.generated/compile-errors.md
+33-1Lines changed: 33 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -846,6 +846,38 @@ Cannot reassign or bind to snippet parameter
846
846
This snippet is shadowing the prop `%prop%` with the same name
847
847
```
848
848
849
+
### state_field_duplicate
850
+
851
+
```
852
+
`%name%` has already been declared on this class
853
+
```
854
+
855
+
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...
856
+
857
+
```js
858
+
classCounter {
859
+
count =$state(0);
860
+
}
861
+
```
862
+
863
+
...or inside the constructor...
864
+
865
+
```js
866
+
classCounter {
867
+
constructor() {
868
+
this.count=$state(0);
869
+
}
870
+
}
871
+
```
872
+
873
+
...but it can only happen once.
874
+
875
+
### state_field_invalid_assignment
876
+
877
+
```
878
+
Cannot assign to a state field before its declaration
879
+
```
880
+
849
881
### state_invalid_export
850
882
851
883
```
@@ -855,7 +887,7 @@ Cannot export state from a module if it is reassigned. Either export a function
855
887
### state_invalid_placement
856
888
857
889
```
858
-
`%rune%(...)` can only be used as a variable declaration initializeror a class field
890
+
`%rune%(...)` can only be used as a variable declaration initializer, a class field declaration, or the first assignment to a class field at the top level of the constructor.
Copy file name to clipboardExpand all lines: packages/svelte/messages/compile-errors/script.md
+29-1Lines changed: 29 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -212,13 +212,41 @@ It's possible to export a snippet from a `<script module>` block, but only if it
212
212
213
213
> Cannot reassign or bind to snippet parameter
214
214
215
+
## state_field_duplicate
216
+
217
+
> `%name%` has already been declared on this class
218
+
219
+
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...
220
+
221
+
```js
222
+
classCounter {
223
+
count =$state(0);
224
+
}
225
+
```
226
+
227
+
...or inside the constructor...
228
+
229
+
```js
230
+
classCounter {
231
+
constructor() {
232
+
this.count=$state(0);
233
+
}
234
+
}
235
+
```
236
+
237
+
...but it can only happen once.
238
+
239
+
## state_field_invalid_assignment
240
+
241
+
> Cannot assign to a state field before its declaration
242
+
215
243
## state_invalid_export
216
244
217
245
> Cannot export state from a module if it is reassigned. Either export a function returning the state value or only mutate the state value's properties
218
246
219
247
## state_invalid_placement
220
248
221
-
> `%rune%(...)` can only be used as a variable declaration initializeror a class field
249
+
> `%rune%(...)` can only be used as a variable declaration initializer, a class field declaration, or the first assignment to a class field at the top level of the constructor.
e(node,'state_field_invalid_assignment',`Cannot assign to a state field before its declaration\nhttps://svelte.dev/e/state_field_invalid_assignment`);
481
+
}
482
+
464
483
/**
465
484
* Cannot export state from a module if it is reassigned. Either export a function returning the state value or only mutate the state value's properties
466
485
* @param {null | number | NodeLike} node
@@ -471,13 +490,13 @@ export function state_invalid_export(node) {
471
490
}
472
491
473
492
/**
474
-
* `%rune%(...)` can only be used as a variable declaration initializeror a class field
493
+
* `%rune%(...)` can only be used as a variable declaration initializer, a class field declaration, or the first assignment to a class field at the top level of the constructor.
475
494
* @param {null | number | NodeLike} node
476
495
* @param {string} rune
477
496
* @returns {never}
478
497
*/
479
498
exportfunctionstate_invalid_placement(node,rune){
480
-
e(node,'state_invalid_placement',`\`${rune}(...)\` can only be used as a variable declaration initializeror a class field\nhttps://svelte.dev/e/state_invalid_placement`);
499
+
e(node,'state_invalid_placement',`\`${rune}(...)\` can only be used as a variable declaration initializer, a class field declaration, or the first assignment to a class field at the top level of the constructor.\nhttps://svelte.dev/e/state_invalid_placement`);
0 commit comments