1- /** @import { CallExpression, Expression, Identifier, Literal, Program, VariableDeclaration, VariableDeclarator } from 'estree' */
1+ /** @import { CallExpression, Expression, Identifier, Literal, Node, Program, VariableDeclaration, VariableDeclarator } from 'estree' */
22/** @import { Binding } from '#compiler' */
33/** @import { ComponentContext, ParallelizedChunk } from '../types' */
44import { dev , is_ignored , locate_node } from '../../../../state.js' ;
@@ -23,12 +23,14 @@ import { get_value } from './shared/declarations.js';
2323export function VariableDeclaration ( node , context ) {
2424 /** @type {VariableDeclarator[] } */
2525 const declarations = [ ] ;
26+ const parent = /** @type {Node } */ ( context . path . at ( - 1 ) ) ;
27+ const position = /** @type {Program } */ ( parent ) . body ?. indexOf ?. ( node ) ;
2628
2729 if ( context . state . analysis . runes ) {
2830 for ( const declarator of node . declarations ) {
2931 const init = /** @type {Expression } */ ( declarator . init ) ;
3032 const rune = get_rune ( init , context . state . scope ) ;
31-
33+ const bindings = context . state . scope . get_bindings ( declarator ) ;
3234 if (
3335 ! rune ||
3436 rune === '$effect.tracking' ||
@@ -53,40 +55,34 @@ export function VariableDeclaration(node, context) {
5355 init . argument ,
5456 context . state . scope ,
5557 context . state . analysis ,
56- [
57- ...( context . state . current_parallelized_chunk ?. bindings ?? [ ] ) ,
58- ...context . state . scope . get_bindings ( declarator )
59- ]
58+ [ ...( context . state . current_parallelized_chunk ?. bindings ?? [ ] ) , ...bindings ]
6059 ) ;
6160 if ( parallelize ) {
62- const bindings = context . state . scope . get_bindings ( declarator ) ;
63- const visited = /** @type {VariableDeclarator } */ (
61+ const { id, init : visited_init } = /** @type {VariableDeclarator } */ (
6462 context . visit ( {
6563 ...declarator ,
6664 init : init . argument
6765 } )
6866 ) ;
69- const declarators = [
70- {
71- id : visited . id ,
72- init : /** @type {Expression } */ ( visited . init )
73- }
74- ] ;
67+ const _declarator = {
68+ id,
69+ init : /** @type {Expression } */ ( visited_init )
70+ } ;
7571 if (
7672 context . state . current_parallelized_chunk &&
7773 context . state . current_parallelized_chunk . kind === node . kind
7874 ) {
79- context . state . current_parallelized_chunk . declarators . push ( ... declarators ) ;
75+ context . state . current_parallelized_chunk . declarators . push ( _declarator ) ;
8076 context . state . current_parallelized_chunk . bindings . push ( ...bindings ) ;
8177 context . state . current_parallelized_chunk . position = /** @type {Program } */ (
82- context . path . at ( - 1 )
78+ parent
8379 ) . body . indexOf ( node ) ;
8480 } else {
8581 /** @type {ParallelizedChunk } */
8682 const chunk = {
8783 kind : node . kind ,
88- declarators,
89- position : /** @type { Program } */ ( context . path . at ( - 1 ) ) . body . indexOf ( node ) ,
84+ declarators : [ _declarator ] ,
85+ position,
9086 bindings
9187 } ;
9288 context . state . current_parallelized_chunk = chunk ;
@@ -185,9 +181,7 @@ export function VariableDeclaration(node, context) {
185181 * @param {Expression } value
186182 */
187183 const create_state_declarator = ( id , value ) => {
188- const binding = /** @type {import('#compiler').Binding } */ (
189- context . state . scope . get ( id . name )
190- ) ;
184+ const binding = /** @type {Binding } */ ( context . state . scope . get ( id . name ) ) ;
191185 const is_state = is_state_source ( binding , context . state . analysis ) ;
192186 const is_proxy = should_proxy ( value , context . state . scope ) ;
193187
@@ -386,15 +380,13 @@ export function VariableDeclaration(node, context) {
386380 ) {
387381 context . state . current_parallelized_chunk . declarators . push ( ...declarators ) ;
388382 context . state . current_parallelized_chunk . bindings . push ( ...bindings ) ;
389- context . state . current_parallelized_chunk . position = /** @type {Program } */ (
390- context . path . at ( - 1 )
391- ) . body . indexOf ( node ) ;
383+ context . state . current_parallelized_chunk . position = position ;
392384 } else {
393385 /** @type {ParallelizedChunk } */
394386 const chunk = {
395387 kind : node . kind ,
396388 declarators,
397- position : /** @type { Program } */ ( context . path . at ( - 1 ) ) . body . indexOf ( node ) ,
389+ position,
398390 bindings
399391 } ;
400392 context . state . current_parallelized_chunk = chunk ;
@@ -407,7 +399,7 @@ export function VariableDeclaration(node, context) {
407399 }
408400 } else {
409401 for ( const declarator of node . declarations ) {
410- const bindings = /** @type { Binding[] } */ ( context . state . scope . get_bindings ( declarator ) ) ;
402+ const bindings = context . state . scope . get_bindings ( declarator ) ;
411403 const has_state = bindings . some ( ( binding ) => binding . kind === 'state' ) ;
412404 const has_props = bindings . some ( ( binding ) => binding . kind === 'bindable_prop' ) ;
413405
@@ -510,13 +502,9 @@ export function VariableDeclaration(node, context) {
510502 * @param {Expression } value
511503 */
512504function create_state_declarators ( declarator , context , value ) {
505+ const immutable = context . state . analysis . immutable ? b . true : undefined ;
513506 if ( declarator . id . type === 'Identifier' ) {
514- return [
515- b . declarator (
516- declarator . id ,
517- b . call ( '$.mutable_source' , value , context . state . analysis . immutable ? b . true : undefined )
518- )
519- ] ;
507+ return [ b . declarator ( declarator . id , b . call ( '$.mutable_source' , value , immutable ) ) ] ;
520508 }
521509
522510 const tmp = b . id ( context . state . scope . generate ( 'tmp' ) ) ;
@@ -531,15 +519,13 @@ function create_state_declarators(declarator, context, value) {
531519 const expression = /** @type {Expression } */ ( context . visit ( b . thunk ( value ) ) ) ;
532520 return b . declarator ( id , b . call ( '$.derived' , expression ) ) ;
533521 } ) ,
534- ...paths . map ( ( path ) => {
535- const value = /** @type {Expression } */ ( context . visit ( path . expression ) ) ;
536- const binding = context . state . scope . get ( /** @type {Identifier } */ ( path . node ) . name ) ;
522+ ...paths . map ( ( { expression , node } ) => {
523+ const value = /** @type {Expression } */ ( context . visit ( expression ) ) ;
524+ const binding = context . state . scope . get ( /** @type {Identifier } */ ( node ) . name ) ;
537525
538526 return b . declarator (
539- path . node ,
540- binding ?. kind === 'state'
541- ? b . call ( '$.mutable_source' , value , context . state . analysis . immutable ? b . true : undefined )
542- : value
527+ node ,
528+ binding ?. kind === 'state' ? b . call ( '$.mutable_source' , value , immutable ) : value
543529 ) ;
544530 } )
545531 ] ;
0 commit comments