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
6 changes: 3 additions & 3 deletions src/CommandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class CLIError extends Error {

const optionDeclarations: { [key: string]: yargs.Options } = {
addHeader: {
alias: "ah",
alias: ["ah", "header"],
default: true,
describe: "Specify if a header will be added to compiled files.",
type: "boolean",
Expand Down Expand Up @@ -154,9 +154,9 @@ function runDiagnostics(commandLine: ts.ParsedCommandLine) {
}

/** Find configFile, function from ts api seems to be broken? */
function findConfigFile(commandLine: ts.ParsedCommandLine) {
export function findConfigFile(commandLine: ts.ParsedCommandLine) {
if (!commandLine.options.project) {
return;
throw new CLIError(`error no base path provided, could not find config.`);
}
let configPath;
if (path.isAbsolute(commandLine.options.project)) {
Expand Down
98 changes: 0 additions & 98 deletions src/ForHelper.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/TSHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export class TSHelper {
public static isTupleReturnFunction(type: ts.Type, checker: ts.TypeChecker): boolean {
return type.symbol
&& ((type.symbol.flags & ts.SymbolFlags.Function) !== 0)
&& this.hasCustomDecorator(type, checker, "!TupleReturn");
&& this.hasCustomDecorator(type, checker, "!TupleReturn");
}

public static hasCustomDecorator(type: ts.Type, checker: ts.TypeChecker, decorator: string): boolean {
Expand Down
21 changes: 9 additions & 12 deletions src/Transpiler.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as ts from "typescript";

import { CompilerOptions } from "./CommandLineParser";
import { ForHelper } from "./ForHelper";
import { TSHelper as tsEx } from "./TSHelper";

import * as path from "path";
Expand Down Expand Up @@ -362,24 +361,22 @@ export class LuaTranspiler {
}

public transpileFor(node: ts.ForStatement): string {
// Get iterator variable
const variable = (node.initializer as ts.VariableDeclarationList).declarations[0];
const identifier = variable.name as ts.Identifier;

// Populate three components of lua numeric for loop:
const start = this.transpileExpression(variable.initializer);
const end = ForHelper.GetForEnd(node.condition, this);
const step = ForHelper.GetForStep(node.incrementor, this);

// Add header
let result = this.indent + `for ${identifier.escapedText}=${start},${end},${step} do\n`;
let result = "";
for (const variableDeclaration of (node.initializer as ts.VariableDeclarationList).declarations) {
result += this.transpileVariableDeclaration(variableDeclaration);
}
result += this.indent + `while(${this.transpileExpression(node.condition)}) do\n`;

// Add body
this.pushIndent();
result += this.transpileStatement(node.statement);
result += this.indent + this.transpileExpression(node.incrementor) + "\n";
this.popIndent();

return result + this.indent + "end\n";
result += this.indent + "end\n";

return result;
}

public transpileForOf(node: ts.ForOfStatement): string {
Expand Down
4 changes: 4 additions & 0 deletions test/compiler/project.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ export class CompilerProjectTests {
"typescript_lualib.lua",
"test_src/test_lib/file.lua",
"test_src/main.lua")
@TestCase(".",
"typescript_lualib.lua",
"test_src/test_lib/file.lua",
"test_src/main.lua")
@TestCase("test_src/main.ts",
"typescript_lualib.lua",
"test_src/test_lib/file.lua",
Expand Down
2 changes: 2 additions & 0 deletions test/translation/lua/classExtension2.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
function TestClass.myFunction(self)
end
10 changes: 10 additions & 0 deletions test/translation/lua/classStaticMembers.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
MyClass = MyClass or {}
MyClass.__index = MyClass
function MyClass.new(construct, ...)
local instance = setmetatable({}, MyClass)
if construct and MyClass.constructor then MyClass.constructor(instance, ...) end
return instance
end
MyClass.test = 0
function MyClass.constructor(self)
end
3 changes: 3 additions & 0 deletions test/translation/lua/typeAssert.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
local test1 = 10

local test2 = 10
9 changes: 9 additions & 0 deletions test/translation/ts/classExtension2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/** !Extension */
class TestClass {
}


/** !Extension */
class MyClass extends TestClass {
myFunction() {}
}
3 changes: 3 additions & 0 deletions test/translation/ts/classStaticMembers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class MyClass {
public static test = 0;
}
2 changes: 2 additions & 0 deletions test/translation/ts/typeAssert.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const test1 = <number> 10;
const test2 = 10 as number;
10 changes: 8 additions & 2 deletions test/unit/cli.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Expect, Test, TestCase, Teardown } from "alsatian";

import { CompilerOptions, parseCommandLine, ParsedCommandLine } from "../../src/CommandLineParser";
import { CompilerOptions, findConfigFile, parseCommandLine, ParsedCommandLine } from "../../src/CommandLineParser";


export class CLITests {
Expand All @@ -23,7 +23,7 @@ export class CLITests {
Expect(() => parseCommandLine(['--luaTarget', '42'])).toThrow();
}

@Test("InvalidArgument")
@Test("InvalidArgumentTSTL")
public invalidArgument() {
// Don't check error message because the yargs library messes the message up.
Expect(() => parseCommandLine(['--invalidTarget', 'test'])).toThrow();
Expand Down Expand Up @@ -52,4 +52,10 @@ export class CLITests {
Expect(parsedCommandLine.options['rootDir']).toBe('./testRoot');
}

@Test("Find config no path")
public findConfigNoPath() {
Expect(() => findConfigFile({options: {}, fileNames: [], errors: []})).toThrow();

}

}
18 changes: 18 additions & 0 deletions test/unit/enum.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Expect, Test, TestCase } from "alsatian";
import * as util from "../src/util"

export class EnumTests {
@Test("Unsuported enum")
public unsuportedEnum() {
// Transpile & Assert
Expect(() => {
let lua = util.transpileString(
`enum TestEnum {
val1 = "test",
val2 = "ok",
val3 = "bye"
}`
);
}).toThrowError(Error, "Only numeric initializers allowed for enums.");
}
}
39 changes: 1 addition & 38 deletions test/unit/loops.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,7 @@ export class LuaLoopTests {
@TestCase([0, 1, 2, 3], [1, 2, 3, 4], "let i = 0; arrTest.length > i; i++")
@TestCase([0, 1, 2, 3], [1, 2, 3, 4], "let i = 0; arrTest.length - 1 >= i; i++")
@TestCase([0, 1, 2, 3], [1, 1, 3, 3], "let i = 0; i < arrTest.length; i += 2")
@TestCase([0, 1, 2, 3], [1, 2, 3, 4], "let i = arrTest.length - 1; i <= 0; i--")
@TestCase([0, 1, 2, 3], [0, 2, 2, 4], "let i = arrTest.length - 1; i <= 0; i -= 2")
@TestCase([0, 1, 2, 3], [1, 2, 3, 4 ], "let i = arrTest.length - 1; i >= 0; i--")
@TestCase([0, 1, 2, 3], [0, 2, 2, 4], "let i = arrTest.length - 1; i >= 0; i -= 2")
@TestCase([0, 1, 2, 3], [0, 2, 2, 4], "let i = arrTest.length - 1; i > 0; i -= 2")
@Test("forheader")
Expand All @@ -124,42 +123,6 @@ export class LuaLoopTests {
Expect(result).toBe(JSON.stringify(expected));
}

@Test("forstepThrow")
public forstepThrow(inp: number[], expected: number[], header: string) {
// Transpile & Assert
Expect(() => {
let lua = util.transpileString(
`for (let i = 0; i < 30; i = i + 10) {
}`
);

// Execute
let result = util.executeLua(lua);
}).toThrowError(Error, "Unsupported for-loop increment step: BinaryExpression")
}

@TestCase("let i = 0; i + 3; i++")
@TestCase("let i = 0; 3 + i; i++")
@TestCase("let i = 0; i - 3; i++")
@TestCase("let i = 0; i * 3; i++")
@TestCase("let i = 0; i / 3; i++")
@TestCase("let i = 0; i &= 3; i++")
@TestCase("let i = 0; i < 3; !i")
@TestCase("let i = 0; i < 3; i as string")
@Test("forconditionThrow")
public forconditionThrow(header: string) {
// Transpile & Assert
Expect(() => {
let lua = util.transpileString(
`for (${header}) {
}`
);

// Execute
let result = util.executeLua(lua);
}).toThrow();
}

@TestCase({ ['test1']: 0, ['test2']: 1, ['test3']: 2 }, { ['test1']: 1, ['test2']: 2, ['test3']: 3 })
@Test("forin[Object]")
public forinObject(inp: any, expected: any) {
Expand Down
10 changes: 10 additions & 0 deletions test/unit/string.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ import * as util from "../src/util"

export class StringTests {

@Test("Unsuported string function")
public stringUnsuportedFunction() {
// Assert
Expect(() => {
util.transpileString(
`return "test".testThisIsNoMember()`
);
}).toThrowError(Error, "Unsupported string function: testThisIsNoMember");
}

@TestCase([])
@TestCase([65])
@TestCase([65, 66])
Expand Down