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
2 changes: 1 addition & 1 deletion src/CommandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ function runDiagnostics(commandLine: ts.ParsedCommandLine) {
}
}

commandLine.errors.forEach((err) => {
commandLine.errors.forEach(err => {
let ignore = false;
// Ignore errors caused by tstl specific compiler options
if (err.code === tsInvalidCompilerOptionErrorCode) {
Expand Down
8 changes: 4 additions & 4 deletions src/Compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export function compile(fileNames: string[], options: CompilerOptions): void {
const checker = program.getTypeChecker();

// Get all diagnostics, ignore unsupported extension
const diagnostics = ts.getPreEmitDiagnostics(program).filter((diag) => diag.code !== 6054);
diagnostics.forEach((diagnostic) => {
const diagnostics = ts.getPreEmitDiagnostics(program).filter(diag => diag.code !== 6054);
diagnostics.forEach(diagnostic => {
if (diagnostic.file) {
const { line, character } =
diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start!);
Expand All @@ -30,12 +30,12 @@ export function compile(fileNames: string[], options: CompilerOptions): void {
});

// If there are errors dont emit
if (diagnostics.filter((diag) => diag.category === ts.DiagnosticCategory.Error).length > 0) {
if (diagnostics.filter(diag => diag.category === ts.DiagnosticCategory.Error).length > 0) {
console.log("Stopping compilation process because of errors.");
process.exit(1);
}

program.getSourceFiles().forEach((sourceFile) => {
program.getSourceFiles().forEach(sourceFile => {
if (!sourceFile.isDeclarationFile) {
try {
const rootDir = options.rootDir;
Expand Down
10 changes: 6 additions & 4 deletions src/TSHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ export class TSHelper {
}

public static containsStatement(statements: ts.NodeArray<ts.Statement>, kind: ts.SyntaxKind): boolean {
return statements.some((statement) => statement.kind === kind);
return statements.some(statement => statement.kind === kind);
}

public static isFileModule(sourceFile: ts.SourceFile) {
if (sourceFile) {
// Vanilla ts flags files as external module if they have an import or
// export statement, we only check for export statements
return sourceFile.statements.some((statement) =>
return sourceFile.statements.some(statement =>
(ts.getCombinedModifierFlags(statement) & ts.ModifierFlags.Export) !== 0
|| statement.kind === ts.SyntaxKind.ExportAssignment
|| statement.kind === ts.SyntaxKind.ExportDeclaration);
Expand Down Expand Up @@ -88,9 +88,11 @@ export class TSHelper {

public static hasCustomDecorator(type: ts.Type, checker: ts.TypeChecker, decorator: string): boolean {
if (type.symbol) {
const comment = type.symbol.getDocumentationComment(checker);
const comments = type.symbol.getDocumentationComment(checker);
const decorators =
comment.filter((_) => _.kind === "text").map((_) => _.text.trim()).filter((_) => _[0] === "!");
comments.filter(comment => comment.kind === "text")
.map(comment => comment.text.trim())
.filter(comment => comment[0] === "!");
return decorators.indexOf(decorator) > -1;
}
return false;
Expand Down
141 changes: 75 additions & 66 deletions src/Transpiler.ts

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions test/translation/lua/classConstructorAssignment.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Test = Test or {}
Test.__index = Test
function Test.new(construct, ...)
local instance = setmetatable({}, Test)
if construct and Test.constructor then Test.constructor(instance, ...) end
return instance
end
function Test.constructor(self,field)
self.field = field
end
3 changes: 2 additions & 1 deletion test/translation/lua/enumMembersOnly.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
val1=0
val2=2
val3=3
val3=3
local a = val1
3 changes: 3 additions & 0 deletions test/translation/lua/returnDefault.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function myFunc()
return
end
3 changes: 3 additions & 0 deletions test/translation/ts/classConstructorAssignment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Test {
constructor(private field: number) {}
}
4 changes: 3 additions & 1 deletion test/translation/ts/enumMembersOnly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ enum TestEnum {
val1 = 0,
val2 = 2,
val3
}
}

const a = TestEnum.val1;
3 changes: 3 additions & 0 deletions test/translation/ts/returnDefault.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function myFunc() {
return;
}
41 changes: 37 additions & 4 deletions test/unit/assignments.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Expect, Test, TestCase } from "alsatian";
import { Expect, Test, TestCase, FocusTest } from "alsatian";
import { TranspileError } from "../../src/Transpiler";

import * as util from "../src/util";
const fs = require("fs");
Expand All @@ -13,7 +14,7 @@ export class AssignmentTests {
@TestCase(`{a:3,b:"4"}`, `{a = 3,b = "4"}`)
@Test("Const assignment")
public constAssignment(inp: string, out: string) {
var lua = util.transpileString(`const myvar = ${inp};`)
const lua = util.transpileString(`const myvar = ${inp};`);
Expect(lua).toBe(`local myvar = ${out}`);
}

Expand All @@ -25,7 +26,7 @@ export class AssignmentTests {
@TestCase(`{a:3,b:"4"}`, `{a = 3,b = "4"}`)
@Test("Const assignment")
public letAssignment(inp: string, out: string) {
var lua = util.transpileString(`let myvar = ${inp};`)
const lua = util.transpileString(`let myvar = ${inp};`);
Expect(lua).toBe(`local myvar = ${out}`);
}

Expand All @@ -37,7 +38,39 @@ export class AssignmentTests {
@TestCase(`{a:3,b:"4"}`, `{a = 3,b = "4"}`)
@Test("Const assignment")
public varAssignment(inp: string, out: string) {
var lua = util.transpileString(`var myvar = ${inp};`)
const lua = util.transpileString(`var myvar = ${inp};`);
Expect(lua).toBe(`local myvar = ${out}`);
}

@TestCase("var myvar;")
@TestCase("let myvar;")
@TestCase("const myvar;")
@TestCase("const myvar = null;")
@TestCase("const myvar = undefined;")
@Test("Null assignments")
public nullAssignment(declaration: string) {
const lua = util.transpileString(declaration + " return myvar;");
const result = util.executeLua(lua);
Expect(result).toBe(undefined);
}

@TestCase(["a", "b"], ["e", "f"])
@TestCase(["a", "b"], ["e", "f", "g"])
@TestCase(["a", "b", "c"], ["e", "f", "g"])
@Test("Binding pattern assignment")
public bindingPattern(input: string[], values: string[]) {
const pattern = input.join(",");
const initializer = values.map(v => `"${v}"`).join(",");

const lua = util.transpileString(`const [${pattern}] = [${initializer}]; return [${pattern}].join("-");`);
const result = util.executeLua(lua);

Expect(result).toBe(values.slice(0, input.length).join("-"));
}

@Test("Ellipsis binding pattern")
public ellipsisBindingPattern() {
Expect(() => util.transpileString("let [a,b,...c] = [1,2,3];"))
.toThrowError(Error, "Ellipsis destruction is not allowed.");
}
}
4 changes: 2 additions & 2 deletions test/unit/error.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export class LuaErrorTests {
@Test("throwString")
public trowString() {
// Transpile
let lua = util.transpileString(
const lua = util.transpileString(
`throw "Some Error"`
);
// Assert
Expand All @@ -17,7 +17,7 @@ export class LuaErrorTests {
public throwError() {
// Transpile & Asser
Expect(() => {
let lua = util.transpileString(
const lua = util.transpileString(
`throw Error("Some Error")`
);
}).toThrowError(Error, "Unsupported throw expression, only string literals are supported");
Expand Down
13 changes: 12 additions & 1 deletion test/unit/expressions.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Expect, Test, TestCase, FocusTest } from "alsatian";
import { Expect, Test, TestCase } from "alsatian";
import { LuaTarget } from "../../src/Transpiler";

import * as ts from "typescript";
Expand Down Expand Up @@ -175,6 +175,16 @@ export class ExpressionTests {
Expect(result).toBe(3);
}

@Test("Null Expression")
public nullExpression() {
Expect(util.transpileString("null")).toBe("nil");
}

@Test("Undefined Expression")
public undefinedExpression() {
Expect(util.transpileString("undefined")).toBe("nil");
}

@TestCase([], 7)
@TestCase([5], 9)
@TestCase([1, 2], 3)
Expand Down Expand Up @@ -230,6 +240,7 @@ export class ExpressionTests {
}

@TestCase("= 4", 4 + 4)
@TestCase("-= 3", 4 - 3 + 4)
@TestCase("+= 3", 4 + 3 + 4)
@TestCase("*= 3", 4 * 3 + 4)
@TestCase("/= 3", 4 / 3 + 4)
Expand Down
Loading