fix: properly transform destructured `$derived.by` declarations (#12984)

fixes #12983
pull/12988/head
Rich Harris 3 months ago committed by GitHub
parent f1a1a08a74
commit b4382e422d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: properly transform destructured `$derived.by` declarations

@ -173,7 +173,7 @@ export function VariableDeclaration(node, context) {
let id;
let rhs = value;
if (init.arguments[0].type === 'Identifier') {
if (rune === '$derived' && init.arguments[0].type === 'Identifier') {
id = init.arguments[0];
} else {
id = b.id(context.state.scope.generate('$$d'));

@ -0,0 +1,13 @@
import { flushSync } from 'svelte';
import { test } from '../../test';
export default test({
html: '<button>clicks: 0</button>',
test({ assert, target }) {
const btn = target.querySelector('button');
flushSync(() => btn?.click());
assert.htmlEqual(target.innerHTML, `<button>clicks: 1</button>`);
}
});

@ -0,0 +1,8 @@
<script>
let count = $state(0);
let fn = () => ({ n: count });
let { n } = $derived.by(fn);
</script>
<button onclick={() => (count += 1)}>clicks: {n}</button>
Loading…
Cancel
Save