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: use get in constructor for deriveds #15300

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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/new-cherries-leave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: use `get` in constructor for deriveds
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ export function MemberExpression(node, context) {
if (node.property.type === 'PrivateIdentifier') {
const field = context.state.private_state.get(node.property.name);
if (field) {
return context.state.in_constructor ? b.member(node, 'v') : b.call('$.get', node);
return context.state.in_constructor && (field.kind === 'raw_state' || field.kind === 'state')
? b.member(node, 'v')
: b.call('$.get', node);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { test } from '../../test';

export default test({
html: `<p>state,derived state,derived.by derived state</p>`
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<script>
class Foo {
#state = $state('state')
#derived = $derived('derived ' + this.#state);
#derivedBy = $derived.by(() => {
return 'derived.by ' + this.#derived
});

initial

constructor() {
this.initial = [this.#state, this.#derived, this.#derivedBy]
}
}
const foo = new Foo()
</script>

<p>{foo.initial}</p>