Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ensure class constructor values are proxied #9888

Merged
merged 2 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tall-books-grin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: ensure class constructor values are proxied
35 changes: 32 additions & 3 deletions packages/svelte/src/compiler/phases/3-transform/client/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,16 +157,45 @@ export function serialize_set_binding(node, context, fallback) {

let left = node.left;

// Handle class private/public state assignment cases
while (left.type === 'MemberExpression') {
if (left.object.type === 'ThisExpression' && left.property.type === 'PrivateIdentifier') {
if (context.state.private_state.has(left.property.name) && !state.in_constructor) {
const value = get_assignment_value(node, context);
if (
left.object.type === 'ThisExpression' &&
left.property.type === 'PrivateIdentifier' &&
context.state.private_state.has(left.property.name)
) {
const value = get_assignment_value(node, context);
if (state.in_constructor) {
// See if we should wrap value in $.proxy
if (context.state.analysis.runes && should_proxy(value)) {
const assignment = fallback();
if (assignment.type === 'AssignmentExpression') {
assignment.right = b.call('$.proxy', value);
return assignment;
}
}
} else {
return b.call(
'$.set',
left,
context.state.analysis.runes && should_proxy(value) ? b.call('$.proxy', value) : value
);
}
} else if (
left.object.type === 'ThisExpression' &&
left.property.type === 'Identifier' &&
context.state.public_state.has(left.property.name) &&
state.in_constructor
) {
const value = get_assignment_value(node, context);
// See if we should wrap value in $.proxy
if (context.state.analysis.runes && should_proxy(value)) {
const assignment = fallback();
if (assignment.type === 'AssignmentExpression') {
assignment.right = b.call('$.proxy', value);
return assignment;
}
}
}
// @ts-expect-error
left = left.object;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { test } from '../../test';

export default test({
html: `<button>0</button>`,

async test({ assert, target }) {
const btn = target.querySelector('button');

await btn?.click();
assert.htmlEqual(target.innerHTML, `<button>1</button>`);

await btn?.click();
assert.htmlEqual(target.innerHTML, `<button>2</button>`);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<script>
class Counter {
#count = $state();

constructor(v) {
this.#count = v;
}

get count() {
return this.#count;
}
}
const counter = new Counter({ count: 0 });
</script>

<button on:click={() => counter.count.count++}>{counter.count.count}</button>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { test } from '../../test';

export default test({
html: `<button>0</button>`,

async test({ assert, target }) {
const btn = target.querySelector('button');

await btn?.click();
assert.htmlEqual(target.innerHTML, `<button>1</button>`);

await btn?.click();
assert.htmlEqual(target.innerHTML, `<button>2</button>`);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<script>
class Counter {
count = $state();

constructor(v) {
this.count = v;
}
}
const counter = new Counter({ count: 0 });
</script>

<button on:click={() => counter.count.count++}>{counter.count.count}</button>