Skip to content

Commit 2cf9a79

Browse files
lollekoPerryvw
authored andcommitted
Public API and better webpack compatibility (#239)
* Generated declarations and exposed API * Refactoring into more files
1 parent d75cdab commit 2cf9a79

File tree

12 files changed

+164
-110
lines changed

12 files changed

+164
-110
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
"tstl",
1010
"transpiler"
1111
],
12+
"main": "dist/tstl.js",
13+
"types": "dist/tstl.d.ts",
1214
"scripts": {
1315
"build": "tsc -p tsconfig.json && npm run build-lualib",
1416
"build-lualib": "ts-node ./build_lualib.ts",
@@ -41,7 +43,7 @@
4143
"node": ">=8.5.0"
4244
},
4345
"dependencies": {
44-
"typescript": "^2.9.2",
46+
"typescript": "2.9.2",
4547
"yargs": "^12.0.1"
4648
},
4749
"devDependencies": {

src/CommandLineParser.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,15 @@
1+
import { CompilerOptions } from "./CompilerOptions";
2+
13
import * as fs from "fs";
24
import * as path from "path";
35
import * as ts from "typescript";
46
import * as yargs from "yargs";
57

6-
export interface CompilerOptions extends ts.CompilerOptions {
7-
addHeader?: boolean;
8-
luaTarget?: string;
9-
luaLibImport?: string;
10-
}
11-
12-
export interface ParsedCommandLine extends ts.ParsedCommandLine {
8+
interface ParsedCommandLine extends ts.ParsedCommandLine {
139
options: CompilerOptions;
1410
}
1511

16-
export class CLIError extends Error {
17-
18-
}
19-
20-
export interface YargsOptions {
12+
interface YargsOptions {
2113
[key: string]: yargs.Options;
2214
}
2315

@@ -42,6 +34,10 @@ export const optionDeclarations: YargsOptions = {
4234
},
4335
};
4436

37+
class CLIError extends Error {
38+
39+
}
40+
4541
/**
4642
* Removes defaults from the arguments.
4743
* Returns a tuple where [0] is a copy of the options without defaults and [1] is the extracted defaults.

src/Compiler.ts

Lines changed: 55 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ import * as fs from "fs";
22
import * as path from "path";
33
import * as ts from "typescript";
44

5-
import { CompilerOptions, parseCommandLine } from "./CommandLineParser";
6-
import { LuaTranspiler51 } from "./targets/Transpiler.51";
7-
import { LuaTranspiler52 } from "./targets/Transpiler.52";
8-
import { LuaTranspiler53 } from "./targets/Transpiler.53";
9-
import { LuaTranspilerJIT } from "./targets/Transpiler.JIT";
10-
import { LuaLibImportKind, LuaTarget, LuaTranspiler } from "./Transpiler";
5+
import { parseCommandLine } from "./CommandLineParser";
6+
import { CompilerOptions } from "./CompilerOptions";
7+
import { LuaLibImportKind, LuaTarget } from "./Transpiler";
8+
9+
import { createTranspiler } from "./TranspilerFactory";
1110

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

157-
export function createTranspiler(checker: ts.TypeChecker,
158-
options: CompilerOptions,
159-
sourceFile: ts.SourceFile): LuaTranspiler {
160-
let luaTargetTranspiler: LuaTranspiler;
161-
const target = options.luaTarget ? options.luaTarget.toLowerCase() : "";
162-
switch (target) {
163-
case LuaTarget.Lua51:
164-
luaTargetTranspiler = new LuaTranspiler51(checker, options, sourceFile);
165-
break;
166-
case LuaTarget.Lua52:
167-
luaTargetTranspiler = new LuaTranspiler52(checker, options, sourceFile);
168-
break;
169-
case LuaTarget.Lua53:
170-
luaTargetTranspiler = new LuaTranspiler53(checker, options, sourceFile);
171-
break;
172-
default:
173-
luaTargetTranspiler = new LuaTranspilerJIT(checker, options, sourceFile);
174-
break;
175-
}
156+
const libSource = fs.readFileSync(path.join(path.dirname(require.resolve("typescript")), "lib.es6.d.ts")).toString();
157+
158+
export function transpileString(str: string,
159+
options: CompilerOptions = {
160+
luaLibImport: LuaLibImportKind.Require,
161+
luaTarget: LuaTarget.Lua53,
162+
}): string {
163+
const compilerHost = {
164+
directoryExists: () => true,
165+
fileExists: (fileName): boolean => true,
166+
getCanonicalFileName: fileName => fileName,
167+
getCurrentDirectory: () => "",
168+
getDefaultLibFileName: () => "lib.es6.d.ts",
169+
getDirectories: () => [],
170+
getNewLine: () => "\n",
171+
172+
getSourceFile: (filename, languageVersion) => {
173+
if (filename === "file.ts") {
174+
return ts.createSourceFile(filename, str, ts.ScriptTarget.Latest, false);
175+
}
176+
if (filename === "lib.es6.d.ts") {
177+
return ts.createSourceFile(filename, libSource, ts.ScriptTarget.Latest, false);
178+
}
179+
return undefined;
180+
},
181+
182+
readFile: () => "",
183+
184+
useCaseSensitiveFileNames: () => false,
185+
// Don't write output
186+
writeFile: (name, text, writeByteOrderMark) => null,
187+
};
188+
const program = ts.createProgram(["file.ts"], options, compilerHost);
189+
190+
const result = createTranspiler(program.getTypeChecker(),
191+
options,
192+
program.getSourceFile("file.ts")).transpileSourceFile();
193+
return result.trim();
194+
}
195+
196+
export function transpileFile(filePath: string): string {
197+
const program = ts.createProgram([filePath], {});
198+
const checker = program.getTypeChecker();
199+
200+
// Output errors
201+
const diagnostics = ts.getPreEmitDiagnostics(program).filter(diag => diag.code !== 6054);
202+
diagnostics.forEach(diagnostic => console.log(`${ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n")}`));
176203

177-
return luaTargetTranspiler;
204+
const options: ts.CompilerOptions = { luaLibImport: "none" };
205+
const result = createTranspiler(checker, options, program.getSourceFile(filePath)).transpileSourceFile();
206+
return result.trim();
178207
}
179208

180209
function reportDiagnostic(diagnostic: ts.Diagnostic): void {

src/CompilerOptions.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import * as ts from "typescript";
2+
3+
export interface CompilerOptions extends ts.CompilerOptions {
4+
addHeader?: boolean;
5+
luaTarget?: string;
6+
luaLibImport?: string;
7+
}

src/TSHelper.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export class TSHelper {
4747
if (sourceFile) {
4848
// Vanilla ts flags files as external module if they have an import or
4949
// export statement, we only check for export statements
50+
// TODO will break in 3.x
5051
return sourceFile.statements.some(statement =>
5152
(ts.getCombinedModifierFlags(statement) & ts.ModifierFlags.Export) !== 0
5253
|| statement.kind === ts.SyntaxKind.ExportAssignment

src/Transpiler.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as fs from "fs";
22
import * as path from "path";
33
import * as ts from "typescript";
44

5-
import { CompilerOptions } from "./CommandLineParser";
5+
import { CompilerOptions } from "./CompilerOptions";
66
import { DecoratorKind } from "./Decorator";
77
import { TSTLErrors } from "./Errors";
88
import { TSHelper as tsHelper } from "./TSHelper";
@@ -124,7 +124,7 @@ export abstract class LuaTranspiler {
124124
if (node &&
125125
node.modifiers && this.isModule &&
126126
this.namespace.length === 0 &&
127-
(ts.getCombinedModifierFlags(node) & ts.ModifierFlags.Export)
127+
(ts.getCombinedModifierFlags(node as ts.Declaration) & ts.ModifierFlags.Export)
128128
) {
129129
if (dummy) {
130130
result = this.indent + `exports.${this.definitionName(name)} = {}\n`;
@@ -133,7 +133,7 @@ export abstract class LuaTranspiler {
133133
}
134134
}
135135
if (this.namespace.length !== 0 &&
136-
(ts.getCombinedModifierFlags(node) & ts.ModifierFlags.Export)) {
136+
(ts.getCombinedModifierFlags(node as ts.Declaration) & ts.ModifierFlags.Export)) {
137137
if (dummy) {
138138
result += this.indent + `${this.namespace[this.namespace.length - 1]}.${name} = {}\n`;
139139
} else {

src/TranspilerFactory.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { LuaTranspiler51 } from "./targets/Transpiler.51";
2+
import { LuaTranspiler52 } from "./targets/Transpiler.52";
3+
import { LuaTranspiler53 } from "./targets/Transpiler.53";
4+
import { LuaTranspilerJIT } from "./targets/Transpiler.JIT";
5+
6+
import { LuaTarget, LuaTranspiler } from "./Transpiler";
7+
8+
import { CompilerOptions } from "./CompilerOptions";
9+
10+
import * as ts from "typescript";
11+
12+
export function createTranspiler(
13+
checker: ts.TypeChecker, options: CompilerOptions,
14+
sourceFile: ts.SourceFile): LuaTranspiler {
15+
let luaTargetTranspiler: LuaTranspiler;
16+
const target = options.luaTarget ? options.luaTarget.toLowerCase() : "";
17+
switch (target) {
18+
case LuaTarget.Lua51:
19+
luaTargetTranspiler = new LuaTranspiler51(checker, options, sourceFile);
20+
break;
21+
case LuaTarget.Lua52:
22+
luaTargetTranspiler = new LuaTranspiler52(checker, options, sourceFile);
23+
break;
24+
case LuaTarget.Lua53:
25+
luaTargetTranspiler = new LuaTranspiler53(checker, options, sourceFile);
26+
break;
27+
default:
28+
luaTargetTranspiler = new LuaTranspilerJIT(checker, options, sourceFile);
29+
break;
30+
}
31+
32+
return luaTargetTranspiler;
33+
}

src/tstl.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
export {
2+
parseCommandLine
3+
} from "./CommandLineParser";
4+
5+
export {
6+
CompilerOptions
7+
} from "./CompilerOptions";
8+
9+
export {
10+
compile,
11+
compileFilesWithOptions,
12+
transpileFile,
13+
transpileString,
14+
watchWithOptions
15+
} from "./Compiler";
16+
17+
export {LuaTranspiler51} from "./targets/Transpiler.51";
18+
export {LuaTranspiler52} from "./targets/Transpiler.52";
19+
export {LuaTranspiler53} from "./targets/Transpiler.53";
20+
export {LuaTranspilerJIT} from "./targets/Transpiler.JIT";
21+
22+
export {
23+
LuaLibFeature,
24+
LuaLibImportKind,
25+
LuaTarget,
26+
LuaTranspiler,
27+
} from "./Transpiler";
28+
29+
export {
30+
createTranspiler
31+
} from "./TranspilerFactory";

test/src/util.ts

Lines changed: 9 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -3,62 +3,15 @@ import * as ts from "typescript";
33

44
import { Expect } from "alsatian";
55

6-
import { CompilerOptions } from "../../src/CommandLineParser";
7-
import { createTranspiler } from "../../src/Compiler";
8-
import { LuaLibImportKind, LuaTarget, LuaTranspiler } from "../../src/Transpiler";
6+
import { transpileString } from "../../src/Compiler";
7+
import { LuaTarget, LuaTranspiler } from "../../src/Transpiler";
8+
import { createTranspiler } from "../../src/TranspilerFactory";
99

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

12-
const fs = require("fs");
13-
14-
const libSource = fs.readFileSync(path.join(path.dirname(require.resolve("typescript")), "lib.es6.d.ts")).toString();
15-
16-
export function transpileString(str: string, options: CompilerOptions = { luaLibImport: LuaLibImportKind.Require, luaTarget: LuaTarget.Lua53 }): string {
17-
const compilerHost = {
18-
directoryExists: () => true,
19-
fileExists: (fileName): boolean => true,
20-
getCanonicalFileName: fileName => fileName,
21-
getCurrentDirectory: () => "",
22-
getDefaultLibFileName: () => "lib.es6.d.ts",
23-
getDirectories: () => [],
24-
getNewLine: () => "\n",
25-
26-
getSourceFile: (filename, languageVersion) => {
27-
if (filename === "file.ts") {
28-
return ts.createSourceFile(filename, str, ts.ScriptTarget.Latest, false);
29-
}
30-
if (filename === "lib.es6.d.ts") {
31-
return ts.createSourceFile(filename, libSource, ts.ScriptTarget.Latest, false);
32-
}
33-
return undefined;
34-
},
35-
36-
readFile: () => "",
37-
38-
useCaseSensitiveFileNames: () => false,
39-
// Don't write output
40-
writeFile: (name, text, writeByteOrderMark) => null,
41-
};
42-
const program = ts.createProgram(["file.ts"], options, compilerHost);
43-
44-
const result = createTranspiler(program.getTypeChecker(),
45-
options,
46-
program.getSourceFile("file.ts")).transpileSourceFile();
47-
return result.trim();
48-
}
49-
50-
export function transpileFile(filePath: string): string {
51-
const program = ts.createProgram([filePath], {});
52-
const checker = program.getTypeChecker();
12+
import * as fs from "fs";
5313

54-
// Output errors
55-
const diagnostics = ts.getPreEmitDiagnostics(program).filter(diag => diag.code !== 6054);
56-
diagnostics.forEach(diagnostic => console.log(`${ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n")}`));
57-
58-
const options: ts.CompilerOptions = { luaLibImport: "none" };
59-
const result = createTranspiler(checker, options, program.getSourceFile(filePath)).transpileSourceFile();
60-
return result.trim();
61-
}
14+
export { transpileString };
6215

6316
export function executeLua(luaStr: string, withLib = true): any {
6417
if (withLib) {
@@ -91,7 +44,7 @@ export function executeLua(luaStr: string, withLib = true): any {
9144
}
9245
}
9346

94-
export function expectCodeEqual(code1: string, code2: string) {
47+
export function expectCodeEqual(code1: string, code2: string): void {
9548
// Trim leading/trailing whitespace
9649
let c1 = code1.trim();
9750
let c2 = code2.trim();
@@ -104,14 +57,14 @@ export function expectCodeEqual(code1: string, code2: string) {
10457
}
10558

10659
// Get a mock transpiler to use for testing
107-
export function makeTestTranspiler(target: LuaTarget = LuaTarget.Lua53) {
60+
export function makeTestTranspiler(target: LuaTarget = LuaTarget.Lua53): LuaTranspiler {
10861
return createTranspiler({} as ts.TypeChecker,
10962
{ luaLibImport: "none", luaTarget: target } as any,
11063
{ statements: [] } as any as ts.SourceFile);
11164
}
11265

113-
export function transpileAndExecute(ts: string): any {
114-
return executeLua(transpileString(ts));
66+
export function transpileAndExecute(tsStr: string): any {
67+
return executeLua(transpileString(tsStr));
11568
}
11669

11770
const jsonlib = fs.readFileSync("test/src/json.lua") + "\n";

test/unit/cli.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { Expect, Test, TestCase, Teardown } from "alsatian";
1+
import { Expect, Test, TestCase } from "alsatian";
22

3-
import { CompilerOptions, findConfigFile, parseCommandLine, ParsedCommandLine } from "../../src/CommandLineParser";
3+
import { findConfigFile, parseCommandLine } from "../../src/CommandLineParser";
44

55
export class CLITests {
66

0 commit comments

Comments
 (0)