X Tutup
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/Transpiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,9 @@ export abstract class LuaTranspiler {
case ts.SyntaxKind.FunctionDeclaration:
return this.transpileFunctionDeclaration(node as ts.FunctionDeclaration);
case ts.SyntaxKind.VariableStatement:
return this.indent + this.transpileVariableStatement(node as ts.VariableStatement) + "\n";
return this.indent + this.transpileVariableStatement(node as ts.VariableStatement) + ";\n";
case ts.SyntaxKind.ExpressionStatement:
return this.indent + this.transpileExpression((node as ts.ExpressionStatement).expression) + "\n";
return this.indent + this.transpileExpression((node as ts.ExpressionStatement).expression) + ";\n";
case ts.SyntaxKind.ReturnStatement:
return this.indent + this.transpileReturn(node as ts.ReturnStatement) + "\n";
case ts.SyntaxKind.IfStatement:
Expand Down Expand Up @@ -491,7 +491,7 @@ export abstract class LuaTranspiler {
// Add header
let result = "";
for (const variableDeclaration of (node.initializer as ts.VariableDeclarationList).declarations) {
result += this.indent + this.transpileVariableDeclaration(variableDeclaration);
result += this.indent + this.transpileVariableDeclaration(variableDeclaration) + "\n";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should there also be a ; here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really, you know a while will always follow, so there's no ambiguity. I've tried not to add ; where they weren't needed.

}
result += this.indent + `while(${this.transpileExpression(node.condition)}) do\n`;

Expand Down Expand Up @@ -1407,9 +1407,9 @@ export abstract class LuaTranspiler {
const identifierName = this.transpileIdentifier(node.name);
if (node.initializer) {
const value = this.transpileExpression(node.initializer);
return `local ${identifierName} = ${value}\n`;
return `local ${identifierName} = ${value}`;
} else {
return `local ${identifierName} = nil\n`;
return `local ${identifierName} = nil`;
}
} else if (ts.isArrayBindingPattern(node.name)) {
// Destructuring type
Expand All @@ -1423,9 +1423,9 @@ export abstract class LuaTranspiler {

// Don't unpack TupleReturn decorated functions
if (tsHelper.isTupleReturnCall(node.initializer, this.checker)) {
return `local ${vars}=${this.transpileExpression(node.initializer)}\n`;
return `local ${vars}=${this.transpileExpression(node.initializer)}`;
} else {
return `local ${vars}=${this.transpileDestructingAssignmentValue(node.initializer)}\n`;
return `local ${vars}=${this.transpileDestructingAssignmentValue(node.initializer)}`;
}
} else {
throw TSTLErrors.UnsupportedKind("variable declaration", node.name.kind, node);
Expand Down
2 changes: 1 addition & 1 deletion test/translation/lua/callNamespace.lua
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Namespace.myFunction()
Namespace.myFunction();
27 changes: 9 additions & 18 deletions test/translation/lua/characterEscapeSequence.lua
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
local quoteInDoubleQuotes = "\' \' \'"

local quoteInTemplateString = "\' \' \'"

local doubleQuoteInQuotes = "\" \" \""

local doubleQuoteInDoubleQuotes = "\" \" \""

local doubleQuoteInTemplateString = "\" \" \""

local escapedCharsInQuotes = "\\ \0 \b \t \n \v \f \" \' \`"

local escapedCharsInDoubleQUotes = "\\ \0 \b \t \n \v \f \" \' \`"

local escapedCharsInTemplateString = "\\ \0 \b \t \n \v \f \" \' \`"

local nonEmptyTemplateString = "Level 0: \n\t "..tostring("Level 1: \n\t\t "..tostring("Level 3: \n\t\t\t "..tostring("Last level \n --").." \n --").." \n --").." \n --"

local quoteInDoubleQuotes = "\' \' \'";
local quoteInTemplateString = "\' \' \'";
local doubleQuoteInQuotes = "\" \" \"";
local doubleQuoteInDoubleQuotes = "\" \" \"";
local doubleQuoteInTemplateString = "\" \" \"";
local escapedCharsInQuotes = "\\ \0 \b \t \n \v \f \" \' \`";
local escapedCharsInDoubleQUotes = "\\ \0 \b \t \n \v \f \" \' \`";
local escapedCharsInTemplateString = "\\ \0 \b \t \n \v \f \" \' \`";
local nonEmptyTemplateString = "Level 0: \n\t "..tostring("Level 1: \n\t\t "..tostring("Level 3: \n\t\t\t "..tostring("Last level \n --").." \n --").." \n --").." \n --";
5 changes: 2 additions & 3 deletions test/translation/lua/do.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
local e = 10

local e = 10;
repeat
do
e = (e-1)
e = (e-1);
end
::__continue0::
until not (e>0)
12 changes: 6 additions & 6 deletions test/translation/lua/dotColonFunctionCalls.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
classInstance:colonMethod()
classInstance.dotMethod()
interfaceInstance:colonMethod()
interfaceInstance.dotMethod()
TestNameSpace.dotMethod()
TestNameSpace.dotMethod2()
classInstance:colonMethod();
classInstance.dotMethod();
interfaceInstance:colonMethod();
interfaceInstance.dotMethod();
TestNameSpace.dotMethod();
TestNameSpace.dotMethod2();
2 changes: 1 addition & 1 deletion test/translation/lua/enumMembersOnly.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ val1=0
val2=2
val3=3
val4="bye"
local a = val1
local a = val1;
12 changes: 5 additions & 7 deletions test/translation/lua/getSetAccessors.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ function MyClass.get__field(self)
return self._field+4
end
function MyClass.set__field(self,v)
self._field = (v*2)
self._field = (v*2);
end
local instance = MyClass.new(true)

instance:set__field(4)
local b = instance:get__field()

local c = (4+instance:get__field())*3
local instance = MyClass.new(true);
instance:set__field(4);
local b = instance:get__field();
local c = (4+instance:get__field())*3;
5 changes: 2 additions & 3 deletions test/translation/lua/interfaceIndex.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
local a = {}

a["abc"] = "def"
local a = {};
a["abc"] = "def";
5 changes: 2 additions & 3 deletions test/translation/lua/modulesChangedVariableExport.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
local exports = exports or {}
local test = nil

test = 1
local test = nil;
test = 1;
exports.test = test
return exports
3 changes: 1 addition & 2 deletions test/translation/lua/modulesVariableExport.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
local exports = exports or {}
local test = "test"

local test = "test";
exports.test = test
return exports
2 changes: 1 addition & 1 deletion test/translation/lua/modulesVariableNoExport.lua
Original file line number Diff line number Diff line change
@@ -1 +1 @@
local test = "test"
local test = "test";
2 changes: 1 addition & 1 deletion test/translation/lua/shorthandPropertyAssignment.lua
Original file line number Diff line number Diff line change
@@ -1 +1 @@
local f = function(x) return ({x = x}) end
local f = function(x) return ({x = x}) end;
6 changes: 2 additions & 4 deletions test/translation/lua/tryCatch.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
xpcall(function()
local a = 42

local a = 42;
end,
function(er)
local b = "fail"

local b = "fail";
end)
8 changes: 3 additions & 5 deletions test/translation/lua/tryCatchFinally.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
xpcall(function()
local a = 42

local a = 42;
end,
function(er)
local b = "fail"

local b = "fail";
end)
local c = "finally"
local c = "finally";
5 changes: 2 additions & 3 deletions test/translation/lua/tryFinally.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
xpcall(function()
local a = 42

local a = 42;
end,
function(e)
end)
local b = "finally"
local b = "finally";
5 changes: 2 additions & 3 deletions test/translation/lua/typeAssert.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
local test1 = 10

local test2 = 10
local test1 = 10;
local test2 = 10;
5 changes: 2 additions & 3 deletions test/translation/lua/while.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
local d = 10

local d = 10;
while d>0 do
do
d = (d-1)
d = (d-1);
end
::__continue0::
end
4 changes: 2 additions & 2 deletions test/unit/assignmentDestructuring.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class AssignmentDestructuringTests {
this.assignmentDestruturingTs, {luaTarget: LuaTarget.Lua51, luaLibImport: "none"}
);
// Assert
Expect(lua).toBe(`local a,b=unpack(myFunc())`);
Expect(lua).toBe(`local a,b=unpack(myFunc());`);
}

@Test("Assignment destructuring [5.2]")
Expand All @@ -25,6 +25,6 @@ export class AssignmentDestructuringTests {
this.assignmentDestruturingTs, {luaTarget: LuaTarget.Lua52, luaLibImport: "none"}
);
// Assert
Expect(lua).toBe(`local a,b=table.unpack(myFunc())`);
Expect(lua).toBe(`local a,b=table.unpack(myFunc());`);
}
}
16 changes: 8 additions & 8 deletions test/unit/assignments.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class AssignmentTests {
@Test("Const assignment")
public constAssignment(inp: string, out: string): void {
const lua = util.transpileString(`const myvar = ${inp};`);
Expect(lua).toBe(`local myvar = ${out}`);
Expect(lua).toBe(`local myvar = ${out};`);
}

@TestCase(`"abc"`, `"abc"`)
Expand All @@ -27,7 +27,7 @@ export class AssignmentTests {
@Test("Const assignment")
public letAssignment(inp: string, out: string): void {
const lua = util.transpileString(`let myvar = ${inp};`);
Expect(lua).toBe(`local myvar = ${out}`);
Expect(lua).toBe(`local myvar = ${out};`);
}

@TestCase(`"abc"`, `"abc"`)
Expand All @@ -39,7 +39,7 @@ export class AssignmentTests {
@Test("Const assignment")
public varAssignment(inp: string, out: string): void {
const lua = util.transpileString(`var myvar = ${inp};`);
Expect(lua).toBe(`local myvar = ${out}`);
Expect(lua).toBe(`local myvar = ${out};`);
}

@TestCase("var myvar;")
Expand Down Expand Up @@ -81,7 +81,7 @@ export class AssignmentTests {
+ `let [a,b] = abc();`;

const lua = util.transpileString(code);
Expect(lua).toBe("local a,b=abc()");
Expect(lua).toBe("local a,b=abc();");
}

@Test("TupleReturn Single assignment")
Expand All @@ -92,7 +92,7 @@ export class AssignmentTests {
+ `a = abc();`;

const lua = util.transpileString(code);
Expect(lua).toBe("local a = ({ abc() })\n\na = ({ abc() })");
Expect(lua).toBe("local a = ({ abc() });\na = ({ abc() });");
}

@Test("TupleReturn interface assignment")
Expand All @@ -104,7 +104,7 @@ export class AssignmentTests {
+ `let [a,b] = jkl.abc();`;

const lua = util.transpileString(code);
Expect(lua).toBe("local a,b=jkl:abc()");
Expect(lua).toBe("local a,b=jkl:abc();");
}

@Test("TupleReturn namespace assignment")
Expand All @@ -116,7 +116,7 @@ export class AssignmentTests {
+ `let [a,b] = def.abc();`;

const lua = util.transpileString(code);
Expect(lua).toBe("local a,b=def.abc()");
Expect(lua).toBe("local a,b=def.abc();");
}

@Test("TupleReturn method assignment")
Expand All @@ -128,7 +128,7 @@ export class AssignmentTests {
+ `let [a,b] = jkl.abc();`;

const lua = util.transpileString(code);
Expect(lua).toBe("local jkl = def.new(true)\n\nlocal a,b=jkl:abc()");
Expect(lua).toBe("local jkl = def.new(true);\nlocal a,b=jkl:abc();");
}

@Test("TupleReturn functional")
Expand Down
27 changes: 27 additions & 0 deletions test/unit/class.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,33 @@ export class ClassTests {
Expect(result).toBe(4);
}

@Test("CastClassMethodCall")
public extraParanthesisAssignment() {
// Transpile
const lua = util.transpileString(
`interface result
{
val : number;
}
class a {
public method(out: result) {
out.val += 2;
}
}
let inst:any = new a();
let result = {val : 0};
(inst as a).method(result);
(inst as a).method(result);
return result.val;`
);

// Execute
const result = util.executeLua(lua);

// Assert
Expect(result).toBe(4);
}

@Test("ClassInheritedMethodCall")
public classInheritedMethodCall(): void {
// Transpile
Expand Down
2 changes: 1 addition & 1 deletion test/unit/curry.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class LuaCurryTests {
`(x: number) => (y: number) => x + y;`
);
// Assert
Expect(lua).toBe(`function(x) return function(y) return x+y end end`);
Expect(lua).toBe(`function(x) return function(y) return x+y end end;`);
}

@Test("curryingAdd")
Expand Down
Loading
X Tutup