@@ -237,42 +237,35 @@ 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 {{ declarations: ESTree.VariableDeclaration[] , paths: DestructuredAssignment[] } }
240+ * @returns {{ inserts: Array<{ id: ESTree.Identifier, value: ESTree.Expression }> , paths: DestructuredAssignment[] } }
241241 */
242242export function extract_paths ( param , initial ) {
243243 /**
244244 * When dealing with array destructuring patterns (`let [a, b, c] = $derived(blah())`)
245245 * we need an intermediate declaration that creates an array, since `blah()` could
246246 * return a non-array-like iterator
247- * @type {ESTree.VariableDeclaration[] }
247+ * @type {Array<{ id: ESTree.Identifier, value: ESTree.Expression }> }
248248 */
249- const declarations = [ ] ;
249+ const inserts = [ ] ;
250250
251251 /** @type {DestructuredAssignment[] } */
252252 const paths = [ ] ;
253253
254- _extract_paths ( paths , declarations , param , initial , initial , false ) ;
254+ _extract_paths ( paths , inserts , param , initial , initial , false ) ;
255255
256- return { declarations , paths } ;
256+ return { inserts , paths } ;
257257}
258258
259259/**
260260 * @param {DestructuredAssignment[] } paths
261- * @param {ESTree.VariableDeclaration[] } declarations
261+ * @param {Array<{ id: ESTree.Identifier, value: ESTree.Expression }> } inserts
262262 * @param {ESTree.Node } param
263263 * @param {ESTree.Expression } expression
264264 * @param {ESTree.Expression } update_expression
265265 * @param {boolean } has_default_value
266266 * @returns {DestructuredAssignment[] }
267267 */
268- function _extract_paths (
269- paths ,
270- declarations ,
271- param ,
272- expression ,
273- update_expression ,
274- has_default_value
275- ) {
268+ function _extract_paths ( paths , inserts , param , expression , update_expression , has_default_value ) {
276269 switch ( param . type ) {
277270 case 'Identifier' :
278271 case 'MemberExpression' :
@@ -316,7 +309,7 @@ function _extract_paths(
316309 } else {
317310 _extract_paths (
318311 paths ,
319- declarations ,
312+ inserts ,
320313 prop . argument ,
321314 rest_expression ,
322315 rest_expression ,
@@ -332,7 +325,7 @@ function _extract_paths(
332325
333326 _extract_paths (
334327 paths ,
335- declarations ,
328+ inserts ,
336329 prop . value ,
337330 object_expression ,
338331 object_expression ,
@@ -344,11 +337,26 @@ function _extract_paths(
344337 break ;
345338
346339 case 'ArrayPattern' : {
340+ // we create an intermediate declaration to convert iterables to arrays if necessary.
341+ // the consumer is responsible for setting the name of the identifier
342+ const id = b . id ( '#' ) ;
343+
344+ const is_rest = param . elements . at ( - 1 ) ?. type === 'RestElement' ;
345+ const call = b . call (
346+ '$.to_array' ,
347+ expression ,
348+ is_rest ? undefined : b . literal ( param . elements . length )
349+ ) ;
350+
351+ inserts . push ( { id, value : b . call ( '$.derived' , b . thunk ( call ) ) } ) ;
352+
353+ const array = b . call ( '$.get' , id ) ;
354+
347355 for ( let i = 0 ; i < param . elements . length ; i += 1 ) {
348356 const element = param . elements [ i ] ;
349357 if ( element ) {
350358 if ( element . type === 'RestElement' ) {
351- const rest_expression = b . call ( b . member ( expression , 'slice' ) , b . literal ( i ) ) ;
359+ const rest_expression = b . call ( b . member ( array , 'slice' ) , b . literal ( i ) ) ;
352360
353361 if ( element . argument . type === 'Identifier' ) {
354362 paths . push ( {
@@ -361,19 +369,19 @@ function _extract_paths(
361369 } else {
362370 _extract_paths (
363371 paths ,
364- declarations ,
372+ inserts ,
365373 element . argument ,
366374 rest_expression ,
367375 rest_expression ,
368376 has_default_value
369377 ) ;
370378 }
371379 } else {
372- const array_expression = b . member ( expression , b . literal ( i ) , true ) ;
380+ const array_expression = b . member ( array , b . literal ( i ) , true ) ;
373381
374382 _extract_paths (
375383 paths ,
376- declarations ,
384+ inserts ,
377385 element ,
378386 array_expression ,
379387 array_expression ,
@@ -398,14 +406,7 @@ function _extract_paths(
398406 update_expression
399407 } ) ;
400408 } else {
401- _extract_paths (
402- paths ,
403- declarations ,
404- param . left ,
405- fallback_expression ,
406- update_expression ,
407- true
408- ) ;
409+ _extract_paths ( paths , inserts , param . left , fallback_expression , update_expression , true ) ;
409410 }
410411
411412 break ;
0 commit comments