mirror of https://github.com/sveltejs/svelte
fix: make deriveds on the server lazy again (#15964)
* fix: make deriveds on the server lazy again Fixes a regression introduced in #15820: deriveds need to be lazily called on the server, too, since they can close over variables only later defined Fixes #15960 * fix: handle basic assignment of deriveds on the server * fix: use `build_assignment_value` for deriveds assignments * use once * allow writing to public deriveds on server --------- Co-authored-by: paoloricciuti <ricciutipaolo@gmail.com>pull/15989/head
parent
9250c9cb36
commit
ac046df40a
@ -0,0 +1,5 @@
|
||||
---
|
||||
'svelte': patch
|
||||
---
|
||||
|
||||
fix: make deriveds on the server lazy again
|
@ -0,0 +1,23 @@
|
||||
/** @import { ClassBody, MemberExpression } from 'estree' */
|
||||
/** @import { Context } from '../types.js' */
|
||||
import * as b from '#compiler/builders';
|
||||
|
||||
/**
|
||||
* @param {MemberExpression} node
|
||||
* @param {Context} context
|
||||
*/
|
||||
export function MemberExpression(node, context) {
|
||||
if (
|
||||
context.state.analysis.runes &&
|
||||
node.object.type === 'ThisExpression' &&
|
||||
node.property.type === 'PrivateIdentifier'
|
||||
) {
|
||||
const field = context.state.state_fields?.get(`#${node.property.name}`);
|
||||
|
||||
if (field?.type === '$derived' || field?.type === '$derived.by') {
|
||||
return b.call(node);
|
||||
}
|
||||
}
|
||||
|
||||
context.next();
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
import { test } from '../../test';
|
||||
|
||||
export default test({
|
||||
html: `3 3 3 3`
|
||||
});
|
@ -0,0 +1,29 @@
|
||||
<script>
|
||||
class X {
|
||||
x = $state(1);
|
||||
on_class = $derived(this.x * 2);
|
||||
#on_class_private = $derived(this.x * 2);
|
||||
#in_constructor_private
|
||||
|
||||
constructor() {
|
||||
this.#in_constructor_private = $derived(this.x * 2);
|
||||
this.in_constructor = $derived(this.x * 2);
|
||||
this.#on_class_private = 3;
|
||||
this.#in_constructor_private = 3;
|
||||
}
|
||||
|
||||
get on_class_private() {
|
||||
return this.#on_class_private;
|
||||
}
|
||||
|
||||
get in_constructor_private() {
|
||||
return this.#in_constructor_private;
|
||||
}
|
||||
}
|
||||
|
||||
const x = new X();
|
||||
x.on_class = 3;
|
||||
x.in_constructor = 3;
|
||||
</script>
|
||||
|
||||
{x.on_class} {x.in_constructor} {x.on_class_private} {x.in_constructor_private}
|
Loading…
Reference in new issue