Skip to content

Commit 0cc2b4e

Browse files
committed
WIP
1 parent 8e021c8 commit 0cc2b4e

File tree

1 file changed

+44
-16
lines changed
  • packages/svelte/src/compiler/utils

1 file changed

+44
-16
lines changed

packages/svelte/src/compiler/utils/ast.js

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -237,30 +237,46 @@ export function extract_identifiers_from_destructuring(node, nodes = []) {
237237
* Extracts all destructured assignments from a pattern.
238238
* @param {ESTree.Node} param
239239
* @param {ESTree.Expression} initial
240-
* @returns {{ paths: DestructuredAssignment[] }}
240+
* @returns {{ declarations: ESTree.VariableDeclaration[], paths: DestructuredAssignment[] }}
241241
*/
242242
export function extract_paths(param, initial) {
243+
/**
244+
* When dealing with array destructuring patterns (`let [a, b, c] = $derived(blah())`)
245+
* we need an intermediate declaration that creates an array, since `blah()` could
246+
* return a non-array-like iterator
247+
* @type {ESTree.VariableDeclaration[]}
248+
*/
249+
const declarations = [];
250+
243251
/** @type {DestructuredAssignment[]} */
244252
const paths = [];
245253

246-
_extract_paths(paths, param, initial, initial, false);
254+
_extract_paths(paths, declarations, param, initial, initial, false);
247255

248-
return { paths };
256+
return { declarations, paths };
249257
}
250258

251259
/**
252-
* @param {DestructuredAssignment[]} assignments
260+
* @param {DestructuredAssignment[]} paths
261+
* @param {ESTree.VariableDeclaration[]} declarations
253262
* @param {ESTree.Node} param
254263
* @param {ESTree.Expression} expression
255264
* @param {ESTree.Expression} update_expression
256265
* @param {boolean} has_default_value
257266
* @returns {DestructuredAssignment[]}
258267
*/
259-
function _extract_paths(assignments = [], param, expression, update_expression, has_default_value) {
268+
function _extract_paths(
269+
paths,
270+
declarations,
271+
param,
272+
expression,
273+
update_expression,
274+
has_default_value
275+
) {
260276
switch (param.type) {
261277
case 'Identifier':
262278
case 'MemberExpression':
263-
assignments.push({
279+
paths.push({
264280
node: param,
265281
is_rest: false,
266282
has_default_value,
@@ -290,7 +306,7 @@ function _extract_paths(assignments = [], param, expression, update_expression,
290306
const rest_expression = b.call('$.exclude_from_object', expression, b.array(props));
291307

292308
if (prop.argument.type === 'Identifier') {
293-
assignments.push({
309+
paths.push({
294310
node: prop.argument,
295311
is_rest: true,
296312
has_default_value,
@@ -299,7 +315,8 @@ function _extract_paths(assignments = [], param, expression, update_expression,
299315
});
300316
} else {
301317
_extract_paths(
302-
assignments,
318+
paths,
319+
declarations,
303320
prop.argument,
304321
rest_expression,
305322
rest_expression,
@@ -314,7 +331,8 @@ function _extract_paths(assignments = [], param, expression, update_expression,
314331
);
315332

316333
_extract_paths(
317-
assignments,
334+
paths,
335+
declarations,
318336
prop.value,
319337
object_expression,
320338
object_expression,
@@ -325,15 +343,15 @@ function _extract_paths(assignments = [], param, expression, update_expression,
325343

326344
break;
327345

328-
case 'ArrayPattern':
346+
case 'ArrayPattern': {
329347
for (let i = 0; i < param.elements.length; i += 1) {
330348
const element = param.elements[i];
331349
if (element) {
332350
if (element.type === 'RestElement') {
333351
const rest_expression = b.call(b.member(expression, 'slice'), b.literal(i));
334352

335353
if (element.argument.type === 'Identifier') {
336-
assignments.push({
354+
paths.push({
337355
node: element.argument,
338356
is_rest: true,
339357
has_default_value,
@@ -342,7 +360,8 @@ function _extract_paths(assignments = [], param, expression, update_expression,
342360
});
343361
} else {
344362
_extract_paths(
345-
assignments,
363+
paths,
364+
declarations,
346365
element.argument,
347366
rest_expression,
348367
rest_expression,
@@ -353,7 +372,8 @@ function _extract_paths(assignments = [], param, expression, update_expression,
353372
const array_expression = b.member(expression, b.literal(i), true);
354373

355374
_extract_paths(
356-
assignments,
375+
paths,
376+
declarations,
357377
element,
358378
array_expression,
359379
array_expression,
@@ -364,27 +384,35 @@ function _extract_paths(assignments = [], param, expression, update_expression,
364384
}
365385

366386
break;
387+
}
367388

368389
case 'AssignmentPattern': {
369390
const fallback_expression = build_fallback(expression, param.right);
370391

371392
if (param.left.type === 'Identifier') {
372-
assignments.push({
393+
paths.push({
373394
node: param.left,
374395
is_rest: false,
375396
has_default_value: true,
376397
expression: fallback_expression,
377398
update_expression
378399
});
379400
} else {
380-
_extract_paths(assignments, param.left, fallback_expression, update_expression, true);
401+
_extract_paths(
402+
paths,
403+
declarations,
404+
param.left,
405+
fallback_expression,
406+
update_expression,
407+
true
408+
);
381409
}
382410

383411
break;
384412
}
385413
}
386414

387-
return assignments;
415+
return paths;
388416
}
389417

390418
/**

0 commit comments

Comments
 (0)