From c6d7dd32812a1f4e6992b92132420352d39d5d84 Mon Sep 17 00:00:00 2001 From: Lolleko Date: Thu, 8 Nov 2018 09:59:28 +0100 Subject: [PATCH 1/3] Fixed constructor overloads Fixes #274 --- src/Transpiler.ts | 8 +++-- test/unit/overloads.spec.ts | 58 ++++++++++++++++++++++++++++++++++--- 2 files changed, 59 insertions(+), 7 deletions(-) diff --git a/src/Transpiler.ts b/src/Transpiler.ts index 12cec8911..d72e67fe1 100644 --- a/src/Transpiler.ts +++ b/src/Transpiler.ts @@ -339,7 +339,7 @@ export abstract class LuaTranspiler { this.importLuaLibFeature(func); params = params.filter(element => { return element.toString() !== ""; - }); + }); return `__TS__${func}(${params.join(", ")})`; } @@ -1778,8 +1778,10 @@ export abstract class LuaTranspiler { result += this.indent + `${className}.${fieldName} = ${value}\n`; } - // Try to find constructor - const constructor = node.members.filter(ts.isConstructorDeclaration)[0]; + // Find first constructor with body + const constructor = node.members.filter(n => { + return ts.isConstructorDeclaration(n) && n.body; + })[0] as ts.ConstructorDeclaration; if (constructor) { // Add constructor plus initialization of instance fields result += this.transpileConstructor(constructor, className); diff --git a/test/unit/overloads.spec.ts b/test/unit/overloads.spec.ts index 69fa96cc1..e98fd6f0b 100644 --- a/test/unit/overloads.spec.ts +++ b/test/unit/overloads.spec.ts @@ -4,7 +4,7 @@ import * as util from "../src/util"; export class OverloadTests { @Test("overload function1") - public overloadFunction1() { + public overloadFunction1(): void { const lua = util.transpileString( `function abc(def: number): string; function abc(def: string): string; @@ -23,7 +23,7 @@ export class OverloadTests { } @Test("overload function2") - public overloadFunction2() { + public overloadFunction2(): void { const lua = util.transpileString( `function abc(def: number): string; function abc(def: string): string; @@ -42,7 +42,7 @@ export class OverloadTests { } @Test("overload method1") - public overloadMethod1() { + public overloadMethod1(): void { const lua = util.transpileString( `class myclass { static abc(def: number): string; @@ -63,7 +63,7 @@ export class OverloadTests { } @Test("overload method2") - public overloadMethod2() { + public overloadMethod2(): void { const lua = util.transpileString( `class myclass { static abc(def: number): string; @@ -82,4 +82,54 @@ export class OverloadTests { Expect(result).toBe("ghj"); } + + @Test("constructor1") + public constructor1(): void { + const lua = util.transpileString( + `class myclass { + num: number; + str: string; + + constructor(def: number): string; + constructor(def: string): string; + constructor(def: number | string): string { + if (typeof def == "number") { + this.num = def; + } else { + this.str = def; + } + } + } + const inst = new myclass(3); + return inst.num`); + + const result = util.executeLua(lua); + + Expect(result).toBe(3); + } + + @Test("constructor2") + public constructor2(): void { + const lua = util.transpileString( + `class myclass { + num: number; + str: string; + + constructor(def: number): string; + constructor(def: string): string; + constructor(def: number | string): string { + if (typeof def == "number") { + this.num = def; + } else { + this.str = def; + } + } + } + const inst = new myclass("ghj"); + return inst.str`); + + const result = util.executeLua(lua); + + Expect(result).toBe("ghj"); + } } From 6999e33395660adbd86ed63d8213383dc9a09045 Mon Sep 17 00:00:00 2001 From: Lolleko Date: Thu, 8 Nov 2018 10:07:26 +0100 Subject: [PATCH 2/3] Removed lambda body --- src/Transpiler.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Transpiler.ts b/src/Transpiler.ts index d72e67fe1..04aa8e304 100644 --- a/src/Transpiler.ts +++ b/src/Transpiler.ts @@ -337,9 +337,7 @@ export abstract class LuaTranspiler { public transpileLuaLibFunction(func: LuaLibFeature, ...params: string[]): string { this.importLuaLibFeature(func); - params = params.filter(element => { - return element.toString() !== ""; - }); + params = params.filter(element => element.toString() !== ""); return `__TS__${func}(${params.join(", ")})`; } From 279a041f5db14470a007b3fefeec313ed8a83e90 Mon Sep 17 00:00:00 2001 From: Lolleko Date: Thu, 8 Nov 2018 10:45:16 +0100 Subject: [PATCH 3/3] Removed transpileString (should be added to changelog since this was exposed in the API) --- src/Compiler.ts | 13 ------------- src/Transpiler.ts | 5 ++--- src/tstl.ts | 1 - 3 files changed, 2 insertions(+), 17 deletions(-) diff --git a/src/Compiler.ts b/src/Compiler.ts index b15c3850f..1f2eaa8e4 100644 --- a/src/Compiler.ts +++ b/src/Compiler.ts @@ -193,19 +193,6 @@ export function transpileString(str: string, return result.trim(); } -export function transpileFile(filePath: string): string { - const program = ts.createProgram([filePath], {}); - const checker = program.getTypeChecker(); - - // Output errors - const diagnostics = ts.getPreEmitDiagnostics(program).filter(diag => diag.code !== 6054); - diagnostics.forEach(diagnostic => console.log(`${ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n")}`)); - - const options: ts.CompilerOptions = { luaLibImport: "none" }; - const result = createTranspiler(checker, options, program.getSourceFile(filePath)).transpileSourceFile(); - return result.trim(); -} - function reportDiagnostic(diagnostic: ts.Diagnostic): void { if (diagnostic.file) { const { line, character } = diff --git a/src/Transpiler.ts b/src/Transpiler.ts index 04aa8e304..48f697fcc 100644 --- a/src/Transpiler.ts +++ b/src/Transpiler.ts @@ -1777,9 +1777,8 @@ export abstract class LuaTranspiler { } // Find first constructor with body - const constructor = node.members.filter(n => { - return ts.isConstructorDeclaration(n) && n.body; - })[0] as ts.ConstructorDeclaration; + const constructor = + node.members.filter(n => ts.isConstructorDeclaration(n) && n.body)[0] as ts.ConstructorDeclaration; if (constructor) { // Add constructor plus initialization of instance fields result += this.transpileConstructor(constructor, className); diff --git a/src/tstl.ts b/src/tstl.ts index cef7e8cc0..31174588c 100644 --- a/src/tstl.ts +++ b/src/tstl.ts @@ -9,7 +9,6 @@ export { export { compile, compileFilesWithOptions, - transpileFile, transpileString, watchWithOptions } from "./Compiler";