fix: improve ssr code generation for class property $derived (#10661)

* fix: improve ssr code generation for class property $derived

* fix ts
pull/10664/head
Dominic Gannaway 10 months ago committed by GitHub
parent 99e1665ce1
commit b2e9be2148
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
"svelte": patch
---
fix: improve ssr code generation for class property $derived

@ -545,7 +545,7 @@ const javascript_visitors_runes = {
if (node.value != null && node.value.type === 'CallExpression') {
const rune = get_rune(node.value, state.scope);
if (rune === '$state' || rune === '$state.frozen' || rune === '$derived') {
if (rune === '$state' || rune === '$state.frozen') {
return {
...node,
value:
@ -554,6 +554,26 @@ const javascript_visitors_runes = {
: /** @type {import('estree').Expression} */ (visit(node.value.arguments[0]))
};
}
if (rune === '$derived') {
return {
type: 'MethodDefinition',
kind: 'get',
key: node.key,
computed: false,
static: false,
value: b.function(
null,
[],
b.block([
b.return(
node.value.arguments.length === 0
? null
: /** @type {import('estree').Expression} */ (visit(node.value.arguments[0]))
)
])
)
};
}
if (rune === '$derived.by') {
return {
...node,

@ -0,0 +1,5 @@
import { test } from '../../test';
export default test({
html: `2`
});

@ -0,0 +1,15 @@
<script>
export class Counter {
count = $state(0);
doubled = $derived(this.count * 2);
constructor(initialCount = 0) {
this.count = initialCount;
}
}
const counter = new Counter(1);
</script>
{counter.doubled}
Loading…
Cancel
Save