Skip to content

Commit 8566b79

Browse files
Add luaLibDir compiler option
1 parent d497824 commit 8566b79

File tree

2 files changed

+55
-23
lines changed

2 files changed

+55
-23
lines changed

src/Compiler.ts

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ import dedent = require("dedent")
1111
import { LuaTranspiler, TranspileError } from "./Transpiler";
1212
import { TSHelper as tsEx } from "./TSHelper";
1313

14-
function compile(fileNames: string[], options: ts.CompilerOptions): void {
14+
interface CompilerOptions extends ts.CompilerOptions {
15+
addHeader?: boolean;
16+
luaLibDir?: string;
17+
}
18+
19+
function compile(fileNames: string[], options: CompilerOptions): void {
1520
let program = ts.createProgram(fileNames, options);
1621
let checker = program.getTypeChecker();
1722

@@ -42,9 +47,20 @@ function compile(fileNames: string[], options: ts.CompilerOptions): void {
4247
options.outDir = options.rootDir;
4348
}
4449

50+
if (!('addHeader' in options)) {
51+
options.addHeader = true;
52+
}
53+
54+
if (!options.luaLibDir) {
55+
options.luaLibDir = options.outDir;
56+
}
57+
else {
58+
options.luaLibDir = path.join(options.outDir, options.luaLibDir);
59+
}
60+
4561
// Copy lualib to target dir
4662
// This isnt run in sync because copyFileSync wont report errors.
47-
fs.copyFile(path.resolve(__dirname, "../dist/lualib/typescript.lua"), path.join(options.outDir, "typescript_lualib.lua"), (err: NodeJS.ErrnoException) => {
63+
fs.copyFile(path.resolve(__dirname, "../dist/lualib/typescript.lua"), path.join(options.luaLibDir, "typescript_lualib.lua"), (err: NodeJS.ErrnoException) => {
4864
if (err) {
4965
console.log("ERROR: copying lualib to output.");
5066
}
@@ -64,7 +80,7 @@ function compile(fileNames: string[], options: ts.CompilerOptions): void {
6480
}
6581

6682
// change extension
67-
var fileNameLua = path.basename(outPath, path.extname(outPath)) + '.lua';
83+
const fileNameLua = path.basename(outPath, path.extname(outPath)) + '.lua';
6884
outPath = path.join(path.dirname(outPath), fileNameLua);
6985

7086
// Write output
@@ -97,7 +113,7 @@ function printAST(node: ts.Node, indent: number) {
97113

98114
// Removes the option and value form argv
99115
function removeInvalidOptionsFromArgv(commandLine: ReadonlyArray<string>, invalidOptions: ReadonlyArray<string>): ReadonlyArray<string> {
100-
let result = commandLine.slice()
116+
let result = commandLine.slice();
101117
for (let opt of invalidOptions) {
102118
let index = result.indexOf('--' + opt);
103119
if (index === -1) {
@@ -113,32 +129,41 @@ function removeInvalidOptionsFromArgv(commandLine: ReadonlyArray<string>, invali
113129
// Polyfill for report diagnostics
114130
function logError(commandLine: ts.ParsedCommandLine) {
115131
if (commandLine.errors.length !== 0) {
132+
let invalidErrorCount = 0;
116133
commandLine.errors.forEach((err) => {
117-
console.log(err.messageText);
134+
// Allow all compiler options
135+
if (err.code == 5023) {
136+
invalidErrorCount += 1;
137+
}
138+
else {
139+
console.log(err.messageText);
140+
}
118141
});
119-
process.exit(1);
142+
if (invalidErrorCount != commandLine.errors.length) {
143+
process.exit(1);
144+
}
120145
}
121146
}
122147

123148
function executeCommandLine(args: ReadonlyArray<string>) {
124149
// Right now luaTarget, version and help are the only cli options, if more are added we should
125150
// add a more advanced custom parser
126-
var argv = minimist(args, {
151+
const argv = minimist(args, {
127152
string: ['l'],
128153
alias: { h: 'help', v: 'version', l: 'luaTarget' },
129154
default: { luaTarget: 'JIT' },
130155
'--': true,
131156
stopEarly: true,
132157
});
133158

134-
let tstlVersion
159+
let tstlVersion;
135160
try {
136161
tstlVersion = require('../package').version;
137162
} catch (e) {
138163
tstlVersion = "0.0.0";
139164
}
140165

141-
var helpString = dedent(`Version: ${tstlVersion}
166+
const helpString = dedent(`Version: ${tstlVersion}
142167
Syntax: tstl [options] [files...]
143168
144169
In addtion to the options listed below you can also pass options for the
@@ -165,7 +190,7 @@ function executeCommandLine(args: ReadonlyArray<string>) {
165190
}
166191

167192
// Remove tstl options otherwise ts will emit an error
168-
let sanitizedArgs = removeInvalidOptionsFromArgv(args, ['l', 'luaTarget'])
193+
let sanitizedArgs = removeInvalidOptionsFromArgv(args, ['l', 'luaTarget']);
169194

170195
let commandLine = ts.parseCommandLine(sanitizedArgs);
171196

@@ -207,6 +232,13 @@ function executeCommandLine(args: ReadonlyArray<string>) {
207232
if (argv.luaTarget === "JIT" && commandLine.raw.luaTarget !== argv.luaTarget) {
208233
commandLine.options.luaTarget = commandLine.raw.luaTarget;
209234
}
235+
236+
// Add ignored compiler options
237+
for (const compilerOption in commandLine.raw.compilerOptions) {
238+
if (!(compilerOption in commandLine.options)) {
239+
commandLine.options[compilerOption] = commandLine.raw.compilerOptions[compilerOption];
240+
}
241+
}
210242
}
211243

212244
if (configPath && !commandLine.options.rootDir) {

src/Transpiler.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export class LuaTranspiler {
2525
static transpileSourceFile(node: ts.SourceFile, checker: ts.TypeChecker, options: ts.CompilerOptions): string {
2626
let transpiler = new LuaTranspiler(checker, options, node);
2727

28-
let header = options.addHeader ? "--=======================================================================================\n"
28+
const header = options.addHeader ? "--=======================================================================================\n"
2929
+ "-- Generated by TypescriptToLua transpiler https://github.com/Perryvw/TypescriptToLua \n"
3030
+ "-- Date: " + new Date().toDateString() + "\n"
3131
+ "--=======================================================================================\n"
@@ -48,7 +48,7 @@ export class LuaTranspiler {
4848

4949
indent: string;
5050
checker: ts.TypeChecker;
51-
options: ts.CompilerOptions
51+
options: ts.CompilerOptions;
5252
genVarCounter: number;
5353
transpilingSwitch: boolean;
5454
namespace: string[];
@@ -189,8 +189,8 @@ export class LuaTranspiler {
189189
const imports = node.importClause.namedBindings;
190190

191191
if (ts.isNamedImports(imports)) {
192-
let fileImportTable = path.basename(importPathWithoutQuotes) + this.importCount
193-
let result = `local ${fileImportTable} = require(${this.getImportPath(importPathWithoutQuotes)})\n`
192+
const fileImportTable = path.basename(importPathWithoutQuotes) + this.importCount;
193+
let result = `local ${fileImportTable} = require(${this.getImportPath(importPathWithoutQuotes)})\n`;
194194
this.importCount++;
195195
imports.elements.forEach(element => {
196196
if (element.propertyName) {
@@ -212,19 +212,19 @@ export class LuaTranspiler {
212212
if (tsEx.isPhantom(this.checker.getTypeAtLocation(node), this.checker)) return this.transpileNode(node.body);
213213

214214
const defName = this.definitionName(node.name.text);
215-
let result = this.indent + this.accessPrefix(node) + `${node.name.text} = ${node.name.text} or {}\n`
215+
let result = this.indent + this.accessPrefix(node) + `${node.name.text} = ${node.name.text} or {}\n`;
216216
if (this.namespace.length > 0) {
217217
result += this.indent + `${defName} = ${node.name.text} or {}\n`;
218218
}
219-
result += this.makeExport(defName, node)
219+
result += this.makeExport(defName, node);
220220
// Create closure
221-
result += this.indent + "do\n"
221+
result += this.indent + "do\n";
222222
this.pushIndent();
223223
this.namespace.push(node.name.text);
224224
result += this.transpileNode(node.body);
225225
this.namespace.pop();
226226
this.popIndent();
227-
result += this.indent + "end\n"
227+
result += this.indent + "end\n";
228228
return result;
229229
}
230230

@@ -443,15 +443,15 @@ export class LuaTranspiler {
443443
}
444444

445445
transpileTry(node: ts.TryStatement): string {
446-
let tryFunc = "function()\n"
446+
let tryFunc = "function()\n";
447447
this.pushIndent();
448448
tryFunc += this.transpileBlock(node.tryBlock);
449449
this.popIndent();
450450
tryFunc += "end";
451451
let catchFunc = "function(e)\nend";
452452
if (node.catchClause) {
453453
let variableName = (<ts.Identifier>node.catchClause.variableDeclaration.name).escapedText;
454-
catchFunc = this.indent + `function(${variableName})\n`
454+
catchFunc = this.indent + `function(${variableName})\n`;
455455
this.pushIndent();
456456
catchFunc += this.transpileBlock(node.catchClause.block);
457457
this.popIndent();
@@ -1074,7 +1074,7 @@ export class LuaTranspiler {
10741074
let result = "";
10751075
const identifier = node.name;
10761076
const methodName = identifier.escapedText;
1077-
const parameters = node.parameters
1077+
const parameters = node.parameters;
10781078
const body = node.body;
10791079

10801080
// Build parameter string
@@ -1144,7 +1144,7 @@ export class LuaTranspiler {
11441144
let result = "";
11451145

11461146
// Skip header if this is an extension class
1147-
var isExtension = tsEx.isExtensionClass(this.checker.getTypeAtLocation(node), this.checker);
1147+
const isExtension = tsEx.isExtensionClass(this.checker.getTypeAtLocation(node), this.checker);
11481148
if (!isExtension) {
11491149
// Write class declaration
11501150
const classOr = noClassOr ? "" : `${className} or `;
@@ -1153,7 +1153,7 @@ export class LuaTranspiler {
11531153
result += this.makeExport(className, node);
11541154
} else {
11551155
const baseName = (<ts.Identifier>extendsType.expression).escapedText;
1156-
result += this.indent + this.accessPrefix(node) + `${className} = ${classOr}${baseName}.new()\n`
1156+
result += this.indent + this.accessPrefix(node) + `${className} = ${classOr}${baseName}.new()\n`;
11571157
result += this.makeExport(className, node);
11581158
}
11591159
result += this.indent + `${className}.__index = ${className}\n`;

0 commit comments

Comments
 (0)