@@ -2284,6 +2284,9 @@ export class LuaTransformer {
22842284 const variableDeclarations = this . transformVariableDeclaration ( initializer . declarations [ 0 ] ) ;
22852285 if ( ts . isArrayBindingPattern ( initializer . declarations [ 0 ] . name ) ) {
22862286 expression = this . createUnpackCall ( expression , initializer ) ;
2287+
2288+ } else if ( ts . isObjectBindingPattern ( initializer . declarations [ 0 ] . name ) ) {
2289+ throw TSTLErrors . UnsupportedObjectDestructuringInForOf ( initializer ) ;
22872290 }
22882291
22892292 const variableStatements = this . statementVisitResultToArray ( variableDeclarations ) ;
@@ -2302,6 +2305,10 @@ export class LuaTransformer {
23022305 expression = this . createUnpackCall ( expression , initializer ) ;
23032306 variables = initializer . elements
23042307 . map ( e => this . transformExpression ( e ) ) as tstl . AssignmentLeftHandSideExpression [ ] ;
2308+
2309+ } else if ( ts . isObjectLiteralExpression ( initializer ) ) {
2310+ throw TSTLErrors . UnsupportedObjectDestructuringInForOf ( initializer ) ;
2311+
23052312 } else {
23062313 variables = this . transformExpression ( initializer ) as tstl . AssignmentLeftHandSideExpression ;
23072314 }
@@ -2336,43 +2343,35 @@ export class LuaTransformer {
23362343 }
23372344
23382345 private transformForOfArrayStatement ( statement : ts . ForOfStatement , block : tstl . Block ) : StatementVisitResult {
2339- const arrayExpression = this . transformExpression ( statement . expression ) ;
2340-
2341- // Arrays use numeric for loop (performs better than ipairs)
2342- const indexVariable = tstl . createIdentifier ( "____TS_index" ) ;
2343- if ( ! ts . isIdentifier ( statement . expression ) ) {
2344- // Cache iterable expression if it's not a simple identifier
2345- // local ____TS_array = ${iterable};
2346- // for ____TS_index = 1, #____TS_array do
2347- // local ${initializer} = ____TS_array[____TS_index]
2348- const arrayVariable = tstl . createIdentifier ( "____TS_array" , statement . expression ) ;
2349- const arrayAccess = tstl . createTableIndexExpression ( arrayVariable , indexVariable ) ;
2350- const initializer = this . transformForOfInitializer ( statement . initializer , arrayAccess ) ;
2351- block . statements . splice ( 0 , 0 , initializer ) ;
2352- return [
2353- tstl . createVariableDeclarationStatement ( arrayVariable , arrayExpression ) ,
2354- tstl . createForStatement (
2355- block ,
2356- indexVariable ,
2357- tstl . createNumericLiteral ( 1 ) ,
2358- tstl . createUnaryExpression ( arrayVariable , tstl . SyntaxKind . LengthOperator )
2359- ) ,
2360- ] ;
2346+ let valueVariable : tstl . Identifier ;
2347+ if ( ts . isVariableDeclarationList ( statement . initializer ) ) {
2348+ // Declaration of new variable
2349+ const variables = statement . initializer . declarations [ 0 ] . name ;
2350+ if ( ts . isArrayBindingPattern ( variables ) || ts . isObjectBindingPattern ( variables ) ) {
2351+ valueVariable = tstl . createIdentifier ( "____TS_values" ) ;
2352+ block . statements . unshift ( this . transformForOfInitializer ( statement . initializer , valueVariable ) ) ;
2353+
2354+ } else {
2355+ valueVariable = this . transformIdentifier ( variables ) ;
2356+ }
23612357
23622358 } else {
2363- // Simple identifier version
2364- // for ____TS_index = 1, #${iterable} do
2365- // local ${initializer} = ${iterable}[____TS_index]
2366- const iterableAccess = tstl . createTableIndexExpression ( arrayExpression , indexVariable ) ;
2367- const initializer = this . transformForOfInitializer ( statement . initializer , iterableAccess ) ;
2368- block . statements . splice ( 0 , 0 , initializer ) ;
2369- return tstl . createForStatement (
2370- block ,
2371- indexVariable ,
2372- tstl . createNumericLiteral ( 1 ) ,
2373- tstl . createUnaryExpression ( arrayExpression , tstl . SyntaxKind . LengthOperator )
2374- ) ;
2359+ // Assignment to existing variable
2360+ valueVariable = tstl . createIdentifier ( "____TS_value" ) ;
2361+ block . statements . unshift ( this . transformForOfInitializer ( statement . initializer , valueVariable ) ) ;
23752362 }
2363+
2364+ const ipairsCall = tstl . createCallExpression (
2365+ tstl . createIdentifier ( "ipairs" ) ,
2366+ [ this . transformExpression ( statement . expression ) ]
2367+ ) ;
2368+
2369+ return tstl . createForInStatement (
2370+ block ,
2371+ [ tstl . createAnonymousIdentifier ( ) , valueVariable ] ,
2372+ [ ipairsCall ] ,
2373+ statement
2374+ ) ;
23762375 }
23772376
23782377 private transformForOfLuaIteratorStatement ( statement : ts . ForOfStatement , block : tstl . Block ) : StatementVisitResult {
0 commit comments