Skip to content

Commit 7c04fa7

Browse files
committed
Addded NS Transformer
1 parent e124f1d commit 7c04fa7

File tree

3 files changed

+45
-40
lines changed

3 files changed

+45
-40
lines changed

src/TransformHelper.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@ import * as ts from "typescript";
22

33
export class TransformHelper {
44
// Helper to create simple lua variable statement;
5-
public static createLuaVariableStatement(
6-
identifier: ts.Identifier,
7-
expression: ts.Expression,
8-
typeNode?: ts.TypeNode
9-
): ts.VariableStatement {
5+
public static createLuaVariableStatement(identifier: ts.Identifier,
6+
expression?: ts.Expression,
7+
typeNode?: ts.TypeNode): ts.VariableStatement {
108
const declaration = ts.createVariableDeclaration(identifier, typeNode, expression);
119
const statement = ts.createVariableStatement([], ts.createVariableDeclarationList([declaration]));
1210
return statement;

src/Transformer.ts

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/Transpiler.ts

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,6 @@ export abstract class LuaTranspiler {
267267
switch (node.kind) {
268268
case ts.SyntaxKind.ClassDeclaration:
269269
return this.transpileClass(node as ts.ClassDeclaration);
270-
case ts.SyntaxKind.ModuleDeclaration:
271-
return this.transpileNamespace(node as ts.ModuleDeclaration);
272270
case ts.SyntaxKind.ModuleBlock:
273271
return this.transpileBlock(node as ts.Block);
274272
case ts.SyntaxKind.EnumDeclaration:
@@ -319,36 +317,6 @@ export abstract class LuaTranspiler {
319317
return `__TS__${func}(${params.join(", ")})`;
320318
}
321319

322-
public transpileNamespace(node: ts.ModuleDeclaration): string {
323-
const decorators = tsHelper.getCustomDecorators(this.checker.getTypeAtLocation(node), this.checker);
324-
// If phantom namespace just transpile the body as normal
325-
if (decorators.has(DecoratorKind.Phantom) && node.body) {
326-
return this.transpileNode(node.body);
327-
}
328-
329-
const defName = this.definitionName(node.name.text);
330-
// Initialize to pre-existing export if one exists
331-
const prefix = (this.namespace.length === 0 && this.isModule &&
332-
(ts.getCombinedModifierFlags(node) & ts.ModifierFlags.Export)) ? `exports.${node.name.text} or ` : "";
333-
let result =
334-
this.indent +
335-
this.accessPrefix(node) +
336-
`${node.name.text} = ${prefix}${node.name.text} or {}\n`;
337-
338-
this.pushExport(node.name.text, node);
339-
// Create closure
340-
result += this.indent + "do\n";
341-
this.pushIndent();
342-
this.namespace.push(node.name.text);
343-
if (node.body) {
344-
result += this.transpileNode(node.body);
345-
}
346-
this.namespace.pop();
347-
this.popIndent();
348-
result += this.indent + "end\n";
349-
return result;
350-
}
351-
352320
public transpileEnum(node: ts.EnumDeclaration): string {
353321
const type = this.checker.getTypeAtLocation(node);
354322

0 commit comments

Comments
 (0)