8000 allow writing to public deriveds on server · sveltejs/svelte@5ba3572 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5ba3572

Browse files
committed
allow writing to public deriveds on server
1 parent 846d780 commit 5ba3572

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

packages/svelte/src/compiler/phases/3-transform/server/visitors/ClassBody.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ export function ClassBody(node, context) {
3636

3737
body.push(
3838
b.prop_def(field.key, null),
39-
b.method('get', b.key(name), [], [b.return(b.call(member))])
39+
b.method('get', b.key(name), [], [b.return(b.call(member))]),
40+
b.method('set', b.key(name), [b.id('$$value')], [b.return(b.call(member, b.id('$$value')))])
4041
);
4142
}
4243
}
@@ -61,6 +62,7 @@ export function ClassBody(node, context) {
6162
if (name[0] === '#' || field.type === '$state' || field.type === '$state.raw') {
6263
body.push(/** @type {PropertyDefinition} */ (context.visit(definition, child_state)));
6364
} else if (field.node === definition) {
65+
// $derived / $derived.by
6466
const member = b.member(b.this, field.key);
6567

6668
body.push(
@@ -69,7 +71,8 @@ export function ClassBody(node, context) {
6971
/** @type {CallExpression} */ (context.visit(field.value, child_state))
7072
),
7173

72-
b.method('get', definition.key, [], [b.return(b.call(member))])
74+
b.method('get', definition.key, [], [b.return(b.call(member))]),
75+
b.method('set', b.key(name), [b.id('$$value')], [b.return(b.call(member, b.id('$$value')))])
7376
);
7477
}
7578
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { test } from '../../test';
2+
3+
export default test({
4+
html: `3 3 3 3`
5+
});
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<script>
2+
class X {
3+
x = $state(1);
4+
on_class = $derived(this.x * 2);
5+
#on_class_private = $derived(this.x * 2);
6+
#in_constructor_private
7+
8+
constructor() {
9+
this.#in_constructor_private = $derived(this.x * 2);
10+
this.in_constructor = $derived(this.x * 2);
11+
this.#on_class_private = 3;
12+
this.#in_constructor_private = 3;
13+
}
14+
15+
get on_class_private() {
16+
return this.#on_class_private;
17+
}
18+
19+
get in_constructor_private() {
20+
return this.#in_constructor_private;
21+
}
22+
}
23+
24+
const x = new X();
25+
x.on_class = 3;
26+
x.in_constructor = 3;
27+
</script>
28+
29+
{x.on_class} {x.in_constructor} {x.on_class_private} {x.in_constructor_private}

0 commit comments

Comments
 (0)
0