Skip to content

Commit b4382e4

Browse files
authored
fix: properly transform destructured $derived.by declarations (#12984)
fixes #12983
1 parent f1a1a08 commit b4382e4

File tree

4 files changed

+27
-1
lines changed

4 files changed

+27
-1
lines changed

.changeset/olive-llamas-warn.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: properly transform destructured `$derived.by` declarations

packages/svelte/src/compiler/phases/3-transform/client/visitors/VariableDeclaration.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ export function VariableDeclaration(node, context) {
173173
let id;
174174
let rhs = value;
175175

176-
if (init.arguments[0].type === 'Identifier') {
176+
if (rune === '$derived' && init.arguments[0].type === 'Identifier') {
177177
id = init.arguments[0];
178178
} else {
179179
id = b.id(context.state.scope.generate('$$d'));
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { flushSync } from 'svelte';
2+
import { test } from '../../test';
3+
4+
export default test({
5+
html: '<button>clicks: 0</button>',
6+
7+
test({ assert, target }) {
8+
const btn = target.querySelector('button');
9+
10+
flushSync(() => btn?.click());
11+
assert.htmlEqual(target.innerHTML, `<button>clicks: 1</button>`);
12+
}
13+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<script>
2+
let count = $state(0);
3+
let fn = () => ({ n: count });
4+
5+
let { n } = $derived.by(fn);
6+
</script>
7+
8+
<button onclick={() => (count += 1)}>clicks: {n}</button>

0 commit comments

Comments
 (0)