Skip to content

Commit b5fcd11

Browse files
authored
fix: correctly transform reassignments to class fields in SSR mode (#16051)
* fix: correctly transform reassignments to class fields in SSR mode * add test, fix more stuff * fix
1 parent e5d0cd2 commit b5fcd11

File tree

5 files changed

+65
-6
lines changed

5 files changed

+65
-6
lines changed

.changeset/rude-drinks-relate.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: correctly transform reassignments to class fields in SSR mode

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@ export function AssignmentExpression(node, context) {
2424
* @returns {Expression | null}
2525
*/
2626
function build_assignment(operator, left, right, context) {
27-
if (context.state.analysis.runes && left.type === 'MemberExpression') {
27+
if (
28+
context.state.analysis.runes &&
29+
left.type === 'MemberExpression' &&
30+
left.object.type === 'ThisExpression' &&
31+
!left.computed
32+
) {
2833
const name = get_name(left.property);
2934
const field = name && context.state.state_fields.get(name);
3035

@@ -44,7 +49,11 @@ function build_assignment(operator, left, right, context) {
4449
/** @type {Expression} */ (context.visit(right))
4550
);
4651
}
47-
} else if (field && (field.type === '$derived' || field.type === '$derived.by')) {
52+
} else if (
53+
field &&
54+
(field.type === '$derived' || field.type === '$derived.by') &&
55+
left.property.type === 'PrivateIdentifier'
56+
) {
4857
let value = /** @type {Expression} */ (
4958
context.visit(build_assignment_value(operator, left, right))
5059
);

packages/svelte/tests/snapshot/samples/class-state-field-constructor-assignment/_expected/client/index.svelte.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default function Class_state_field_constructor_assignment($$anchor, $$pro
55
$.push($$props, true);
66

77
class Foo {
8-
#a = $.state();
8+
#a = $.state(0);
99

1010
get a() {
1111
return $.get(this.#a);
@@ -16,10 +16,31 @@ export default function Class_state_field_constructor_assignment($$anchor, $$pro
1616
}
1717

1818
#b = $.state();
19+
#foo = $.derived(() => ({ bar: this.a * 2 }));
20+
21+
get foo() {
22+
return $.get(this.#foo);
23+
}
24+
25+
set foo(value) {
26+
$.set(this.#foo, value);
27+
}
28+
29+
#bar = $.derived(() => ({ baz: this.foo }));
30+
31+
get bar() {
32+
return $.get(this.#bar);
33+
}
34+
35+
set bar(value) {
36+
$.set(this.#bar, value);
37+
}
1938

2039
constructor() {
2140
this.a = 1;
2241
$.set(this.#b, 2);
42+
this.foo.bar = 3;
43+
this.bar = 4;
2344
}
2445
}
2546

packages/svelte/tests/snapshot/samples/class-state-field-constructor-assignment/_expected/server/index.svelte.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,33 @@ export default function Class_state_field_constructor_assignment($$payload, $$pr
44
$.push();
55

66
class Foo {
7-
a;
7+
a = 0;
88
#b;
9+
#foo = $.derived(() => ({ bar: this.a * 2 }));
10+
11+
get foo() {
12+
return this.#foo();
13+
}
14+
15+
set foo($$value) {
16+
return this.#foo($$value);
17+
}
18+
19+
#bar = $.derived(() => ({ baz: this.foo }));
20+
21+
get bar() {
22+
return this.#bar();
23+
}
24+
25+
set bar($$value) {
26+
return this.#bar($$value);
27+
}
928

1029
constructor() {
1130
this.a = 1;
1231
this.#b = 2;
32+
this.foo.bar = 3;
33+
this.bar = 4;
1334
}
1435
}
1536

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
<script>
22
class Foo {
3-
a = $state();
3+
a = $state(0);
44
#b = $state();
5-
5+
foo = $derived({ bar: this.a * 2 });
6+
bar = $derived({ baz: this.foo });
67
constructor() {
78
this.a = 1;
89
this.#b = 2;
10+
this.foo.bar = 3;
11+
this.bar = 4;
912
}
1013
}
1114
</script>

0 commit comments

Comments
 (0)