Skip to content

Commit 0d0f81b

Browse files
committed
Added ability to deal with overloads
1 parent ca573cb commit 0d0f81b

3 files changed

Lines changed: 52 additions & 14 deletions

File tree

src/Transpiler.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,6 +1185,9 @@ export abstract class LuaTranspiler {
11851185
}
11861186

11871187
public transpileFunctionDeclaration(node: ts.FunctionDeclaration): string {
1188+
// Don't transpile functions without body (overload declarations)
1189+
if (!node.body) { return ""; }
1190+
11881191
let result = "";
11891192
const identifier = node.name;
11901193
const methodName = identifier.escapedText;
@@ -1232,6 +1235,9 @@ export abstract class LuaTranspiler {
12321235
}
12331236

12341237
public transpileMethodDeclaration(node: ts.MethodDeclaration, callPath: string): string {
1238+
// Don't transpile methods without body (overload declarations)
1239+
if (!node.body) { return ""; }
1240+
12351241
let result = "";
12361242
const identifier = node.name as ts.Identifier;
12371243
const methodName = identifier.escapedText;

test/unit/expressions.spec.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,6 @@ export class ExpressionTests {
1919
Expect(util.transpileString(input)).toBe(lua);
2020
}
2121

22-
@TestCase("obj instanceof someClass", "Unsupported binary operator kind: instanceof")
23-
@TestCase("typeof obj", "Unsupported expression kind: TypeOfExpression")
24-
@Test("Prohibted Expressions")
25-
public prohibtedExpressions(input: string, expectedError: string) {
26-
Expect(() => {
27-
util.transpileString(input);
28-
}).toThrowError(Error, expectedError);
29-
}
30-
3122
@TestCase("1+1", "1+1")
3223
@TestCase("1-1", "1-1")
3324
@TestCase("1*1", "1*1")

test/unit/overloads.spec.ts

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ import { Expect, Test, TestCase } from "alsatian";
33
import * as util from "../src/util";
44

55
export class OverloadTests {
6-
7-
@Test("overload1")
8-
public overload1() {
6+
@Test("overload function1")
7+
public overloadFunction1() {
98
const lua = util.transpileString(
109
`function abc(def: number): string;
1110
function abc(def: string): string;
@@ -23,8 +22,8 @@ export class OverloadTests {
2322
Expect(result).toBe("jkl9");
2423
}
2524

26-
@Test("overload2")
27-
public overload2() {
25+
@Test("overload function2")
26+
public overloadFunction2() {
2827
const lua = util.transpileString(
2928
`function abc(def: number): string;
3029
function abc(def: string): string;
@@ -41,4 +40,46 @@ export class OverloadTests {
4140

4241
Expect(result).toBe("ghj");
4342
}
43+
44+
@Test("overload method1")
45+
public overloadMethod1() {
46+
const lua = util.transpileString(
47+
`class myclass {
48+
static abc(def: number): string;
49+
static abc(def: string): string;
50+
static abc(def: number | string): string {
51+
if (typeof def == "number") {
52+
return "jkl" + (def * 3);
53+
} else {
54+
return def;
55+
}
56+
}
57+
}
58+
return myclass.abc(3);`);
59+
60+
const result = util.executeLua(lua);
61+
62+
Expect(result).toBe("jkl9");
63+
}
64+
65+
@Test("overload method2")
66+
public overloadMethod2() {
67+
const lua = util.transpileString(
68+
`class myclass {
69+
static abc(def: number): string;
70+
static abc(def: string): string;
71+
static abc(def: number | string): string {
72+
if (typeof def == "number") {
73+
return "jkl" + (def * 3);
74+
} else {
75+
return def;
76+
}
77+
}
78+
}
79+
return myclass.abc("ghj");`);
80+
81+
const result = util.executeLua(lua);
82+
83+
Expect(result).toBe("ghj");
84+
}
4485
}

0 commit comments

Comments
 (0)