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 12cec8911..48f697fcc 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(", ")})`; } @@ -1778,8 +1776,9 @@ 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 => 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"; 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"); + } }