@@ -27,7 +27,7 @@ var LuaTranspiler = /** @class */ (function () {
2727 function LuaTranspiler ( checker ) {
2828 this . indent = "" ;
2929 this . checker = checker ;
30- this . switchCounter = 0 ;
30+ this . genVarCounter = 0 ;
3131 this . transpilingSwitch = false ;
3232 }
3333 // Transpile a source file
@@ -153,7 +153,7 @@ var LuaTranspiler = /** @class */ (function () {
153153 } ;
154154 LuaTranspiler . prototype . transpileBreak = function ( ) {
155155 if ( this . transpilingSwitch ) {
156- return this . indent + ( "goto switchDone" + this . switchCounter + "\n" ) ;
156+ return this . indent + ( "goto switchDone" + this . genVarCounter + "\n" ) ;
157157 }
158158 else {
159159 return this . indent + "break\n" ;
@@ -257,23 +257,23 @@ var LuaTranspiler = /** @class */ (function () {
257257 }
258258 _this . pushIndent ( ) ;
259259 // Labels for fallthrough
260- result += _this . indent + ( "::switchCase" + ( _this . switchCounter + index ) + "::\n" ) ;
260+ result += _this . indent + ( "::switchCase" + ( _this . genVarCounter + index ) + "::\n" ) ;
261261 _this . transpilingSwitch = true ;
262262 clause . statements . forEach ( function ( statement ) {
263263 result += _this . transpileNode ( statement ) ;
264264 } ) ;
265265 _this . transpilingSwitch = false ;
266266 // If this goto is reached, fall through to the next case
267267 if ( index < clauses . length - 1 ) {
268- result += _this . indent + ( "goto switchCase" + ( _this . switchCounter + index + 1 ) + "\n" ) ;
268+ result += _this . indent + ( "goto switchCase" + ( _this . genVarCounter + index + 1 ) + "\n" ) ;
269269 }
270270 _this . popIndent ( ) ;
271271 } ) ;
272272 result += this . indent + "end\n" ;
273- result += this . indent + ( "::switchDone" + this . switchCounter + "::\n" ) ;
273+ result += this . indent + ( "::switchDone" + this . genVarCounter + "::\n" ) ;
274274 result += this . indent + "--------Switch statement end--------\n" ;
275275 //Increment counter for next switch statement
276- this . switchCounter += clauses . length ;
276+ this . genVarCounter += clauses . length ;
277277 return result ;
278278 } ;
279279 LuaTranspiler . prototype . transpileReturn = function ( node ) {
@@ -497,6 +497,8 @@ var LuaTranspiler = /** @class */ (function () {
497497 return "TS_some(" + caller + ", " + params + ")" ;
498498 case "every" :
499499 return "TS_every(" + caller + ", " + params + ")" ;
500+ case "slice" :
501+ return "TS_slice(" + caller + ", " + params + ")" ;
500502 default :
501503 throw new TranspileError ( "Unsupported array function: " + expression . name . escapedText , node ) ;
502504 }
@@ -573,14 +575,36 @@ var LuaTranspiler = /** @class */ (function () {
573575 return result ;
574576 } ;
575577 LuaTranspiler . prototype . transpileVariableDeclaration = function ( node ) {
576- // Find variable identifier
577- var identifier = node . name ;
578- if ( node . initializer ) {
578+ var _this = this ;
579+ if ( ts . isIdentifier ( node . name ) ) {
580+ // Find variable identifier
581+ var identifier = node . name ;
582+ if ( node . initializer ) {
583+ var value = this . transpileExpression ( node . initializer ) ;
584+ return "local " + identifier . escapedText + " = " + value ;
585+ }
586+ else {
587+ return "local " + identifier . escapedText + " = nil" ;
588+ }
589+ }
590+ else if ( ts . isArrayBindingPattern ( node . name ) ) {
591+ // Destructuring type
579592 var value = this . transpileExpression ( node . initializer ) ;
580- return "local " + identifier . escapedText + " = " + value ;
593+ var parentName_1 = "__destr" + this . genVarCounter ;
594+ this . genVarCounter ++ ;
595+ var result_1 = "local " + parentName_1 + " = " + value + "\n" ;
596+ node . name . elements . forEach ( function ( elem , index ) {
597+ if ( ! elem . dotDotDotToken ) {
598+ result_1 += _this . indent + ( "local " + elem . name . escapedText + " = " + parentName_1 + "[" + ( index + 1 ) + "]\n" ) ;
599+ }
600+ else {
601+ result_1 += _this . indent + ( "local " + elem . name . escapedText + " = TS_slice(" + parentName_1 + ", " + index + ")\n" ) ;
602+ }
603+ } ) ;
604+ return result_1 ;
581605 }
582606 else {
583- return "local " + identifier . escapedText + " = nil" ;
607+ throw new TranspileError ( "Unsupported variable declaration type " + TSHelper_1 . TSHelper . enumName ( node . name . kind , ts . SyntaxKind ) , node ) ;
584608 }
585609 } ;
586610 LuaTranspiler . prototype . transpileFunctionDeclaration = function ( node ) {
0 commit comments