Skip to content

Commit 26fe357

Browse files
authored
Fixed constructor overloads (#275)
* Fixed constructor overloads Fixes #274 * Removed lambda body * Removed transpileFile (should be added to changelog since this was exposed in the API)
1 parent 8001015 commit 26fe357

File tree

4 files changed

+58
-23
lines changed

4 files changed

+58
-23
lines changed

src/Compiler.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -193,19 +193,6 @@ export function transpileString(str: string,
193193
return result.trim();
194194
}
195195

196-
export function transpileFile(filePath: string): string {
197-
const program = ts.createProgram([filePath], {});
198-
const checker = program.getTypeChecker();
199-
200-
// Output errors
201-
const diagnostics = ts.getPreEmitDiagnostics(program).filter(diag => diag.code !== 6054);
202-
diagnostics.forEach(diagnostic => console.log(`${ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n")}`));
203-
204-
const options: ts.CompilerOptions = { luaLibImport: "none" };
205-
const result = createTranspiler(checker, options, program.getSourceFile(filePath)).transpileSourceFile();
206-
return result.trim();
207-
}
208-
209196
function reportDiagnostic(diagnostic: ts.Diagnostic): void {
210197
if (diagnostic.file) {
211198
const { line, character } =

src/Transpiler.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -337,9 +337,7 @@ export abstract class LuaTranspiler {
337337

338338
public transpileLuaLibFunction(func: LuaLibFeature, ...params: string[]): string {
339339
this.importLuaLibFeature(func);
340-
params = params.filter(element => {
341-
return element.toString() !== "";
342-
});
340+
params = params.filter(element => element.toString() !== "");
343341
return `__TS__${func}(${params.join(", ")})`;
344342
}
345343

@@ -1778,8 +1776,9 @@ export abstract class LuaTranspiler {
17781776
result += this.indent + `${className}.${fieldName} = ${value}\n`;
17791777
}
17801778

1781-
// Try to find constructor
1782-
const constructor = node.members.filter(ts.isConstructorDeclaration)[0];
1779+
// Find first constructor with body
1780+
const constructor =
1781+
node.members.filter(n => ts.isConstructorDeclaration(n) && n.body)[0] as ts.ConstructorDeclaration;
17831782
if (constructor) {
17841783
// Add constructor plus initialization of instance fields
17851784
result += this.transpileConstructor(constructor, className);

src/tstl.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ export {
99
export {
1010
compile,
1111
compileFilesWithOptions,
12-
transpileFile,
1312
transpileString,
1413
watchWithOptions
1514
} from "./Compiler";

test/unit/overloads.spec.ts

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as util from "../src/util";
44

55
export class OverloadTests {
66
@Test("overload function1")
7-
public overloadFunction1() {
7+
public overloadFunction1(): void {
88
const lua = util.transpileString(
99
`function abc(def: number): string;
1010
function abc(def: string): string;
@@ -23,7 +23,7 @@ export class OverloadTests {
2323
}
2424

2525
@Test("overload function2")
26-
public overloadFunction2() {
26+
public overloadFunction2(): void {
2727
const lua = util.transpileString(
2828
`function abc(def: number): string;
2929
function abc(def: string): string;
@@ -42,7 +42,7 @@ export class OverloadTests {
4242
}
4343

4444
@Test("overload method1")
45-
public overloadMethod1() {
45+
public overloadMethod1(): void {
4646
const lua = util.transpileString(
4747
`class myclass {
4848
static abc(def: number): string;
@@ -63,7 +63,7 @@ export class OverloadTests {
6363
}
6464

6565
@Test("overload method2")
66-
public overloadMethod2() {
66+
public overloadMethod2(): void {
6767
const lua = util.transpileString(
6868
`class myclass {
6969
static abc(def: number): string;
@@ -82,4 +82,54 @@ export class OverloadTests {
8282

8383
Expect(result).toBe("ghj");
8484
}
85+
86+
@Test("constructor1")
87+
public constructor1(): void {
88+
const lua = util.transpileString(
89+
`class myclass {
90+
num: number;
91+
str: string;
92+
93+
constructor(def: number): string;
94+
constructor(def: string): string;
95+
constructor(def: number | string): string {
96+
if (typeof def == "number") {
97+
this.num = def;
98+
} else {
99+
this.str = def;
100+
}
101+
}
102+
}
103+
const inst = new myclass(3);
104+
return inst.num`);
105+
106+
const result = util.executeLua(lua);
107+
108+
Expect(result).toBe(3);
109+
}
110+
111+
@Test("constructor2")
112+
public constructor2(): void {
113+
const lua = util.transpileString(
114+
`class myclass {
115+
num: number;
116+
str: string;
117+
118+
constructor(def: number): string;
119+
constructor(def: string): string;
120+
constructor(def: number | string): string {
121+
if (typeof def == "number") {
122+
this.num = def;
123+
} else {
124+
this.str = def;
125+
}
126+
}
127+
}
128+
const inst = new myclass("ghj");
129+
return inst.str`);
130+
131+
const result = util.executeLua(lua);
132+
133+
Expect(result).toBe("ghj");
134+
}
85135
}

0 commit comments

Comments
 (0)