@@ -944,54 +944,53 @@ const instance_script = {
944
944
node . body . type === 'ExpressionStatement' &&
945
945
node . body . expression . type === 'AssignmentExpression'
946
946
) {
947
- const ids = extract_identifiers ( node . body . expression . left ) ;
948
- const [ , expression_ids ] = extract_all_identifiers_from_expression (
949
- node . body . expression . right
950
- ) ;
951
- const bindings = ids . map ( ( id ) => state . scope . get ( id . name ) ) ;
952
- const reassigned_bindings = bindings . filter ( ( b ) => b ?. reassigned ) ;
947
+ const { left, right } = node . body . expression ;
953
948
954
- if (
955
- node . body . expression . right . type !== 'Literal' &&
956
- ! bindings . some ( ( b ) => b ?. kind === 'store_sub' ) &&
957
- node . body . expression . left . type !== 'MemberExpression'
958
- ) {
959
- let { start, end } = /** @type {{ start: number, end: number } } */ (
960
- node . body . expression . right
961
- ) ;
949
+ const ids = extract_identifiers ( left ) ;
950
+ const [ , expression_ids ] = extract_all_identifiers_from_expression ( right ) ;
951
+ const bindings = ids . map ( ( id ) => /** @type {Binding } */ ( state . scope . get ( id . name ) ) ) ;
962
952
963
- check_rune_binding ( 'derived' ) ;
953
+ if ( bindings . every ( ( b ) => b . kind === 'legacy_reactive' ) ) {
954
+ if (
955
+ right . type !== 'Literal' &&
956
+ bindings . every ( ( b ) => b . kind !== 'store_sub' ) &&
957
+ left . type !== 'MemberExpression'
958
+ ) {
959
+ let { start, end } = /** @type {{ start: number, end: number } } */ ( right ) ;
964
960
965
- // $derived
966
- state . str . update (
967
- /** @type {number } */ ( node . start ) ,
968
- /** @type {number } */ ( node . body . expression . start ) ,
969
- 'let '
970
- ) ;
961
+ check_rune_binding ( 'derived' ) ;
971
962
972
- if ( node . body . expression . right . type === 'SequenceExpression' ) {
973
- while ( state . str . original [ start ] !== '(' ) start -= 1 ;
974
- while ( state . str . original [ end - 1 ] !== ')' ) end += 1 ;
975
- }
963
+ // $derived
964
+ state . str . update (
965
+ /** @type {number } */ ( node . start ) ,
966
+ /** @type {number } */ ( node . body . expression . start ) ,
967
+ 'let '
968
+ ) ;
969
+
970
+ if ( right . type === 'SequenceExpression' ) {
971
+ while ( state . str . original [ start ] !== '(' ) start -= 1 ;
972
+ while ( state . str . original [ end - 1 ] !== ')' ) end += 1 ;
973
+ }
974
+
975
+ state . str . prependRight ( start , `$derived(` ) ;
976
976
977
- state . str . prependRight ( start , `$derived(` ) ;
977
+ // in a case like `$: ({ a } = b())`, there's already a trailing parenthesis.
978
+ // otherwise, we need to add one
979
+ if ( state . str . original [ /** @type {number } */ ( node . body . start ) ] !== '(' ) {
980
+ state . str . appendLeft ( end , `)` ) ;
981
+ }
978
982
979
- // in a case like `$: ({ a } = b())`, there's already a trailing parenthesis.
980
- // otherwise, we need to add one
981
- if ( state . str . original [ /** @type {number } */ ( node . body . start ) ] !== '(' ) {
982
- state . str . appendLeft ( end , `)` ) ;
983
+ return ;
983
984
}
984
985
985
- return ;
986
- } else {
987
- for ( const binding of reassigned_bindings ) {
988
- if ( binding && ( ids . includes ( binding . node ) || expression_ids . length === 0 ) ) {
986
+ for ( const binding of bindings ) {
987
+ if ( binding . reassigned && ( ids . includes ( binding . node ) || expression_ids . length === 0 ) ) {
989
988
check_rune_binding ( 'state' ) ;
990
989
const init =
991
990
binding . kind === 'state'
992
991
? ' = $state()'
993
992
: expression_ids . length === 0
994
- ? ` = $state(${ state . str . original . substring ( /** @type {number } */ ( node . body . expression . right . start ) , node . body . expression . right . end ) } )`
993
+ ? ` = $state(${ state . str . original . substring ( /** @type {number } */ ( right . start ) , right . end ) } )`
995
994
: '' ;
996
995
// implicitly-declared variable which we need to make explicit
997
996
state . str . prependLeft (
@@ -1000,7 +999,8 @@ const instance_script = {
1000
999
) ;
1001
1000
}
1002
1001
}
1003
- if ( expression_ids . length === 0 && ! bindings . some ( ( b ) => b ?. kind === 'store_sub' ) ) {
1002
+
1003
+ if ( expression_ids . length === 0 && bindings . every ( ( b ) => b . kind !== 'store_sub' ) ) {
1004
1004
state . str . remove ( /** @type {number } */ ( node . start ) , /** @type {number } */ ( node . end ) ) ;
1005
1005
return ;
1006
1006
}
0 commit comments