File tree Expand file tree Collapse file tree 4 files changed +46
-1
lines changed
src/compiler/phases/3-transform/server
tests/runtime-runes/samples/class-state-derived-2 Expand file tree Collapse file tree 4 files changed +46
-1
lines changed Original file line number Diff line number Diff line change
1
+ ---
2
+ " svelte " : patch
3
+ ---
4
+
5
+ fix: improve ssr code generation for class property $derived
Original file line number Diff line number Diff line change @@ -545,7 +545,7 @@ const javascript_visitors_runes = {
545
545
if ( node . value != null && node . value . type === 'CallExpression' ) {
546
546
const rune = get_rune ( node . value , state . scope ) ;
547
547
548
- if ( rune === '$state' || rune === '$state.frozen' || rune === '$derived' ) {
548
+ if ( rune === '$state' || rune === '$state.frozen' ) {
549
549
return {
550
550
...node ,
551
551
value :
@@ -554,6 +554,26 @@ const javascript_visitors_runes = {
554
554
: /** @type {import('estree').Expression } */ ( visit ( node . value . arguments [ 0 ] ) )
555
555
} ;
556
556
}
557
+ if ( rune === '$derived' ) {
558
+ return {
559
+ type : 'MethodDefinition' ,
560
+ kind : 'get' ,
561
+ key : node . key ,
562
+ computed : false ,
563
+ static : false ,
564
+ value : b . function (
565
+ null ,
566
+ [ ] ,
567
+ b . block ( [
568
+ b . return (
569
+ node . value . arguments . length === 0
570
+ ? null
571
+ : /** @type {import('estree').Expression } */ ( visit ( node . value . arguments [ 0 ] ) )
572
+ )
573
+ ] )
574
+ )
575
+ } ;
576
+ }
557
577
if ( rune === '$derived.by' ) {
558
578
return {
559
579
...node ,
Original file line number Diff line number Diff line change
1
+ import { test } from '../../test' ;
2
+
3
+ export default test ( {
4
+ html : `2`
5
+ } ) ;
Original file line number Diff line number Diff line change
1
+ <script >
2
+ export class Counter {
3
+ count = $state (0 );
4
+ doubled = $derived (this .count * 2 );
5
+
6
+ constructor (initialCount = 0 ) {
7
+ this .count = initialCount;
8
+ }
9
+ }
10
+
11
+ const counter = new Counter (1 );
12
+ </script >
13
+
14
+ {counter .doubled }
15
+
You can’t perform that action at this time.
0 commit comments