Skip to content

Commit 608a17f

Browse files
committed
spread at tmp declaration
1 parent 6bc1235 commit 608a17f

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,9 +308,14 @@ function create_state_declarators(declarator, { scope, analysis }, value) {
308308
}
309309

310310
const tmp = scope.generate('tmp');
311-
const paths = extract_paths(declarator.id, is_array(declarator.init, scope));
311+
const paths = extract_paths(declarator.id, true);
312312
return [
313-
b.declarator(b.id(tmp), value),
313+
b.declarator(
314+
b.id(tmp),
315+
declarator.id.type === 'ArrayPattern' && !is_array(value, scope)
316+
? b.array([b.spread(value)])
317+
: value
318+
),
314319
...paths.map((path) => {
315320
const value = path.expression?.(b.id(tmp));
316321
const binding = scope.get(/** @type {Identifier} */ (path.node).name);

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,14 @@ function create_state_declarators(declarator, scope, value) {
182182
}
183183

184184
const tmp = scope.generate('tmp');
185-
const paths = extract_paths(declarator.id, is_array(declarator.init, scope));
185+
const paths = extract_paths(declarator.id, true);
186186
return [
187-
b.declarator(b.id(tmp), value), // TODO inject declarator for opts, so we can use it below
187+
b.declarator(
188+
b.id(tmp),
189+
declarator.id.type === 'ArrayPattern' && !is_array(value, scope)
190+
? b.array([b.spread(value)])
191+
: value
192+
), // TODO inject declarator for opts, so we can use it below
188193
...paths.map((path) => {
189194
const value = path.expression?.(b.id(tmp));
190195
return b.declarator(path.node, value);

packages/svelte/src/compiler/phases/3-transform/shared/assignments.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/** @import { AssignmentExpression, AssignmentOperator, Expression, Node, Pattern } from 'estree' */
22
/** @import { Context as ClientContext } from '../client/types.js' */
33
/** @import { Context as ServerContext } from '../server/types.js' */
4-
import { extract_paths, is_expression_async } from '../../../utils/ast.js';
4+
import { extract_paths, is_array, is_expression_async } from '../../../utils/ast.js';
55
import * as b from '#compiler/builders';
66

77
/**
@@ -23,7 +23,10 @@ export function visit_assignment_expression(node, context, build_assignment) {
2323

2424
let changed = false;
2525

26-
const assignments = extract_paths(node.left).map((path) => {
26+
const assignments = extract_paths(
27+
node.left,
28+
!should_cache && is_array(value, context.state.scope)
29+
).map((path) => {
2730
const value = path.expression?.(rhs);
2831

2932
let assignment = build_assignment('=', path.node, value, context);
@@ -54,7 +57,14 @@ export function visit_assignment_expression(node, context, build_assignment) {
5457

5558
if (should_cache) {
5659
// the right hand side is a complex expression, wrap in an IIFE to cache it
57-
const iife = b.arrow([rhs], sequence);
60+
const iife = b.arrow(
61+
[
62+
!is_array(value, context.state.scope) && node.left.type === 'ArrayPattern'
63+
? b.array_pattern([b.rest(rhs)])
64+
: rhs
65+
],
66+
sequence
67+
);
5868

5969
const iife_is_async =
6070
is_expression_async(value) ||

0 commit comments

Comments
 (0)