@@ -622,23 +622,41 @@ const ASTCallbacks = {
622622
623623 // Analyze which outer scope variables are assigned in the loop body
624624 const assignedVars = new Set ( ) ;
625- const analyzeBlock = ( body ) => {
625+ const analyzeBlock = ( body , parentLocalVars = new Set ( ) ) => {
626626 if ( body . type !== 'BlockStatement' ) return ;
627627
628+ // First pass: collect variable declarations within this block
629+ const localVars = new Set ( [ ...parentLocalVars ] ) ;
630+ for ( const stmt of body . body ) {
631+ if ( stmt . type === 'VariableDeclaration' ) {
632+ for ( const decl of stmt . declarations ) {
633+ if ( decl . id . type === 'Identifier' ) {
634+ localVars . add ( decl . id . name ) ;
635+ }
636+ }
637+ }
638+ }
639+
640+ // Second pass: find assignments to non-local variables
628641 for ( const stmt of body . body ) {
629642 if ( stmt . type === 'ExpressionStatement' &&
630643 stmt . expression . type === 'AssignmentExpression' ) {
631644 const left = stmt . expression . left ;
632645 if ( left . type === 'Identifier' ) {
633- assignedVars . add ( left . name ) ;
646+ // Direct variable assignment: x = value
647+ if ( ! localVars . has ( left . name ) ) {
648+ assignedVars . add ( left . name ) ;
649+ }
634650 } else if ( left . type === 'MemberExpression' &&
635651 left . object . type === 'Identifier' ) {
636652 // Property assignment: obj.prop = value (includes swizzles)
637- assignedVars . add ( left . object . name ) ;
653+ if ( ! localVars . has ( left . object . name ) ) {
654+ assignedVars . add ( left . object . name ) ;
655+ }
638656 }
639657 } else if ( stmt . type === 'BlockStatement' ) {
640- // Recursively analyze nested block statements
641- analyzeBlock ( stmt ) ;
658+ // Recursively analyze nested block statements, passing down local vars
659+ analyzeBlock ( stmt , localVars ) ;
642660 }
643661 }
644662 } ;
0 commit comments