Skip to content

Commit 7de3e3b

Browse files
feat: simplify derived object destructuring (#12781)
* simplify derived object destructuring * add test for destructuring an array * add changeset * shorter temp variable name * skip intermediate derived for simple cases --------- Co-authored-by: Rich Harris <[email protected]>
1 parent d6e26c0 commit 7de3e3b

File tree

4 files changed

+30
-32
lines changed

4 files changed

+30
-32
lines changed

.changeset/wicked-years-drive.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
feat: simplify derived object destructuring

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

+20-30
Original file line numberDiff line numberDiff line change
@@ -171,39 +171,29 @@ export function VariableDeclaration(node, context) {
171171
)
172172
);
173173
} else {
174-
const bindings = context.state.scope.get_bindings(declarator);
175-
const object_id = context.state.scope.generate('derived_object');
176-
const values_id = context.state.scope.generate('derived_values');
177-
declarations.push(
178-
b.declarator(
179-
b.id(object_id),
180-
b.call('$.derived', rune === '$derived.by' ? value : b.thunk(value))
181-
)
182-
);
183-
declarations.push(
184-
b.declarator(
185-
b.id(values_id),
186-
b.call(
187-
'$.derived',
188-
b.thunk(
189-
b.block([
190-
b.let(declarator.id, b.call('$.get', b.id(object_id))),
191-
b.return(b.array(bindings.map((binding) => binding.node)))
192-
])
193-
)
194-
)
195-
)
196-
);
174+
const bindings = extract_paths(declarator.id);
175+
176+
const init = /** @type {CallExpression} */ (declarator.init);
177+
178+
/** @type {Identifier} */
179+
let id;
180+
let rhs = value;
181+
182+
if (init.arguments[0].type === 'Identifier') {
183+
id = init.arguments[0];
184+
} else {
185+
id = b.id(context.state.scope.generate('$$d'));
186+
rhs = b.call('$.get', id);
187+
188+
declarations.push(
189+
b.declarator(id, b.call('$.derived', rune === '$derived.by' ? value : b.thunk(value)))
190+
);
191+
}
192+
197193
for (let i = 0; i < bindings.length; i++) {
198194
const binding = bindings[i];
199195
declarations.push(
200-
b.declarator(
201-
binding.node,
202-
b.call(
203-
'$.derived',
204-
b.thunk(b.member(b.call('$.get', b.id(values_id)), b.literal(i), true))
205-
)
206-
)
196+
b.declarator(binding.node, b.call('$.derived', b.thunk(binding.expression(rhs))))
207197
);
208198
}
209199
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { test } from '../../test';
22

33
export default test({
4-
html: `true 1 2 baz`
4+
html: `true 1 2 baz 1 2 3`
55
});
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<script>
22
let stuff = $state({ foo: true, bar: [1, 2, {baz: 'baz'}] });
33
let { foo, bar: [a, b, { baz }]} = $derived(stuff);
4+
5+
let stuff2 = $state([1, 2, 3]);
6+
let [d, e, f] = $derived(stuff2);
47
</script>
58

6-
{foo} {a} {b} {baz}
9+
{foo} {a} {b} {baz} {d} {e} {f}

0 commit comments

Comments
 (0)