@@ -468,27 +468,23 @@ const instance_script = {
468
468
*/
469
469
let labeled_statement ;
470
470
471
+ // Analyze declaration bindings to see if they're exclusively updated within a single reactive statement
471
472
const possible_derived = bindings . every ( ( binding ) =>
472
473
binding . references . every ( ( reference ) => {
473
- const declaration_idx = reference . path . find ( ( el ) => el . type === 'VariableDeclaration' ) ;
474
- const assignment_idx = reference . path . find ( ( el ) => el . type === 'AssignmentExpression' ) ;
475
- const update_idx = reference . path . find ( ( el ) => el . type === 'UpdateExpression' ) ;
476
- const labeled_idx = reference . path . find (
474
+ const declaration = reference . path . find ( ( el ) => el . type === 'VariableDeclaration' ) ;
475
+ const assignment = reference . path . find ( ( el ) => el . type === 'AssignmentExpression' ) ;
476
+ const update = reference . path . find ( ( el ) => el . type === 'UpdateExpression' ) ;
477
+ const labeled = reference . path . find (
477
478
( el ) => el . type === 'LabeledStatement' && el . label . name === '$'
478
479
) ;
479
480
480
- if ( assignment_idx && labeled_idx ) {
481
+ if ( assignment && labeled ) {
481
482
if ( assignment_in_labeled ) return false ;
482
- assignment_in_labeled = /** @type {AssignmentExpression } */ ( assignment_idx ) ;
483
- labeled_statement = /** @type {LabeledStatement } */ ( labeled_idx ) ;
483
+ assignment_in_labeled = /** @type {AssignmentExpression } */ ( assignment ) ;
484
+ labeled_statement = /** @type {LabeledStatement } */ ( labeled ) ;
484
485
}
485
486
486
- return (
487
- ! update_idx &&
488
- ( declaration_idx ||
489
- ( labeled_idx && assignment_idx ) ||
490
- ( ! labeled_idx && ! assignment_idx ) )
491
- ) ;
487
+ return ! update && ( declaration || ( labeled && assignment ) || ( ! labeled && ! assignment ) ) ;
492
488
} )
493
489
) ;
494
490
@@ -521,6 +517,7 @@ const instance_script = {
521
517
labeled_statement &&
522
518
( labeled_has_single_assignment || is_expression_assignment )
523
519
) {
520
+ // Someone wrote a `$: { ... }` statement which we can turn into a `$derived`
524
521
state . str . appendRight (
525
522
/** @type {number } */ ( declarator . id . typeAnnotation ?. end ?? declarator . id . end ) ,
526
523
' = $derived('
@@ -550,6 +547,7 @@ const instance_script = {
550
547
' = $state('
551
548
) ;
552
549
if ( should_be_state ) {
550
+ // someone wrote a `$: foo = ...` statement which we can turn into `let foo = $state(...)`
553
551
state . str . appendRight (
554
552
/** @type {number } */ ( declarator . id . typeAnnotation ?. end ?? declarator . id . end ) ,
555
553
state . str
0 commit comments