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
21 changes: 20 additions & 1 deletion src/Transpiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1186,10 +1186,14 @@ export class LuaTranspiler {
paramNames.push((param.name as ts.Identifier).escapedText as string);
});

// Parameters with default values
const defaultValueParams = node.parameters.filter((declaration) => declaration.initializer !== undefined);

// Build function header
result += this.indent + `function ${callPath}${methodName}(${paramNames.join(",")})\n`;

this.pushIndent();
result += this.transpileParameterDefaultValues(defaultValueParams);
result += this.transpileBlock(body);
this.popIndent();

Expand Down Expand Up @@ -1370,14 +1374,29 @@ export class LuaTranspiler {
paramNames.push((param.name as ts.Identifier).escapedText as string);
});

if (ts.isBlock(node.body)) {
const defaultValueParams = node.parameters.filter((declaration) => declaration.initializer !== undefined);

if (ts.isBlock(node.body) || defaultValueParams.length > 0) {
let result = `function(${paramNames.join(",")})\n`;
this.pushIndent();
result += this.transpileParameterDefaultValues(defaultValueParams);
result += this.transpileBlock(node.body);
this.popIndent();
return result + this.indent + "end\n";
} else {
return `function(${paramNames.join(",")}) return ` + this.transpileExpression(node.body) + " end";
}
}

public transpileParameterDefaultValues(params: ts.ParameterDeclaration[]): string {
let result = "";

params.filter((declaration) => declaration.initializer !== undefined).forEach((declaration) => {
const paramName = (declaration.name as ts.Identifier).escapedText;
const paramValue = this.transpileExpression(declaration.initializer);
result += this.indent + `if ${paramName}==nil then ${paramName}=${paramValue} end\n`;
});

return result;
}
}
14 changes: 14 additions & 0 deletions test/translation/lua/classMethodDefaultParameters.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
MyClass = MyClass or {}
MyClass.__index = MyClass
function MyClass.new(construct, ...)
local instance = setmetatable({}, MyClass)
if construct and MyClass.constructor then MyClass.constructor(instance, ...) end
return instance
end
function MyClass.constructor(self)
end
function MyClass.MyMethod(self,a,b)
if a==nil then a=3 end
if b==nil then b=5 end
return a+b
end
5 changes: 5 additions & 0 deletions test/translation/ts/classMethodDefaultParameters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class MyClass {
public MyMethod(a: number = 3, b: number = 5) {
return a + b;
}
}
56 changes: 50 additions & 6 deletions test/unit/expressions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export class ExpressionTests {
}).toThrowError(Error, expectedError);
}


@TestCase("1+1", "1+1")
@TestCase("1-1", "1-1")
@TestCase("1*1", "1*1")
Expand Down Expand Up @@ -93,7 +92,7 @@ export class ExpressionTests {
@TestCase("a>>>=b", "a=bit.rshift(a,b)")
@Test("Bitop [JIT]")
public bitOperatorOverrideJIT(input: string, lua: string) {
Expect(util.transpileString(input, { luaTarget: 'JIT', dontRequireLuaLib: true })).toBe(lua);
Expect(util.transpileString(input, { luaTarget: "JIT", dontRequireLuaLib: true })).toBe(lua);
}

@TestCase("a&b", "a&b")
Expand All @@ -108,10 +107,9 @@ export class ExpressionTests {
@TestCase("a>>>=b", "a=a>>>b")
@Test("Bitop [5.3]")
public bitOperatorOverride53(input: string, lua: string) {
Expect(util.transpileString(input, { luaTarget: '5.3', dontRequireLuaLib: true })).toBe(lua);
Expect(util.transpileString(input, { luaTarget: "5.3", dontRequireLuaLib: true })).toBe(lua);
}


@TestCase("1+1", "1+1")
@TestCase("-1+1", "-1+1")
@TestCase("1*30+4", "(1*30)+4")
Expand All @@ -130,7 +128,7 @@ export class ExpressionTests {
}

@Test("Arrow Function Expression")
public arrowFunctionExpression(input: string) {
public arrowFunctionExpression() {
// Transpile
const lua = util.transpileString(`let add = (a, b) => a+b; return add(1,2);`);

Expand All @@ -141,8 +139,31 @@ export class ExpressionTests {
Expect(result).toBe(3);
}

@TestCase([])
@TestCase([5])
@TestCase([1, 2])
@Test("Arrow Default Values")
public arrowFunctionDefaultValues(inp: number[]) {
// Default value is 3 for v1
const v1 = inp.length > 0 ? inp[0] : 3;
// Default value is 4 for v2
const v2 = inp.length > 1 ? inp[1] : 4;

const callArgs = inp.join(",");

// Transpile
const lua = util.transpileString(`let add = (a: number = 3, b: number = 4) => { return a+b; }`
+ `return add(${callArgs});`);

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

// Assert
Expect(result).toBe(v1 + v2);
}

@Test("Function Expression")
public functionExpression(input: string) {
public functionExpression() {
// Transpile
const lua = util.transpileString(`let add = function(a, b) {return a+b}; return add(1,2);`);

Expand All @@ -152,4 +173,27 @@ export class ExpressionTests {
// Assert
Expect(result).toBe(3);
}

@TestCase([], 7)
@TestCase([5], 9)
@TestCase([1, 2], 3)
@Test("Arrow Default Values")
public functionExpressionDefaultValues(inp: number[]) {
// Default value is 3 for v1
const v1 = inp.length > 0 ? inp[0] : 3;
// Default value is 4 for v2
const v2 = inp.length > 1 ? inp[1] : 4;

const callArgs = inp.join(",");

// Transpile
const lua = util.transpileString(`let add = function(a: number = 3, b: number = 4) { return a+b; }`
+ `return add(${callArgs});`);

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

// Assert
Expect(result).toBe(v1 + v2);
}
}