@@ -239,36 +239,52 @@ export class LuaTransformer {
239239
240240 const result : ts . Node [ ] = [ ] ;
241241
242- let declarationNameExpression : ts . Expression ;
243242 if ( this . currentNamespace ) {
243+ // outerNS.innerNS = outerNS.innerNS or {};
244+ // local innerNS = outerNS.innerNS
244245 const declarationNameExpression =
245246 ts . createPropertyAccess ( this . currentNamespace . name , node . name as ts . Identifier ) ;
246247 const declarationAssignment = ts . createAssignment (
247248 declarationNameExpression , ts . createLogicalOr ( declarationNameExpression , ts . createObjectLiteral ( ) ) ) ;
248249
249250 result . push ( declarationAssignment ) ;
250- // outerNS.innerNS = outerNS.innerNS or {};
251- // local innerNS = outerNS.innerNS
251+
252+ const localDeclaration =
253+ transformHelper . createLuaVariableStatement ( node . name as ts . Identifier , declarationNameExpression ) ;
254+
255+ result . push ( localDeclaration ) ;
252256 } else if ( this . isModule && ( ts . getCombinedModifierFlags ( node ) & ts . ModifierFlags . Export ) ) {
253- declarationNameExpression =
254- ts . createPropertyAccess ( ts . createIdentifier ( "export" ) , node . name as ts . Identifier ) ;
255257 // exports.NS = exports.NS or {}
256258 // local NS = exports.NS
259+ const declarationNameExpression =
260+ ts . createPropertyAccess ( ts . createIdentifier ( "exports" ) , node . name as ts . Identifier ) ;
261+ const declarationAssignment = ts . createAssignment (
262+ declarationNameExpression , ts . createLogicalOr ( declarationNameExpression , ts . createObjectLiteral ( ) ) ) ;
263+
264+ result . push ( declarationAssignment ) ;
265+
266+ const localDeclaration =
267+ transformHelper . createLuaVariableStatement ( node . name as ts . Identifier , declarationNameExpression ) ;
268+
269+ result . push ( localDeclaration ) ;
257270 } else {
258- declarationNameExpression = node . name ;
259- // NS = NS or {}
260- // local NS = NS
271+ // local NS = NS or {}
272+ const declarationNameExpression = node . name ;
273+ const declarationAssignment = ts . createAssignment (
274+ declarationNameExpression , ts . createLogicalOr ( declarationNameExpression , ts . createObjectLiteral ( ) ) ) ;
275+
276+ result . push ( declarationAssignment ) ;
261277 }
262278
263279 // Set current namespace for nested NS
264280 // Keep previous currentNS to reset after block transpilation
265281 const previousNamespace = this . currentNamespace ;
266282 this . currentNamespace = node ;
267283
268- // Transform moduleblock to block and transform it
269- if ( ts . isModuleBlock ( node . body ) ) {
284+ // Transform moduleblock to block and visit it
285+ if ( node . body && ts . isModuleBlock ( node . body ) ) {
270286 const bodyBlock = this . visitBlock ( ts . createBlock ( node . body . statements ) ) as ts . Block ;
271- result . push ( bodyBlock ) ;
287+ // result.push(bodyBlock);
272288 }
273289
274290 this . currentNamespace = previousNamespace ;
@@ -430,8 +446,8 @@ export class LuaTransformer {
430446 public visitComputedPropertyName ( node : ts . ComputedPropertyName ) : ts . VisitResult < ts . ComputedPropertyName > {
431447 return node ;
432448 }
433- public visitBlock ( node : ts . Block ) : ts . VisitResult < ts . Block > {
434- return node ;
449+ public visitBlock ( node : ts . Block ) : ts . Block {
450+ return ts . updateBlock ( node , node . statements . map ( s => this . visitor ( s ) ) as ts . Statement [ ] ) ;
435451 }
436452 public visitModuleBlock ( node : ts . ModuleBlock ) : ts . VisitResult < ts . ModuleBlock > {
437453 return node ;
0 commit comments