1
- /** @import { CallExpression, Expression, Identifier, Literal, Program, VariableDeclaration, VariableDeclarator } from 'estree' */
1
+ /** @import { CallExpression, Expression, Identifier, Literal, Node, Program, VariableDeclaration, VariableDeclarator } from 'estree' */
2
2
/** @import { Binding } from '#compiler' */
3
3
/** @import { ComponentContext, ParallelizedChunk } from '../types' */
4
4
import { dev , is_ignored , locate_node } from '../../../../state.js' ;
@@ -23,12 +23,14 @@ import { get_value } from './shared/declarations.js';
23
23
export function VariableDeclaration ( node , context ) {
24
24
/** @type {VariableDeclarator[] } */
25
25
const declarations = [ ] ;
26
+ const parent = /** @type {Node } */ ( context . path . at ( - 1 ) ) ;
27
+ const position = /** @type {Program } */ ( parent ) . body ?. indexOf ?. ( node ) ;
26
28
27
29
if ( context . state . analysis . runes ) {
28
30
for ( const declarator of node . declarations ) {
29
31
const init = /** @type {Expression } */ ( declarator . init ) ;
30
32
const rune = get_rune ( init , context . state . scope ) ;
31
-
33
+ const bindings = context . state . scope . get_bindings ( declarator ) ;
32
34
if (
33
35
! rune ||
34
36
rune === '$effect.tracking' ||
@@ -53,40 +55,34 @@ export function VariableDeclaration(node, context) {
53
55
init . argument ,
54
56
context . state . scope ,
55
57
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 ]
60
59
) ;
61
60
if ( parallelize ) {
62
- const bindings = context . state . scope . get_bindings ( declarator ) ;
63
- const visited = /** @type {VariableDeclarator } */ (
61
+ const { id, init : visited_init } = /** @type {VariableDeclarator } */ (
64
62
context . visit ( {
65
63
...declarator ,
66
64
init : init . argument
67
65
} )
68
66
) ;
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
+ } ;
75
71
if (
76
72
context . state . current_parallelized_chunk &&
77
73
context . state . current_parallelized_chunk . kind === node . kind
78
74
) {
79
- context . state . current_parallelized_chunk . declarators . push ( ... declarators ) ;
75
+ context . state . current_parallelized_chunk . declarators . push ( _declarator ) ;
80
76
context . state . current_parallelized_chunk . bindings . push ( ...bindings ) ;
81
77
context . state . current_parallelized_chunk . position = /** @type {Program } */ (
82
- context . path . at ( - 1 )
78
+ parent
83
79
) . body . indexOf ( node ) ;
84
80
} else {
85
81
/** @type {ParallelizedChunk } */
86
82
const chunk = {
87
83
kind : node . kind ,
88
- declarators,
89
- position : /** @type { Program } */ ( context . path . at ( - 1 ) ) . body . indexOf ( node ) ,
84
+ declarators : [ _declarator ] ,
85
+ position,
90
86
bindings
91
87
} ;
92
88
context . state . current_parallelized_chunk = chunk ;
@@ -185,9 +181,7 @@ export function VariableDeclaration(node, context) {
185
181
* @param {Expression } value
186
182
*/
187
183
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 ) ) ;
191
185
const is_state = is_state_source ( binding , context . state . analysis ) ;
192
186
const is_proxy = should_proxy ( value , context . state . scope ) ;
193
187
@@ -386,15 +380,13 @@ export function VariableDeclaration(node, context) {
386
380
) {
387
381
context . state . current_parallelized_chunk . declarators . push ( ...declarators ) ;
388
382
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 ;
392
384
} else {
393
385
/** @type {ParallelizedChunk } */
394
386
const chunk = {
395
387
kind : node . kind ,
396
388
declarators,
397
- position : /** @type { Program } */ ( context . path . at ( - 1 ) ) . body . indexOf ( node ) ,
389
+ position,
398
390
bindings
399
391
} ;
400
392
context . state . current_parallelized_chunk = chunk ;
@@ -407,7 +399,7 @@ export function VariableDeclaration(node, context) {
407
399
}
408
400
} else {
409
401
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 ) ;
411
403
const has_state = bindings . some ( ( binding ) => binding . kind === 'state' ) ;
412
404
const has_props = bindings . some ( ( binding ) => binding . kind === 'bindable_prop' ) ;
413
405
@@ -510,13 +502,9 @@ export function VariableDeclaration(node, context) {
510
502
* @param {Expression } value
511
503
*/
512
504
function create_state_declarators ( declarator , context , value ) {
505
+ const immutable = context . state . analysis . immutable ? b . true : undefined ;
513
506
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 ) ) ] ;
520
508
}
521
509
522
510
const tmp = b . id ( context . state . scope . generate ( 'tmp' ) ) ;
@@ -531,15 +519,13 @@ function create_state_declarators(declarator, context, value) {
531
519
const expression = /** @type {Expression } */ ( context . visit ( b . thunk ( value ) ) ) ;
532
520
return b . declarator ( id , b . call ( '$.derived' , expression ) ) ;
533
521
} ) ,
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 ) ;
537
525
538
526
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
543
529
) ;
544
530
} )
545
531
] ;
0 commit comments