Skip to content

Commit 8a63a99

Browse files
committed
Added import options
1 parent 548d16f commit 8a63a99

9 files changed

Lines changed: 41 additions & 23 deletions

File tree

build_lualib.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import * as glob from "glob";
33
import {compile} from "./src/Compiler";
44

55
compile([
6-
"-ah",
7-
"--dontRequireLuaLib",
6+
"--luaLibImport",
7+
"none",
88
"--luaTarget",
99
"5.1",
1010
"--outDir",

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"scripts": {
1313
"build": "tsc -p tsconfig.json && npm run build-lualib",
1414
"build-lualib": "ts-node ./build_lualib.ts",
15-
"test": "tslint -p . && npm run build && ts-node ./test/runner.ts",
15+
"test": "tslint -p . && ts-node ./test/runner.ts",
1616
"coverage": "nyc npm test && nyc report --reporter=text-lcov > coverage.lcov",
1717
"coverage-html": "nyc npm test && nyc report --reporter=html",
1818
"test-threaded": "tslint -p . && npm run build && ts-node ./test/threaded_runner.ts",

src/CommandLineParser.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as yargs from "yargs";
66
export interface CompilerOptions extends ts.CompilerOptions {
77
addHeader?: boolean;
88
luaTarget?: string;
9-
dontRequireLuaLib?: boolean;
9+
luaLibImport?: string;
1010
}
1111

1212
export interface ParsedCommandLine extends ts.ParsedCommandLine {
@@ -24,10 +24,11 @@ const optionDeclarations: { [key: string]: yargs.Options } = {
2424
describe: "Specify if a header will be added to compiled files.",
2525
type: "boolean",
2626
},
27-
dontRequireLuaLib: {
28-
default: false,
29-
describe: "Dont require lua library that enables advanced Typescipt/JS functionality.",
30-
type: "boolean",
27+
luaLibImport: {
28+
choices: ["inline", "require", "none"],
29+
default: "inline",
30+
describe: "Specify Lua target version.",
31+
type: "string",
3132
},
3233
luaTarget: {
3334
alias: "lt",
@@ -117,6 +118,9 @@ function addTSTLOptions(commandLine: ts.ParsedCommandLine,
117118

118119
/** Check the current state of the ParsedCommandLine for errors */
119120
function runDiagnostics(commandLine: ts.ParsedCommandLine) {
121+
// Remove files that dont exist
122+
commandLine.fileNames = commandLine.fileNames.filter(file => fs.existsSync(file) || fs.existsSync(file + ".ts"));
123+
120124
const tsInvalidCompilerOptionErrorCode = 5023;
121125
if (commandLine.errors.length !== 0) {
122126
// Generate a list of valid option names and aliases

src/Compiler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { LuaTranspiler51 } from "./targets/Transpiler.51";
77
import { LuaTranspiler52 } from "./targets/Transpiler.52";
88
import { LuaTranspiler53 } from "./targets/Transpiler.53";
99
import { LuaTranspilerJIT } from "./targets/Transpiler.JIT";
10-
import { LuaTarget, LuaTranspiler, TranspileError } from "./Transpiler";
10+
import { LuaLibImportKind, LuaTarget, LuaTranspiler, TranspileError } from "./Transpiler";
1111

1212
export function compile(argv: string[]) {
1313
const commandLine = parseCommandLine(argv);
@@ -97,7 +97,7 @@ export function compileFilesWithOptions(fileNames: string[], options: CompilerOp
9797
});
9898

9999
// Copy lualib to target dir
100-
if (!options.dontRequireLuaLib) {
100+
if (options.luaLibImport === LuaLibImportKind.Require) {
101101
fs.copyFileSync(
102102
path.resolve(__dirname, "../dist/lualib/lualib_bundle.lua"),
103103
path.join(options.outDir, "lualib_bundle.lua")

src/Transpiler.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as ts from "typescript";
33
import { CompilerOptions } from "./CommandLineParser";
44
import { TSHelper as tsHelper } from "./TSHelper";
55

6+
import * as fs from "fs";
67
import * as path from "path";
78

89
/* tslint:disable */
@@ -42,6 +43,12 @@ export enum LuaLibFeature {
4243
Ternary = "Ternary",
4344
}
4445

46+
export enum LuaLibImportKind {
47+
Inline = "inline",
48+
Require = "require",
49+
None = "none",
50+
}
51+
4552
interface ExportInfo {
4653
name: string;
4754
node: ts.Node;
@@ -171,9 +178,9 @@ export abstract class LuaTranspiler {
171178
"-- https://github.com/Perryvw/TypescriptToLua\n";
172179
}
173180
let result = header;
174-
if (!this.options.dontRequireLuaLib) {
181+
if (this.options.luaLibImport === LuaLibImportKind.Require) {
175182
// require helper functions
176-
result += `require("typescript_lualib")\n`;
183+
result += `require("lualib_bundle")\n`;
177184
}
178185
if (this.isModule) {
179186
// Shadow exports if it already exists
@@ -188,6 +195,13 @@ export abstract class LuaTranspiler {
188195
if (this.isModule) {
189196
result += "return exports\n";
190197
}
198+
199+
if (this.options.luaLibImport === LuaLibImportKind.Inline) {
200+
for (const feature of this.luaLibFeatureSet) {
201+
const featureFile = path.resolve(__dirname, `../dist/lualib/${feature}.lua`);
202+
result += fs.readFileSync(featureFile) + "\n";
203+
}
204+
}
191205
return result;
192206
}
193207

test/src/util.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const fs = require("fs");
1313

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

16-
export function transpileString(str: string, options: CompilerOptions = { dontRequireLuaLib: true, luaTarget: LuaTarget.Lua53 }): string {
16+
export function transpileString(str: string, options: CompilerOptions = { luaLibImport: "require", luaTarget: LuaTarget.Lua53 }): string {
1717
const compilerHost = {
1818
directoryExists: () => true,
1919
fileExists: (fileName): boolean => true,
@@ -55,7 +55,7 @@ export function transpileFile(filePath: string): string {
5555
const diagnostics = ts.getPreEmitDiagnostics(program).filter(diag => diag.code !== 6054);
5656
diagnostics.forEach(diagnostic => console.log(`${ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n")}`));
5757

58-
const options: ts.CompilerOptions = { dontRequireLuaLib: true };
58+
const options: ts.CompilerOptions = { luaLibImport: "none" };
5959
const result = createTranspiler(checker, options, program.getSourceFile(filePath)).transpileSourceFile();
6060
return result.trim();
6161
}
@@ -106,7 +106,7 @@ export function expectCodeEqual(code1: string, code2: string) {
106106
// Get a mock transpiler to use for testing
107107
export function makeTestTranspiler(target: LuaTarget = LuaTarget.Lua53) {
108108
return createTranspiler({} as ts.TypeChecker,
109-
{ dontRequireLuaLib: true, luaTarget: target } as any,
109+
{ luaLibImport: "none", luaTarget: target } as any,
110110
{ statements: [] } as any as ts.SourceFile);
111111
}
112112

test/unit/cli.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export class CLITests {
77
@Test("defaultOption")
88
@TestCase("luaTarget", "JIT")
99
@TestCase("addHeader", true)
10-
@TestCase("dontRequireLuaLib", false)
10+
@TestCase("luaLibImport", "inline")
1111
@TestCase("rootDir", process.cwd())
1212
@TestCase("outDir", process.cwd())
1313
public defaultOptions(option: any, expected: any) {

test/unit/expressions.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ export class ExpressionTests {
107107
@Test("Bitop [5.1]")
108108
public bitOperatorOverride51(input: string, lua: string) {
109109
// Bit operations not supported in 5.1, expect an exception
110-
Expect(() => util.transpileString(input, { luaTarget: LuaTarget.Lua51, dontRequireLuaLib: true }))
110+
Expect(() => util.transpileString(input, { luaTarget: LuaTarget.Lua51, luaLibImport: "none" }))
111111
.toThrow();
112112
}
113113

@@ -126,7 +126,7 @@ export class ExpressionTests {
126126
@TestCase("a>>>=b", "a = bit.arshift(a,b)")
127127
@Test("Bitop [JIT]")
128128
public bitOperatorOverrideJIT(input: string, lua: string) {
129-
Expect(util.transpileString(input, { luaTarget: LuaTarget.LuaJIT, dontRequireLuaLib: true })).toBe(lua);
129+
Expect(util.transpileString(input, { luaTarget: LuaTarget.LuaJIT, luaLibImport: "none" })).toBe(lua);
130130
}
131131

132132
@TestCase("~a", "bit32.bnot(a)")
@@ -144,7 +144,7 @@ export class ExpressionTests {
144144
@TestCase("a>>>=b", "a = bit32.arshift(a,b)")
145145
@Test("Bitop [5.2]")
146146
public bitOperatorOverride52(input: string, lua: string) {
147-
Expect(util.transpileString(input, { luaTarget: LuaTarget.Lua52, dontRequireLuaLib: true })).toBe(lua);
147+
Expect(util.transpileString(input, { luaTarget: LuaTarget.Lua52, luaLibImport: "none" })).toBe(lua);
148148
}
149149

150150
@TestCase("~a", "~a")
@@ -160,14 +160,14 @@ export class ExpressionTests {
160160
@TestCase("a>>=b", "a = a >> b")
161161
@Test("Bitop [5.3]")
162162
public bitOperatorOverride53(input: string, lua: string) {
163-
Expect(util.transpileString(input, { luaTarget: LuaTarget.Lua53, dontRequireLuaLib: true })).toBe(lua);
163+
Expect(util.transpileString(input, { luaTarget: LuaTarget.Lua53, luaLibImport: "none" })).toBe(lua);
164164
}
165165

166166
@TestCase("a>>>b")
167167
@TestCase("a>>>=b")
168168
@Test("Unsupported bitop 5.3")
169169
public bitOperatorOverride53Unsupported(input: string) {
170-
Expect(() => util.transpileString(input, { luaTarget: LuaTarget.Lua53, dontRequireLuaLib: true }))
170+
Expect(() => util.transpileString(input, { luaTarget: LuaTarget.Lua53, luaLibImport: "none" }))
171171
.toThrowError(Error, "Bitwise operator >>> not supported in Lua 5.3");
172172
}
173173

test/unit/modules.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ export class LuaModuleTests {
1515
@Test("lualibRequire")
1616
public lualibRequire() {
1717
// Transpile
18-
const lua = util.transpileString(``, {dontRequireLuaLib: false, luaTarget: "JIT"});
18+
const lua = util.transpileString(``, {luaLibImport: "require", luaTarget: "JIT"});
1919

2020
// Assert
21-
Expect(lua).toBe(`require("typescript_lualib")`);
21+
Expect(lua).toBe(`require("lualib_bundle")`);
2222
}
2323

2424
@Test("Import named bindings exception")

0 commit comments

Comments
 (0)