Skip to content

Commit

Permalink
fix: ensure class constructor values are proxied (#9888)
Browse files Browse the repository at this point in the history
* fix: ensure class constructor values are proxied

* debugger
  • Loading branch information
trueadm committed Dec 11, 2023
1 parent 646c0c4 commit bdd63c8
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 3 deletions.
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>

1 comment on commit bdd63c8

@vercel
Copy link

@vercel vercel bot commented on bdd63c8 Dec 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

svelte-5-preview – ./sites/svelte-5-preview

svelte-5-preview.vercel.app
svelte-octane.vercel.app
svelte-5-preview-svelte.vercel.app
svelte-5-preview-git-main-svelte.vercel.app

Please sign in to comment.