fix: use `get` in constructor for deriveds ()

Co-authored-by: Rich Harris <rich.harris@vercel.com>
pull/15575/head
Paolo Ricciuti 2 weeks ago committed by GitHub
parent 83d0c5894d
commit ade66c6fea
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: use `get` in constructor for deriveds

@ -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);
}
}

@ -0,0 +1,5 @@
import { test } from '../../test';
export default test({
html: `<p>state,derived state,derived.by derived state</p>`
});

@ -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>
Loading…
Cancel
Save