Skip to content

Commit 2cd0859

Browse files
committed
Fixed namespace translation test and almsot every other translation tes
1 parent 101d5ff commit 2cd0859

26 files changed

+104
-86
lines changed

src/LuaPrinter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ export class LuaPrinter {
308308
paramterArr.push(this.printDotsLiteral(expression.dots));
309309
}
310310

311-
let result = `function (${paramterArr.join(", ")})\n`;
311+
let result = `function(${paramterArr.join(", ")})\n`;
312312
this.pushIndent();
313313
result += this.printBlock(expression.body);
314314
this.popIndent();

src/LuaTransformer.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,20 @@ export class LuaTransformer {
8080
this.isModule = tsHelper.isFileModule(node);
8181

8282
const statements = this.transformStatements(node.statements);
83+
if (this.isModule) {
84+
statements.unshift(
85+
tstl.createVariableDeclarationStatement(
86+
tstl.createIdentifier("exports"),
87+
tstl.createBinaryExpression(
88+
tstl.createIdentifier("exports"),
89+
tstl.createTableExpression(),
90+
tstl.SyntaxKind.OrOperator
91+
)));
92+
statements.push(
93+
tstl.createReturnStatement(
94+
[tstl.createIdentifier("exports")]
95+
));
96+
}
8397

8498
return [tstl.createBlock(statements, undefined, node), this.luaLibFeatureSet];
8599
}
@@ -738,11 +752,11 @@ export class LuaTransformer {
738752
const namespaceDeclaration = tstl.createAssignmentStatement(
739753
tstl.createTableIndexExpression(
740754
this.transformIdentifier(this.currentNamespace.name as ts.Identifier),
741-
this.transformIdentifier(statement.name as ts.Identifier)),
755+
tstl.createStringLiteral(this.transformIdentifier(statement.name as ts.Identifier).text)),
742756
tstl.createBinaryExpression(
743757
tstl.createTableIndexExpression(
744758
this.transformIdentifier(this.currentNamespace.name as ts.Identifier),
745-
this.transformIdentifier(statement.name as ts.Identifier)),
759+
tstl.createStringLiteral(this.transformIdentifier(statement.name as ts.Identifier).text)),
746760
tstl.createTableExpression(),
747761
tstl.SyntaxKind.OrOperator));
748762

@@ -753,7 +767,7 @@ export class LuaTransformer {
753767
this.transformIdentifier(statement.name as ts.Identifier),
754768
tstl.createTableIndexExpression(
755769
this.transformIdentifier(this.currentNamespace.name as ts.Identifier),
756-
this.transformIdentifier(statement.name as ts.Identifier)));
770+
tstl.createStringLiteral(this.transformIdentifier(statement.name as ts.Identifier).text)));
757771

758772
result.push(localDeclaration);
759773
} else if (this.isModule && (ts.getCombinedModifierFlags(statement) & ts.ModifierFlags.Export)) {
@@ -775,16 +789,17 @@ export class LuaTransformer {
775789
result.push(localDeclaration);
776790
} else {
777791
// local NS = NS or {}
778-
const localDeclaration = tstl.createVariableDeclarationStatement(
792+
// TODO this is somewhat redundant since createLocalOrGlobalDeclaration also handles exports
793+
const localDeclaration = this.createLocalOrGlobalDeclaration(
779794
this.transformIdentifier(statement.name as ts.Identifier),
780795
tstl.createBinaryExpression(
781-
tstl.createIdentifier("NS"),
796+
this.transformIdentifier(statement.name as ts.Identifier),
782797
tstl.createTableExpression(),
783798
tstl.SyntaxKind.OrOperator
784799
)
785800
);
786801

787-
result.push(localDeclaration);
802+
result.push(...localDeclaration);
788803
}
789804

790805
// Set current namespace for nested NS
@@ -794,7 +809,7 @@ export class LuaTransformer {
794809

795810
// Transform moduleblock to block and visit it
796811
if (statement.body && ts.isModuleBlock(statement.body)) {
797-
result.push(...this.transformStatements(statement.body.statements));
812+
result.push(tstl.createDoStatement(this.transformStatements(statement.body.statements)));
798813
}
799814

800815
this.currentNamespace = previousNamespace;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
MyClass.myFunction = function (self)
1+
MyClass.myFunction = function(self)
22
end;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
TestClass.myFunction = function (self)
1+
TestClass.myFunction = function(self)
22
end;
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
RenamedTestClass.myFunction = function (self)
1+
RenamedTestClass.myFunction = function(self)
22
end;
3-
RenamedMyClass.myFunction = function (self)
3+
RenamedMyClass.myFunction = function(self)
44
end;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
MyClass.test = "test";
22
MyClass.testP = "testP";
3-
MyClass.myFunction = function (self)
3+
MyClass.myFunction = function(self)
44
end;
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
ClassB = ClassB or {};
22
ClassB.__index = ClassB;
3-
ClassB.new = function (construct, ...)
3+
ClassB.new = function(construct, ...)
44
local self = setmetatable({}, ClassB);
55
if construct and ClassB.constructor then
66
ClassB.constructor(self, ...);
77
end
88
return self;
99
end;
10-
ClassB.constructor = function (self)
10+
ClassB.constructor = function(self)
1111
end;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
varargsFunction = function (a, ...)
1+
varargsFunction = function(a, ...)
22
local b = ({...});
33
end;

test/translation/lua/getSetAccessors.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
MyClass = MyClass or {};
22
MyClass.__index = MyClass;
3-
MyClass.new = function (construct, ...)
3+
MyClass.new = function(construct, ...)
44
local self = setmetatable({}, MyClass);
55
if construct and MyClass.constructor then
66
MyClass.constructor(self, ...);
77
end
88
return self;
99
end;
10-
MyClass.constructor = function (self)
10+
MyClass.constructor = function(self)
1111
end;
12-
MyClass.get__field = function (self)
12+
MyClass.get__field = function(self)
1313
return self._field + 4;
1414
end;
15-
MyClass.set__field = function (self, v)
15+
MyClass.set__field = function(self, v)
1616
self._field = v * 2;
1717
end;
1818
local instance = MyClass.new(true);
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
MyClass = MyClass or {};
22
MyClass.__index = MyClass;
3-
MyClass.new = function (construct, ...)
3+
MyClass.new = function(construct, ...)
44
local self = setmetatable({}, MyClass);
55
if construct and MyClass.constructor then
66
MyClass.constructor(self, ...);
77
end
88
return self;
99
end;
10-
MyClass.constructor = function (self)
10+
MyClass.constructor = function(self)
1111
end;
12-
MyClass.varargsFunction = function (self, a, ...)
12+
MyClass.varargsFunction = function(self, a, ...)
1313
local b = ({...});
1414
end;

0 commit comments

Comments
 (0)