Skip to content

Commit 710f73f

Browse files
authored
fix: improve derived rune destructuring support (#10665)
1 parent 61e7442 commit 710f73f

File tree

4 files changed

+53
-4
lines changed

4 files changed

+53
-4
lines changed

.changeset/tasty-cheetahs-appear.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: improve derived rune destructuring support

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -299,15 +299,22 @@ export const javascript_visitors_runes = {
299299
);
300300
} else {
301301
const bindings = state.scope.get_bindings(declarator);
302-
const id = state.scope.generate('derived_value');
302+
const object_id = state.scope.generate('derived_object');
303+
const values_id = state.scope.generate('derived_values');
303304
declarations.push(
304305
b.declarator(
305-
b.id(id),
306+
b.id(object_id),
307+
b.call('$.derived', b.thunk(rune === '$derived.by' ? b.call(value) : value))
308+
)
309+
);
310+
declarations.push(
311+
b.declarator(
312+
b.id(values_id),
306313
b.call(
307314
'$.derived',
308315
b.thunk(
309316
b.block([
310-
b.let(declarator.id, rune === '$derived.by' ? b.call(value) : value),
317+
b.let(declarator.id, b.call('$.get', b.id(object_id))),
311318
b.return(b.array(bindings.map((binding) => binding.node)))
312319
])
313320
)
@@ -321,7 +328,7 @@ export const javascript_visitors_runes = {
321328
binding.node,
322329
b.call(
323330
'$.derived',
324-
b.thunk(b.member(b.call('$.get', b.id(id)), b.literal(i), true))
331+
b.thunk(b.member(b.call('$.get', b.id(values_id)), b.literal(i), true))
325332
)
326333
)
327334
);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { test } from '../../test';
2+
3+
export default test({
4+
async test({ assert, target }) {
5+
const btn = target.querySelector('button');
6+
await btn?.click();
7+
8+
assert.htmlEqual(target.innerHTML, `<button>1 1 1</button>`);
9+
10+
await btn?.click();
11+
12+
assert.htmlEqual(target.innerHTML, `<button>2 2 2</button>`);
13+
}
14+
});
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
<script>
3+
function get_values() {
4+
let a = $state(0);
5+
let b = $state(0);
6+
let c = $state(0);
7+
8+
return {
9+
get a() { return a },
10+
get b() { return b },
11+
get c() { return c },
12+
increment() {
13+
a++;
14+
b++;
15+
c++;
16+
}
17+
};
18+
}
19+
20+
const { a, b, c, increment } = $derived(get_values());
21+
</script>
22+
23+
<button onclick={increment}>{a} {b} {c}</button>

0 commit comments

Comments
 (0)