11import * as ts from "typescript" ;
22import * as lua from "../../../LuaAST" ;
33import { TransformationContext } from "../../context" ;
4+ import { transformInPrecedingStatementScope } from "../../utils/preceding-statements" ;
45import { performHoisting , popScope , pushScope , ScopeType } from "../../utils/scope" ;
56import { isAssignmentPattern } from "../../utils/typescript" ;
67import { transformAssignment } from "../binary-expression/assignments" ;
@@ -49,14 +50,20 @@ export function transformForInitializer(
4950) : lua . Identifier {
5051 const valueVariable = lua . createIdentifier ( "____value" ) ;
5152
53+ pushScope ( context , ScopeType . LoopInitializer ) ;
54+
5255 if ( ts . isVariableDeclarationList ( initializer ) ) {
5356 // Declaration of new variable
5457
5558 const binding = getVariableDeclarationBinding ( context , initializer ) ;
5659 if ( ts . isArrayBindingPattern ( binding ) || ts . isObjectBindingPattern ( binding ) ) {
57- block . statements . unshift ( ...transformBindingPattern ( context , binding , valueVariable ) ) ;
60+ const [ precedingStatements , bindings ] = transformInPrecedingStatementScope ( context , ( ) =>
61+ transformBindingPattern ( context , binding , valueVariable )
62+ ) ;
63+ block . statements . unshift ( ...precedingStatements , ...bindings ) ;
5864 } else {
5965 // Single variable declared in for loop
66+ popScope ( context ) ;
6067 return transformIdentifier ( context , binding ) ;
6168 }
6269 } else {
@@ -69,6 +76,7 @@ export function transformForInitializer(
6976 ) ;
7077 }
7178
79+ popScope ( context ) ;
7280 return valueVariable ;
7381}
7482
0 commit comments