Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 0 additions & 13 deletions src/Compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 } =
Expand Down
9 changes: 4 additions & 5 deletions src/Transpiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(", ")})`;
}

Expand Down Expand Up @@ -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);
Expand Down
1 change: 0 additions & 1 deletion src/tstl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ export {
export {
compile,
compileFilesWithOptions,
transpileFile,
transpileString,
watchWithOptions
} from "./Compiler";
Expand Down
58 changes: 54 additions & 4 deletions test/unit/overloads.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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");
}
}