@@ -141,22 +141,29 @@ export function Program(node, context) {
141141 const body = [ ] ;
142142 for ( let i = 0 ; i < node . body . length ; i ++ ) {
143143 const transformed = /** @type {Program['body'][number] } */ ( context . visit ( node . body [ i ] ) ) ;
144- const chunk = context . state . parallelized_chunks ?. at ( - 1 ) ;
145144 body . push ( transformed ) ;
146- if ( chunk && chunk . position === i ) {
147- if ( chunk . declarators . length === 1 ) {
148- const declarator = chunk . declarators [ 0 ] ;
149- body . push (
150- b . declaration ( chunk . kind , [
151- b . declarator ( declarator . id , b . call ( b . await ( b . call ( '$.save' , declarator . init ) ) ) )
152- ] )
153- ) ;
154- } else {
155- const pattern = b . array_pattern ( chunk . declarators . map ( ( { id } ) => id ) ) ;
156- const init = b . call ( '$.all' , ...chunk . declarators . map ( ( { init } ) => init ) ) ;
157- body . push ( b . declaration ( chunk . kind , [ b . declarator ( pattern , b . await ( init ) ) ] ) ) ;
158- }
145+ }
146+ let offset = 0 ;
147+ for ( const chunk of context . state . parallelized_chunks ) {
148+ if ( chunk . declarators . length === 1 ) {
149+ const declarator = chunk . declarators [ 0 ] ;
150+ body . splice (
151+ chunk . position + offset ,
152+ 0 ,
153+ b . declaration ( chunk . kind , [
154+ b . declarator ( declarator . id , b . call ( b . await ( b . call ( '$.save' , declarator . init ) ) ) )
155+ ] )
156+ ) ;
157+ } else {
158+ const pattern = b . array_pattern ( chunk . declarators . map ( ( { id } ) => id ) ) ;
159+ const init = b . call ( '$.all' , ...chunk . declarators . map ( ( { init } ) => init ) ) ;
160+ body . splice (
161+ chunk . position + offset ,
162+ 0 ,
163+ b . declaration ( chunk . kind , [ b . declarator ( pattern , b . await ( init ) ) ] )
164+ ) ;
159165 }
166+ offset ++ ;
160167 }
161168 return {
162169 ...node ,
0 commit comments