Skip to content

Commit 36c2376

Browse files
committed
Added basic namespace support
1 parent 0696b42 commit 36c2376

File tree

9 files changed

+86
-11
lines changed

9 files changed

+86
-11
lines changed

dist/TSHelper.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ var TSHelper = /** @class */ (function () {
6161
&& ((type.symbol.flags & ts.SymbolFlags.Class) != 0)
6262
&& this.hasCustomDecorator(type, "!Extension");
6363
};
64+
TSHelper.isPhantom = function (type) {
65+
return type.symbol
66+
&& ((type.symbol.flags & ts.SymbolFlags.Namespace) != 0)
67+
&& this.hasCustomDecorator(type, "!Phantom");
68+
};
6469
TSHelper.hasCustomDecorator = function (type, decorator) {
6570
if (type.symbol) {
6671
var comment = type.symbol.getDocumentationComment();

dist/Transpiler.js

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ var LuaTranspiler = /** @class */ (function () {
4747
LuaTranspiler.prototype.popIndent = function () {
4848
this.indent = this.indent.slice(4);
4949
};
50+
LuaTranspiler.prototype.definitionName = function (name) {
51+
return this.namespace.concat(name).join(".");
52+
};
5053
// Transpile a block
5154
LuaTranspiler.prototype.transpileBlock = function (node) {
5255
var _this = this;
@@ -73,6 +76,10 @@ var LuaTranspiler = /** @class */ (function () {
7376
return this.transpileImport(node);
7477
case ts.SyntaxKind.ClassDeclaration:
7578
return this.transpileClass(node);
79+
case ts.SyntaxKind.ModuleDeclaration:
80+
return this.transpileNamespace(node);
81+
case ts.SyntaxKind.ModuleBlock:
82+
return this.transpileBlock(node);
7683
case ts.SyntaxKind.EnumDeclaration:
7784
return this.transpileEnum(node);
7885
case ts.SyntaxKind.FunctionDeclaration:
@@ -128,14 +135,26 @@ var LuaTranspiler = /** @class */ (function () {
128135
throw new TranspileError("Unsupported import type.", node);
129136
}
130137
};
138+
LuaTranspiler.prototype.transpileNamespace = function (node) {
139+
// If phantom namespace just transpile the body as normal
140+
if (TSHelper_1.TSHelper.isPhantom(this.checker.getTypeAtLocation(node)))
141+
return this.transpileNode(node.body);
142+
var defName = this.definitionName(node.name.text);
143+
var result = this.indent + (defName + " = {}\n");
144+
this.namespace.push(node.name.text);
145+
result += this.transpileNode(node.body);
146+
this.namespace.pop();
147+
return result;
148+
};
131149
LuaTranspiler.prototype.transpileEnum = function (node) {
132150
var _this = this;
133151
var val = 0;
134152
var result = "";
135153
var type = this.checker.getTypeAtLocation(node);
136154
var membersOnly = TSHelper_1.TSHelper.isCompileMembersOnlyEnum(type);
137155
if (!membersOnly) {
138-
result += this.indent + (node.name.escapedText + "={}\n");
156+
var defName = this.definitionName(node.name.escapedText);
157+
result += this.indent + (defName + "={}\n");
139158
}
140159
node.members.forEach(function (member) {
141160
if (member.initializer) {
@@ -146,12 +165,13 @@ var LuaTranspiler = /** @class */ (function () {
146165
throw new TranspileError("Only numeric initializers allowed for enums.", node);
147166
}
148167
}
149-
var name = member.name.escapedText;
150168
if (membersOnly) {
151-
result += _this.indent + (name + "=" + val + "\n");
169+
var defName = _this.definitionName(name);
170+
result += _this.indent + (defName + "=" + val + "\n");
152171
}
153172
else {
154-
result += _this.indent + (node.name.escapedText + "." + name + "=" + val + "\n");
173+
var defName = _this.definitionName(node.name.escapedText + "." + member.name.escapedText);
174+
result += _this.indent + (defName + "=" + val + "\n");
155175
}
156176
val++;
157177
});
@@ -646,7 +666,7 @@ var LuaTranspiler = /** @class */ (function () {
646666
paramNames.push(param.name.escapedText);
647667
});
648668
// Build function header
649-
result += this.indent + ("function " + methodName + "(" + paramNames.join(",") + ")\n");
669+
result += this.indent + ("function " + this.definitionName(methodName) + "(" + paramNames.join(",") + ")\n");
650670
this.pushIndent();
651671
result += this.transpileBlock(body);
652672
this.popIndent();

src/TSHelper.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ export class TSHelper {
6969
&& this.hasCustomDecorator(type, "!Extension");
7070
}
7171

72+
static isPhantom(type: ts.Type): boolean {
73+
return type.symbol
74+
&& ((type.symbol.flags & ts.SymbolFlags.Namespace) != 0)
75+
&& this.hasCustomDecorator(type, "!Phantom");
76+
}
77+
7278
static hasCustomDecorator(type: ts.Type, decorator: string): boolean {
7379
if (type.symbol) {
7480
var comment = type.symbol.getDocumentationComment();

src/Transpiler.ts

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@ export class LuaTranspiler {
2727
checker: ts.TypeChecker;
2828
genVarCounter: number;
2929
transpilingSwitch: boolean;
30+
namespace: string[];
3031

3132
constructor(checker: ts.TypeChecker) {
3233
this.indent = "";
3334
this.checker = checker;
3435
this.genVarCounter = 0;
3536
this.transpilingSwitch = false;
37+
this.namespace = [];
3638
}
3739

3840
pushIndent(): void {
@@ -43,6 +45,10 @@ export class LuaTranspiler {
4345
this.indent = this.indent.slice(4);
4446
}
4547

48+
definitionName(name: string|ts.__String): string {
49+
return this.namespace.concat(<string>name).join(".");
50+
}
51+
4652
// Transpile a block
4753
transpileBlock(node: ts.Node): string {
4854
let result = "";
@@ -70,6 +76,10 @@ export class LuaTranspiler {
7076
return this.transpileImport(<ts.ImportDeclaration>node);
7177
case ts.SyntaxKind.ClassDeclaration:
7278
return this.transpileClass(<ts.ClassDeclaration>node);
79+
case ts.SyntaxKind.ModuleDeclaration:
80+
return this.transpileNamespace(<ts.ModuleDeclaration>node);
81+
case ts.SyntaxKind.ModuleBlock:
82+
return this.transpileBlock(<ts.Block>node);
7383
case ts.SyntaxKind.EnumDeclaration:
7484
return this.transpileEnum(<ts.EnumDeclaration>node);
7585
case ts.SyntaxKind.FunctionDeclaration:
@@ -125,6 +135,18 @@ export class LuaTranspiler {
125135
}
126136
}
127137

138+
transpileNamespace(node: ts.ModuleDeclaration): string {
139+
// If phantom namespace just transpile the body as normal
140+
if (tsEx.isPhantom(this.checker.getTypeAtLocation(node))) return this.transpileNode(node.body);
141+
142+
const defName = this.definitionName(node.name.text);
143+
let result = this.indent + `${defName} = {}\n`;
144+
this.namespace.push(node.name.text);
145+
result += this.transpileNode(node.body);
146+
this.namespace.pop();
147+
return result;
148+
}
149+
128150
transpileEnum(node: ts.EnumDeclaration): string {
129151
let val = 0;
130152
let result = "";
@@ -133,7 +155,8 @@ export class LuaTranspiler {
133155
const membersOnly = tsEx.isCompileMembersOnlyEnum(type);
134156

135157
if (!membersOnly) {
136-
result += this.indent + `${node.name.escapedText}={}\n`;
158+
const defName = this.definitionName(node.name.escapedText)
159+
result += this.indent + `${defName}={}\n`;
137160
}
138161

139162
node.members.forEach(member => {
@@ -145,11 +168,12 @@ export class LuaTranspiler {
145168
}
146169
}
147170

148-
const name = (<ts.Identifier>member.name).escapedText;
149171
if (membersOnly) {
150-
result += this.indent + `${name}=${val}\n`;
172+
const defName = this.definitionName(name);
173+
result += this.indent + `${defName}=${val}\n`;
151174
} else {
152-
result += this.indent + `${node.name.escapedText}.${name}=${val}\n`;
175+
const defName = this.definitionName(`${node.name.escapedText}.${(<ts.Identifier>member.name).escapedText}`);
176+
result += this.indent + `${defName}=${val}\n`;
153177
}
154178

155179
val++;
@@ -699,7 +723,7 @@ export class LuaTranspiler {
699723
});
700724

701725
// Build function header
702-
result += this.indent + `function ${methodName}(${paramNames.join(",")})\n`;
726+
result += this.indent + `function ${this.definitionName(methodName)}(${paramNames.join(",")})\n`;
703727

704728
this.pushIndent();
705729
result += this.transpileBlock(body);

test/src/test.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
--=======================================================================================
2+
-- Generated by TypescriptToLua transpiler https://github.com/Perryvw/TypescriptToLua
3+
-- Date: Mon Jan 29 2018
4+
--=======================================================================================
15
require("./test2"){$imports.name.escapedText} = require("./test2")local a = TestClass.new(true,0)
26
GameState = GameState or {}
37
GameState.__index = GameState

test/src/test/test3.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
--=======================================================================================
2+
-- Generated by TypescriptToLua transpiler https://github.com/Perryvw/TypescriptToLua
3+
-- Date: Mon Jan 29 2018
4+
--=======================================================================================
15
local a = ""
26
local b = {1,2,3}
37
local c = {d=a,e=b}

test/src/test2.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
--=======================================================================================
2+
-- Generated by TypescriptToLua transpiler https://github.com/Perryvw/TypescriptToLua
3+
-- Date: Mon Jan 29 2018
4+
--=======================================================================================
15
myGlobalVar={a=3,b="hello"}
26
local globalString = "3"
37
local input = {1,2}

test/src/test3.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/** !Phantom */
2+
namespace testNS {
3+
function myFunction(): string {
4+
return "hello";
5+
}
6+
}
7+
8+
function test() {}

test/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
"noImplicitThis" : true,
55
"alwaysStrict" : true,
66
"strictNullChecks" : true,
7-
"target": "lua"
7+
"target": "lua",
88
}
99
}

0 commit comments

Comments
 (0)