@@ -57,6 +57,17 @@ interface ExportInfo {
5757 dummy : boolean ;
5858}
5959
60+ export enum ScopeType {
61+ Function ,
62+ Switch ,
63+ Loop ,
64+ }
65+
66+ interface SpecialScope {
67+ type : ScopeType ;
68+ id : number ;
69+ }
70+
6071export abstract class LuaTranspiler {
6172 // Lua key words for this Lua target
6273 // https://www.lua.org/manual/5.0/manual.html#2.1
@@ -70,13 +81,12 @@ export abstract class LuaTranspiler {
7081 public checker : ts . TypeChecker ;
7182 public options : CompilerOptions ;
7283 public genVarCounter : number ;
73- public transpilingSwitch : number ;
7484 public namespace : string [ ] ;
7585 public importCount : number ;
7686 public isModule : boolean ;
7787 public isStrict : boolean ;
7888 public sourceFile : ts . SourceFile ;
79- public loopStack : number [ ] ;
89+ public scopeStack : SpecialScope [ ] ;
8090 public classStack : string [ ] ;
8191 public exportStack : ExportInfo [ ] [ ] ;
8292
@@ -89,15 +99,14 @@ export abstract class LuaTranspiler {
8999 this . checker = checker ;
90100 this . options = options ;
91101 this . genVarCounter = 0 ;
92- this . transpilingSwitch = 0 ;
93102 this . namespace = [ ] ;
94103 this . importCount = 0 ;
95104 this . sourceFile = sourceFile ;
96105 this . isModule = tsHelper . isFileModule ( sourceFile ) ;
97106 this . isStrict = options . alwaysStrict
98107 || ( options . strict && options . alwaysStrict !== false )
99108 || ( this . isModule && options . target && options . target >= ts . ScriptTarget . ES2015 ) ;
100- this . loopStack = [ ] ;
109+ this . scopeStack = [ ] ;
101110 this . classStack = [ ] ;
102111 this . exportStack = [ ] ;
103112 this . luaLibFeatureSet = new Set < LuaLibFeature > ( ) ;
@@ -128,6 +137,19 @@ export abstract class LuaTranspiler {
128137 this . exportStack [ this . exportStack . length - 1 ] . push ( { name : nameIn , node : nodeIn , dummy : dummyIn } ) ;
129138 }
130139
140+ public peekSpecialScope ( ) : SpecialScope {
141+ return this . scopeStack [ this . scopeStack . length - 1 ] ;
142+ }
143+
144+ public pushSpecialScope ( scopeType : ScopeType ) : void {
145+ this . scopeStack . push ( { type : scopeType , id : this . genVarCounter } ) ;
146+ this . genVarCounter ++ ;
147+ }
148+
149+ public popSpecialScope ( ) : SpecialScope {
150+ return this . scopeStack . pop ( ) ;
151+ }
152+
131153 public makeExport ( name : string , node : ts . Node , dummy ?: boolean ) : string {
132154 let result : string = "" ;
133155 if ( node &&
@@ -327,7 +349,7 @@ export abstract class LuaTranspiler {
327349 case ts . SyntaxKind . SwitchStatement :
328350 return this . transpileSwitch ( node as ts . SwitchStatement ) ;
329351 case ts . SyntaxKind . BreakStatement :
330- return this . transpileBreak ( ) ;
352+ return this . transpileBreak ( node as ts . BreakStatement ) ;
331353 case ts . SyntaxKind . TryStatement :
332354 return this . transpileTry ( node as ts . TryStatement ) ;
333355 case ts . SyntaxKind . ThrowStatement :
@@ -459,12 +481,8 @@ export abstract class LuaTranspiler {
459481 return result ;
460482 }
461483
462- public transpileBreak ( ) : string {
463- if ( this . transpilingSwitch > 0 ) {
464- return "" ;
465- } else {
466- return this . indent + "break\n" ;
467- }
484+ public transpileBreak ( node : ts . BreakStatement ) : string {
485+ return this . indent + "break\n" ;
468486 }
469487
470488 public transpileContinue ( node : ts . ContinueStatement ) : string {
@@ -506,8 +524,6 @@ export abstract class LuaTranspiler {
506524 | ts . ForOfStatement
507525 | ts . ForInStatement
508526 ) : string {
509- this . loopStack . push ( this . genVarCounter ) ;
510- this . genVarCounter ++ ;
511527 let result = this . indent + "do\n" ;
512528 this . pushIndent ( ) ;
513529 result += this . transpileStatement ( node . statement ) ;
@@ -1880,6 +1896,7 @@ export abstract class LuaTranspiler {
18801896 body : ts . Block ,
18811897 spreadIdentifier : string = ""
18821898 ) : string {
1899+ this . pushSpecialScope ( ScopeType . Function ) ;
18831900 let result = "" ;
18841901
18851902 // Add default parameters
@@ -1892,6 +1909,7 @@ export abstract class LuaTranspiler {
18921909 }
18931910
18941911 result += this . transpileBlock ( body ) ;
1912+ this . popSpecialScope ( ) ;
18951913
18961914 return result ;
18971915 }
0 commit comments