@@ -3,24 +3,19 @@ import { LuaTarget } from "../../CompilerOptions";
33import * as lua from "../../LuaAST" ;
44import { FunctionVisitor } from "../context" ;
55import { UnsupportedForTarget } from "../utils/errors" ;
6- import { peekScope , performHoisting , popScope , pushScope , ScopeType } from "../utils/scope" ;
6+ import { performHoisting , popScope , pushScope , ScopeType } from "../utils/scope" ;
77
88export const transformSwitchStatement : FunctionVisitor < ts . SwitchStatement > = ( statement , context ) => {
99 if ( context . luaTarget === LuaTarget . Lua51 ) {
1010 throw UnsupportedForTarget ( "Switch statements" , LuaTarget . Lua51 , statement ) ;
1111 }
1212
13- pushScope ( context , ScopeType . Switch ) ;
14-
13+ const scope = pushScope ( context , ScopeType . Switch ) ;
1514 // Give the switch a unique name to prevent nested switches from acting up.
16- const scope = peekScope ( context ) ;
1715 const switchName = `____switch${ scope . id } ` ;
18-
19- const expression = context . transformExpression ( statement . expression ) ;
2016 const switchVariable = lua . createIdentifier ( switchName ) ;
21- const switchVariableDeclaration = lua . createVariableDeclarationStatement ( switchVariable , expression ) ;
2217
23- let statements : lua . Statement [ ] = [ switchVariableDeclaration ] ;
18+ let statements : lua . Statement [ ] = [ ] ;
2419
2520 const caseClauses = statement . caseBlock . clauses . filter ( ts . isCaseClause ) ;
2621 for ( const [ index , clause ] of caseClauses . entries ( ) ) {
@@ -37,25 +32,21 @@ export const transformSwitchStatement: FunctionVisitor<ts.SwitchStatement> = (st
3732 }
3833
3934 const hasDefaultCase = statement . caseBlock . clauses . some ( ts . isDefaultClause ) ;
40- if ( hasDefaultCase ) {
41- statements . push ( lua . createGotoStatement ( `${ switchName } _case_default` ) ) ;
42- } else {
43- statements . push ( lua . createGotoStatement ( `${ switchName } _end` ) ) ;
44- }
35+ statements . push ( lua . createGotoStatement ( `${ switchName } _${ hasDefaultCase ? "case_default" : "end" } ` ) ) ;
4536
4637 for ( const [ index , clause ] of statement . caseBlock . clauses . entries ( ) ) {
47- const label = ts . isCaseClause ( clause )
48- ? lua . createLabelStatement ( `${ switchName } _case_${ index } ` )
49- : lua . createLabelStatement ( `${ switchName } _case_default` ) ;
50-
51- const body = lua . createDoStatement ( context . transformStatements ( clause . statements ) ) ;
52- statements . push ( label , body ) ;
38+ const labelName = `${ switchName } _case_${ ts . isCaseClause ( clause ) ? index : "default" } ` ;
39+ statements . push ( lua . createLabelStatement ( labelName ) ) ;
40+ statements . push ( lua . createDoStatement ( context . transformStatements ( clause . statements ) ) ) ;
5341 }
5442
5543 statements . push ( lua . createLabelStatement ( `${ switchName } _end` ) ) ;
5644
5745 statements = performHoisting ( context , statements ) ;
5846 popScope ( context ) ;
5947
48+ const expression = context . transformExpression ( statement . expression ) ;
49+ statements . unshift ( lua . createVariableDeclarationStatement ( switchVariable , expression ) ) ;
50+
6051 return statements ;
6152} ;
0 commit comments