Skip to content

Commit 6d62d5a

Browse files
authored
Merge pull request #84 from Perryvw/default-parameter-values
Added support for default parameter values
2 parents 9eab650 + 25b4143 commit 6d62d5a

File tree

4 files changed

+89
-7
lines changed

4 files changed

+89
-7
lines changed

src/Transpiler.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1186,10 +1186,14 @@ export class LuaTranspiler {
11861186
paramNames.push((param.name as ts.Identifier).escapedText as string);
11871187
});
11881188

1189+
// Parameters with default values
1190+
const defaultValueParams = node.parameters.filter((declaration) => declaration.initializer !== undefined);
1191+
11891192
// Build function header
11901193
result += this.indent + `function ${callPath}${methodName}(${paramNames.join(",")})\n`;
11911194

11921195
this.pushIndent();
1196+
result += this.transpileParameterDefaultValues(defaultValueParams);
11931197
result += this.transpileBlock(body);
11941198
this.popIndent();
11951199

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

1373-
if (ts.isBlock(node.body)) {
1377+
const defaultValueParams = node.parameters.filter((declaration) => declaration.initializer !== undefined);
1378+
1379+
if (ts.isBlock(node.body) || defaultValueParams.length > 0) {
13741380
let result = `function(${paramNames.join(",")})\n`;
13751381
this.pushIndent();
1382+
result += this.transpileParameterDefaultValues(defaultValueParams);
13761383
result += this.transpileBlock(node.body);
13771384
this.popIndent();
13781385
return result + this.indent + "end\n";
13791386
} else {
13801387
return `function(${paramNames.join(",")}) return ` + this.transpileExpression(node.body) + " end";
13811388
}
13821389
}
1390+
1391+
public transpileParameterDefaultValues(params: ts.ParameterDeclaration[]): string {
1392+
let result = "";
1393+
1394+
params.filter((declaration) => declaration.initializer !== undefined).forEach((declaration) => {
1395+
const paramName = (declaration.name as ts.Identifier).escapedText;
1396+
const paramValue = this.transpileExpression(declaration.initializer);
1397+
result += this.indent + `if ${paramName}==nil then ${paramName}=${paramValue} end\n`;
1398+
});
1399+
1400+
return result;
1401+
}
13831402
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
MyClass = MyClass or {}
2+
MyClass.__index = MyClass
3+
function MyClass.new(construct, ...)
4+
local instance = setmetatable({}, MyClass)
5+
if construct and MyClass.constructor then MyClass.constructor(instance, ...) end
6+
return instance
7+
end
8+
function MyClass.constructor(self)
9+
end
10+
function MyClass.MyMethod(self,a,b)
11+
if a==nil then a=3 end
12+
if b==nil then b=5 end
13+
return a+b
14+
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class MyClass {
2+
public MyMethod(a: number = 3, b: number = 5) {
3+
return a + b;
4+
}
5+
}

test/unit/expressions.spec.ts

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ export class ExpressionTests {
2727
}).toThrowError(Error, expectedError);
2828
}
2929

30-
3130
@TestCase("1+1", "1+1")
3231
@TestCase("1-1", "1-1")
3332
@TestCase("1*1", "1*1")
@@ -93,7 +92,7 @@ export class ExpressionTests {
9392
@TestCase("a>>>=b", "a=bit.rshift(a,b)")
9493
@Test("Bitop [JIT]")
9594
public bitOperatorOverrideJIT(input: string, lua: string) {
96-
Expect(util.transpileString(input, { luaTarget: 'JIT', dontRequireLuaLib: true })).toBe(lua);
95+
Expect(util.transpileString(input, { luaTarget: "JIT", dontRequireLuaLib: true })).toBe(lua);
9796
}
9897

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

114-
115113
@TestCase("1+1", "1+1")
116114
@TestCase("-1+1", "-1+1")
117115
@TestCase("1*30+4", "(1*30)+4")
@@ -130,7 +128,7 @@ export class ExpressionTests {
130128
}
131129

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

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

142+
@TestCase([])
143+
@TestCase([5])
144+
@TestCase([1, 2])
145+
@Test("Arrow Default Values")
146+
public arrowFunctionDefaultValues(inp: number[]) {
147+
// Default value is 3 for v1
148+
const v1 = inp.length > 0 ? inp[0] : 3;
149+
// Default value is 4 for v2
150+
const v2 = inp.length > 1 ? inp[1] : 4;
151+
152+
const callArgs = inp.join(",");
153+
154+
// Transpile
155+
const lua = util.transpileString(`let add = (a: number = 3, b: number = 4) => { return a+b; }`
156+
+ `return add(${callArgs});`);
157+
158+
// Execute
159+
const result = util.executeLua(lua);
160+
161+
// Assert
162+
Expect(result).toBe(v1 + v2);
163+
}
164+
144165
@Test("Function Expression")
145-
public functionExpression(input: string) {
166+
public functionExpression() {
146167
// Transpile
147168
const lua = util.transpileString(`let add = function(a, b) {return a+b}; return add(1,2);`);
148169

@@ -152,4 +173,27 @@ export class ExpressionTests {
152173
// Assert
153174
Expect(result).toBe(3);
154175
}
176+
177+
@TestCase([], 7)
178+
@TestCase([5], 9)
179+
@TestCase([1, 2], 3)
180+
@Test("Arrow Default Values")
181+
public functionExpressionDefaultValues(inp: number[]) {
182+
// Default value is 3 for v1
183+
const v1 = inp.length > 0 ? inp[0] : 3;
184+
// Default value is 4 for v2
185+
const v2 = inp.length > 1 ? inp[1] : 4;
186+
187+
const callArgs = inp.join(",");
188+
189+
// Transpile
190+
const lua = util.transpileString(`let add = function(a: number = 3, b: number = 4) { return a+b; }`
191+
+ `return add(${callArgs});`);
192+
193+
// Execute
194+
const result = util.executeLua(lua);
195+
196+
// Assert
197+
Expect(result).toBe(v1 + v2);
198+
}
155199
}

0 commit comments

Comments
 (0)