@ -944,54 +944,53 @@ const instance_script = {
node . body . type === 'ExpressionStatement' &&
node . body . type === 'ExpressionStatement' &&
node . body . expression . type === 'AssignmentExpression'
node . body . expression . type === 'AssignmentExpression'
) {
) {
const ids = extract _identifiers ( node . body . expression . left ) ;
const { left , right } = node . body . expression ;
const [ , expression _ids ] = extract _all _identifiers _from _expression (
node . body . expression . right
) ;
const bindings = ids . map ( ( id ) => state . scope . get ( id . name ) ) ;
const reassigned _bindings = bindings . filter ( ( b ) => b ? . reassigned ) ;
if (
const ids = extract _identifiers ( left ) ;
node . body . expression . right . type !== 'Literal' &&
const [ , expression _ids ] = extract _all _identifiers _from _expression ( right ) ;
! bindings . some ( ( b ) => b ? . kind === 'store_sub' ) &&
const bindings = ids . map ( ( id ) => /** @type {Binding} */ ( state . scope . get ( id . name ) ) ) ;
node . body . expression . left . type !== 'MemberExpression'
) {
let { start , end } = /** @type {{ start: number, end: number }} */ (
node . body . expression . right
) ;
check _rune _binding ( 'derived' ) ;
if ( bindings . every ( ( b ) => b . kind === 'legacy_reactive' ) ) {
if (
right . type !== 'Literal' &&
bindings . every ( ( b ) => b . kind !== 'store_sub' ) &&
left . type !== 'MemberExpression'
) {
let { start , end } = /** @type {{ start: number, end: number }} */ ( right ) ;
// $derived
check _rune _binding ( 'derived' ) ;
state . str . update (
/** @type {number} */ ( node . start ) ,
/** @type {number} */ ( node . body . expression . start ) ,
'let '
) ;
if ( node . body . expression . right . type === 'SequenceExpression' ) {
// $derived
while ( state . str . original [ start ] !== '(' ) start -= 1 ;
state . str . update (
while ( state . str . original [ end - 1 ] !== ')' ) end += 1 ;
/** @type {number} */ ( node . start ) ,
}
/** @type {number} */ ( node . body . expression . start ) ,
'let '
) ;
if ( right . type === 'SequenceExpression' ) {
while ( state . str . original [ start ] !== '(' ) start -= 1 ;
while ( state . str . original [ end - 1 ] !== ')' ) end += 1 ;
}
state . str . prependRight ( start , ` $ derived( ` ) ;
state . str . prependRight ( start , ` $ derived( ` ) ;
// in a case like `$: ({ a } = b())`, there's already a trailing parenthesis.
// otherwise, we need to add one
if ( state . str . original [ /** @type {number} */ ( node . body . start ) ] !== '(' ) {
state . str . appendLeft ( end , ` ) ` ) ;
}
// in a case like `$: ({ a } = b())`, there's already a trailing parenthesis.
return ;
// otherwise, we need to add one
if ( state . str . original [ /** @type {number} */ ( node . body . start ) ] !== '(' ) {
state . str . appendLeft ( end , ` ) ` ) ;
}
}
return ;
for ( const binding of bindings ) {
} else {
if ( binding . reassigned && ( ids . includes ( binding . node ) || expression _ids . length === 0 ) ) {
for ( const binding of reassigned _bindings ) {
if ( binding && ( ids . includes ( binding . node ) || expression _ids . length === 0 ) ) {
check _rune _binding ( 'state' ) ;
check _rune _binding ( 'state' ) ;
const init =
const init =
binding . kind === 'state'
binding . kind === 'state'
? ' = $state()'
? ' = $state()'
: expression _ids . length === 0
: expression _ids . length === 0
? ` = $ state( ${ state . str . original . substring ( /** @type {number} */ ( node. body . expression . right. start ) , node . body . expression . right . end ) } ) `
? ` = $ state( ${ state . str . original . substring ( /** @type {number} */ ( right. start ) , right . end ) } ) `
: '' ;
: '' ;
// implicitly-declared variable which we need to make explicit
// implicitly-declared variable which we need to make explicit
state . str . prependLeft (
state . str . prependLeft (
@ -1000,7 +999,8 @@ const instance_script = {
) ;
) ;
}
}
}
}
if ( expression _ids . length === 0 && ! bindings . some ( ( b ) => b ? . kind === 'store_sub' ) ) {
if ( expression _ids . length === 0 && bindings . every ( ( b ) => b . kind !== 'store_sub' ) ) {
state . str . remove ( /** @type {number} */ ( node . start ) , /** @type {number} */ ( node . end ) ) ;
state . str . remove ( /** @type {number} */ ( node . start ) , /** @type {number} */ ( node . end ) ) ;
return ;
return ;
}
}