@@ -12,6 +12,9 @@ export class LuaTransformer {
1212 private options : CompilerOptions ;
1313 private context : ts . TransformationContext ;
1414 private sourceFile : ts . SourceFile ;
15+ private isModule : boolean ;
16+
17+ private currentNamespace : ts . ModuleDeclaration ;
1518
1619 constructor ( checker : ts . TypeChecker , options : CompilerOptions ) {
1720 this . checker = checker ;
@@ -20,6 +23,7 @@ export class LuaTransformer {
2023
2124 public transform ( node : ts . SourceFile ) : ts . SourceFile {
2225 this . sourceFile = node ;
26+ this . isModule = tsHelper . isFileModule ( node ) ;
2327 return ts
2428 . transform ( node ,
2529 [ ( ctx : ts . TransformationContext ) => {
@@ -232,8 +236,44 @@ export class LuaTransformer {
232236 if ( decorators . has ( DecoratorKind . Phantom ) && node . body ) {
233237 return node . body ;
234238 }
235- // TODO actual transpilation
236- return node ;
239+
240+ const result : ts . Node [ ] = [ ] ;
241+
242+ let declarationNameExpression : ts . Expression ;
243+ if ( this . currentNamespace ) {
244+ const declarationNameExpression =
245+ ts . createPropertyAccess ( this . currentNamespace . name , node . name as ts . Identifier ) ;
246+ const declarationAssignment = ts . createAssignment (
247+ declarationNameExpression , ts . createLogicalOr ( declarationNameExpression , ts . createObjectLiteral ( ) ) ) ;
248+
249+ result . push ( declarationAssignment ) ;
250+ // outerNS.innerNS = outerNS.innerNS or {};
251+ // local innerNS = outerNS.innerNS
252+ } else if ( this . isModule && ( ts . getCombinedModifierFlags ( node ) & ts . ModifierFlags . Export ) ) {
253+ declarationNameExpression =
254+ ts . createPropertyAccess ( ts . createIdentifier ( "export" ) , node . name as ts . Identifier ) ;
255+ // exports.NS = exports.NS or {}
256+ // local NS = exports.NS
257+ } else {
258+ declarationNameExpression = node . name ;
259+ // NS = NS or {}
260+ // local NS = NS
261+ }
262+
263+ // Set current namespace for nested NS
264+ // Keep previous currentNS to reset after block transpilation
265+ const previousNamespace = this . currentNamespace ;
266+ this . currentNamespace = node ;
267+
268+ // Transform moduleblock to block and transform it
269+ if ( ts . isModuleBlock ( node . body ) ) {
270+ const bodyBlock = this . visitBlock ( ts . createBlock ( node . body . statements ) ) as ts . Block ;
271+ result . push ( bodyBlock ) ;
272+ }
273+
274+ this . currentNamespace = previousNamespace ;
275+
276+ return result ;
237277 }
238278 public visitEnumDeclaration ( node : ts . EnumDeclaration ) : ts . VisitResult < ts . EnumDeclaration > {
239279 return node ;
@@ -424,5 +464,4 @@ export class LuaTransformer {
424464 private pathToLuaRequirePath ( filePath : string ) : string {
425465 return filePath . replace ( new RegExp ( "\\\\|\/" , "g" ) , "." ) ;
426466 }
427-
428467}
0 commit comments