@@ -276,6 +276,8 @@ export function analyze_component(root, source, options) {
276276 /** @type {Template } */
277277 const template = { ast : root . fragment , scope, scopes } ;
278278
279+ let synthetic_stores_legacy_check = [ ] ;
280+
279281 // create synthetic bindings for store subscriptions
280282 for ( const [ name , references ] of module . scope . references ) {
281283 if ( name [ 0 ] !== '$' || RESERVED . includes ( name ) ) continue ;
@@ -351,16 +353,21 @@ export function analyze_component(root, source, options) {
351353 }
352354 }
353355
354- // if we are creating a synthetic binding for a let declaration we should also declare
355- // the declaration as state in case it's reassigned
356- if (
357- declaration !== null &&
358- declaration . kind === 'normal' &&
359- declaration . declaration_kind === 'let' &&
360- declaration . reassigned
361- ) {
362- declaration . kind = 'state' ;
363- }
356+ // we push to the array because at this moment in time we can't be sure if we are in legacy
357+ // mode yet because we are still changing the module scope
358+ synthetic_stores_legacy_check . push ( ( ) => {
359+ // if we are creating a synthetic binding for a let declaration we should also declare
360+ // the declaration as state in case it's reassigned and we are not in runes mode (the function will
361+ // not be called if we are not in runes mode, that's why there's no !runes check here)
362+ if (
363+ declaration !== null &&
364+ declaration . kind === 'normal' &&
365+ declaration . declaration_kind === 'let' &&
366+ declaration . reassigned
367+ ) {
368+ declaration . kind = 'state' ;
369+ }
370+ } ) ;
364371
365372 const binding = instance . scope . declare ( b . id ( name ) , 'store_sub' , 'synthetic' ) ;
366373 binding . references = references ;
@@ -373,6 +380,12 @@ export function analyze_component(root, source, options) {
373380
374381 const runes = options . runes ?? Array . from ( module . scope . references . keys ( ) ) . some ( is_rune ) ;
375382
383+ if ( ! runes ) {
384+ for ( let check of synthetic_stores_legacy_check ) {
385+ check ( ) ;
386+ }
387+ }
388+
376389 if ( runes && root . module ) {
377390 const context = root . module . attributes . find ( ( attribute ) => attribute . name === 'context' ) ;
378391 if ( context ) {
0 commit comments