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
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
"tstl",
"transpiler"
],
"main": "dist/tstl.js",
"types": "dist/tstl.d.ts",
"scripts": {
"build": "tsc -p tsconfig.json && npm run build-lualib",
"build-lualib": "ts-node ./build_lualib.ts",
Expand Down Expand Up @@ -41,7 +43,7 @@
"node": ">=8.5.0"
},
"dependencies": {
"typescript": "^2.9.2",
"typescript": "2.9.2",
"yargs": "^12.0.1"
},
"devDependencies": {
Expand Down
20 changes: 8 additions & 12 deletions src/CommandLineParser.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
import { CompilerOptions } from "./CompilerOptions";

import * as fs from "fs";
import * as path from "path";
import * as ts from "typescript";
import * as yargs from "yargs";

export interface CompilerOptions extends ts.CompilerOptions {
addHeader?: boolean;
luaTarget?: string;
luaLibImport?: string;
}

export interface ParsedCommandLine extends ts.ParsedCommandLine {
interface ParsedCommandLine extends ts.ParsedCommandLine {
options: CompilerOptions;
}

export class CLIError extends Error {

}

export interface YargsOptions {
interface YargsOptions {
[key: string]: yargs.Options;
}

Expand All @@ -42,6 +34,10 @@ export const optionDeclarations: YargsOptions = {
},
};

class CLIError extends Error {

}

/**
* Removes defaults from the arguments.
* Returns a tuple where [0] is a copy of the options without defaults and [1] is the extracted defaults.
Expand Down
81 changes: 55 additions & 26 deletions src/Compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ import * as fs from "fs";
import * as path from "path";
import * as ts from "typescript";

import { CompilerOptions, parseCommandLine } from "./CommandLineParser";
import { LuaTranspiler51 } from "./targets/Transpiler.51";
import { LuaTranspiler52 } from "./targets/Transpiler.52";
import { LuaTranspiler53 } from "./targets/Transpiler.53";
import { LuaTranspilerJIT } from "./targets/Transpiler.JIT";
import { LuaLibImportKind, LuaTarget, LuaTranspiler } from "./Transpiler";
import { parseCommandLine } from "./CommandLineParser";
import { CompilerOptions } from "./CompilerOptions";
import { LuaLibImportKind, LuaTarget } from "./Transpiler";

import { createTranspiler } from "./TranspilerFactory";

export function compile(argv: string[]): void {
const commandLine = parseCommandLine(argv);
Expand Down Expand Up @@ -154,27 +153,57 @@ function emitFilesAndReportErrors(program: ts.Program): number {
return 0;
}

export function createTranspiler(checker: ts.TypeChecker,
options: CompilerOptions,
sourceFile: ts.SourceFile): LuaTranspiler {
let luaTargetTranspiler: LuaTranspiler;
const target = options.luaTarget ? options.luaTarget.toLowerCase() : "";
switch (target) {
case LuaTarget.Lua51:
luaTargetTranspiler = new LuaTranspiler51(checker, options, sourceFile);
break;
case LuaTarget.Lua52:
luaTargetTranspiler = new LuaTranspiler52(checker, options, sourceFile);
break;
case LuaTarget.Lua53:
luaTargetTranspiler = new LuaTranspiler53(checker, options, sourceFile);
break;
default:
luaTargetTranspiler = new LuaTranspilerJIT(checker, options, sourceFile);
break;
}
const libSource = fs.readFileSync(path.join(path.dirname(require.resolve("typescript")), "lib.es6.d.ts")).toString();

export function transpileString(str: string,
options: CompilerOptions = {
luaLibImport: LuaLibImportKind.Require,
luaTarget: LuaTarget.Lua53,
}): string {
const compilerHost = {
directoryExists: () => true,
fileExists: (fileName): boolean => true,
getCanonicalFileName: fileName => fileName,
getCurrentDirectory: () => "",
getDefaultLibFileName: () => "lib.es6.d.ts",
getDirectories: () => [],
getNewLine: () => "\n",

getSourceFile: (filename, languageVersion) => {
if (filename === "file.ts") {
return ts.createSourceFile(filename, str, ts.ScriptTarget.Latest, false);
}
if (filename === "lib.es6.d.ts") {
return ts.createSourceFile(filename, libSource, ts.ScriptTarget.Latest, false);
}
return undefined;
},

readFile: () => "",

useCaseSensitiveFileNames: () => false,
// Don't write output
writeFile: (name, text, writeByteOrderMark) => null,
};
const program = ts.createProgram(["file.ts"], options, compilerHost);

const result = createTranspiler(program.getTypeChecker(),
options,
program.getSourceFile("file.ts")).transpileSourceFile();
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")}`));

return luaTargetTranspiler;
const options: ts.CompilerOptions = { luaLibImport: "none" };
const result = createTranspiler(checker, options, program.getSourceFile(filePath)).transpileSourceFile();
return result.trim();
}

function reportDiagnostic(diagnostic: ts.Diagnostic): void {
Expand Down
7 changes: 7 additions & 0 deletions src/CompilerOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import * as ts from "typescript";

export interface CompilerOptions extends ts.CompilerOptions {
addHeader?: boolean;
luaTarget?: string;
luaLibImport?: string;
}
1 change: 1 addition & 0 deletions src/TSHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export class TSHelper {
if (sourceFile) {
// Vanilla ts flags files as external module if they have an import or
// export statement, we only check for export statements
// TODO will break in 3.x
return sourceFile.statements.some(statement =>
(ts.getCombinedModifierFlags(statement) & ts.ModifierFlags.Export) !== 0
|| statement.kind === ts.SyntaxKind.ExportAssignment
Expand Down
6 changes: 3 additions & 3 deletions src/Transpiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as fs from "fs";
import * as path from "path";
import * as ts from "typescript";

import { CompilerOptions } from "./CommandLineParser";
import { CompilerOptions } from "./CompilerOptions";
import { DecoratorKind } from "./Decorator";
import { TSTLErrors } from "./Errors";
import { TSHelper as tsHelper } from "./TSHelper";
Expand Down Expand Up @@ -124,7 +124,7 @@ export abstract class LuaTranspiler {
if (node &&
node.modifiers && this.isModule &&
this.namespace.length === 0 &&
(ts.getCombinedModifierFlags(node) & ts.ModifierFlags.Export)
(ts.getCombinedModifierFlags(node as ts.Declaration) & ts.ModifierFlags.Export)
) {
if (dummy) {
result = this.indent + `exports.${this.definitionName(name)} = {}\n`;
Expand All @@ -133,7 +133,7 @@ export abstract class LuaTranspiler {
}
}
if (this.namespace.length !== 0 &&
(ts.getCombinedModifierFlags(node) & ts.ModifierFlags.Export)) {
(ts.getCombinedModifierFlags(node as ts.Declaration) & ts.ModifierFlags.Export)) {
if (dummy) {
result += this.indent + `${this.namespace[this.namespace.length - 1]}.${name} = {}\n`;
} else {
Expand Down
33 changes: 33 additions & 0 deletions src/TranspilerFactory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { LuaTranspiler51 } from "./targets/Transpiler.51";
import { LuaTranspiler52 } from "./targets/Transpiler.52";
import { LuaTranspiler53 } from "./targets/Transpiler.53";
import { LuaTranspilerJIT } from "./targets/Transpiler.JIT";

import { LuaTarget, LuaTranspiler } from "./Transpiler";

import { CompilerOptions } from "./CompilerOptions";

import * as ts from "typescript";

export function createTranspiler(
checker: ts.TypeChecker, options: CompilerOptions,
sourceFile: ts.SourceFile): LuaTranspiler {
let luaTargetTranspiler: LuaTranspiler;
const target = options.luaTarget ? options.luaTarget.toLowerCase() : "";
switch (target) {
case LuaTarget.Lua51:
luaTargetTranspiler = new LuaTranspiler51(checker, options, sourceFile);
break;
case LuaTarget.Lua52:
luaTargetTranspiler = new LuaTranspiler52(checker, options, sourceFile);
break;
case LuaTarget.Lua53:
luaTargetTranspiler = new LuaTranspiler53(checker, options, sourceFile);
break;
default:
luaTargetTranspiler = new LuaTranspilerJIT(checker, options, sourceFile);
break;
}

return luaTargetTranspiler;
}
31 changes: 31 additions & 0 deletions src/tstl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export {
parseCommandLine
} from "./CommandLineParser";

export {
CompilerOptions
} from "./CompilerOptions";

export {
compile,
compileFilesWithOptions,
transpileFile,
transpileString,
watchWithOptions
} from "./Compiler";

export {LuaTranspiler51} from "./targets/Transpiler.51";
export {LuaTranspiler52} from "./targets/Transpiler.52";
export {LuaTranspiler53} from "./targets/Transpiler.53";
export {LuaTranspilerJIT} from "./targets/Transpiler.JIT";

export {
LuaLibFeature,
LuaLibImportKind,
LuaTarget,
LuaTranspiler,
} from "./Transpiler";

export {
createTranspiler
} from "./TranspilerFactory";
65 changes: 9 additions & 56 deletions test/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,62 +3,15 @@ import * as ts from "typescript";

import { Expect } from "alsatian";

import { CompilerOptions } from "../../src/CommandLineParser";
import { createTranspiler } from "../../src/Compiler";
import { LuaLibImportKind, LuaTarget, LuaTranspiler } from "../../src/Transpiler";
import { transpileString } from "../../src/Compiler";
import { LuaTarget, LuaTranspiler } from "../../src/Transpiler";
import { createTranspiler } from "../../src/TranspilerFactory";

import {lauxlib, lua, lualib, to_jsstring, to_luastring } from "fengari";

const fs = require("fs");

const libSource = fs.readFileSync(path.join(path.dirname(require.resolve("typescript")), "lib.es6.d.ts")).toString();

export function transpileString(str: string, options: CompilerOptions = { luaLibImport: LuaLibImportKind.Require, luaTarget: LuaTarget.Lua53 }): string {
const compilerHost = {
directoryExists: () => true,
fileExists: (fileName): boolean => true,
getCanonicalFileName: fileName => fileName,
getCurrentDirectory: () => "",
getDefaultLibFileName: () => "lib.es6.d.ts",
getDirectories: () => [],
getNewLine: () => "\n",

getSourceFile: (filename, languageVersion) => {
if (filename === "file.ts") {
return ts.createSourceFile(filename, str, ts.ScriptTarget.Latest, false);
}
if (filename === "lib.es6.d.ts") {
return ts.createSourceFile(filename, libSource, ts.ScriptTarget.Latest, false);
}
return undefined;
},

readFile: () => "",

useCaseSensitiveFileNames: () => false,
// Don't write output
writeFile: (name, text, writeByteOrderMark) => null,
};
const program = ts.createProgram(["file.ts"], options, compilerHost);

const result = createTranspiler(program.getTypeChecker(),
options,
program.getSourceFile("file.ts")).transpileSourceFile();
return result.trim();
}

export function transpileFile(filePath: string): string {
const program = ts.createProgram([filePath], {});
const checker = program.getTypeChecker();
import * as fs from "fs";

// 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();
}
export { transpileString };

export function executeLua(luaStr: string, withLib = true): any {
if (withLib) {
Expand Down Expand Up @@ -91,7 +44,7 @@ export function executeLua(luaStr: string, withLib = true): any {
}
}

export function expectCodeEqual(code1: string, code2: string) {
export function expectCodeEqual(code1: string, code2: string): void {
// Trim leading/trailing whitespace
let c1 = code1.trim();
let c2 = code2.trim();
Expand All @@ -104,14 +57,14 @@ export function expectCodeEqual(code1: string, code2: string) {
}

// Get a mock transpiler to use for testing
export function makeTestTranspiler(target: LuaTarget = LuaTarget.Lua53) {
export function makeTestTranspiler(target: LuaTarget = LuaTarget.Lua53): LuaTranspiler {
return createTranspiler({} as ts.TypeChecker,
{ luaLibImport: "none", luaTarget: target } as any,
{ statements: [] } as any as ts.SourceFile);
}

export function transpileAndExecute(ts: string): any {
return executeLua(transpileString(ts));
export function transpileAndExecute(tsStr: string): any {
return executeLua(transpileString(tsStr));
}

const jsonlib = fs.readFileSync("test/src/json.lua") + "\n";
Expand Down
4 changes: 2 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 { Expect, Test, TestCase } from "alsatian";

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

export class CLITests {

Expand Down
Loading