Skip to content

Commit 34a87a1

Browse files
authored
Added file header and lualib loading to printer (#327)
1 parent 273a54b commit 34a87a1

File tree

4 files changed

+43
-29
lines changed

4 files changed

+43
-29
lines changed

src/LuaPrinter.ts

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import * as tstl from "./LuaAST";
22

3-
import {TSHelper as tsHelper} from "./TSHelper";
3+
import { TSHelper as tsHelper } from "./TSHelper";
4+
import { LuaLibFeature, LuaLib } from "./LuaLib";
5+
import { CompilerOptions } from "./CompilerOptions";
6+
import { LuaLibImportKind } from "./CompilerOptions";
47

58
export class LuaPrinter {
69
/* tslint:disable:object-literal-sort-keys */
@@ -33,14 +36,37 @@ export class LuaPrinter {
3336
};
3437
/* tslint:enable:object-literal-sort-keys */
3538

39+
private options: CompilerOptions;
3640
private currentIndent: string;
3741

38-
public constructor() {
42+
public constructor(options: CompilerOptions) {
43+
this.options = options;
3944
this.currentIndent = "";
4045
}
4146

42-
public print(block: tstl.Block): string {
43-
return this.printBlock(block);
47+
public print(block: tstl.Block, luaLibFeatures?: Set<LuaLibFeature>): string {
48+
let header = "";
49+
50+
if (this.options.addHeader === undefined || this.options.addHeader === true) {
51+
header += `--[[ Generated with https://github.com/Perryvw/TypescriptToLua ]]\n`;
52+
}
53+
54+
if (luaLibFeatures) {
55+
// Require lualib bundle
56+
if ((this.options.luaLibImport === LuaLibImportKind.Require && luaLibFeatures.size > 0)
57+
|| this.options.luaLibImport === LuaLibImportKind.Always)
58+
{
59+
header += `require("lualib_bundle");\n`;
60+
}
61+
// Inline lualib features
62+
else if (this.options.luaLibImport === LuaLibImportKind.Inline && luaLibFeatures.size > 0)
63+
{
64+
header += "-- Lua Library inline imports\n";
65+
header += LuaLib.loadFeatures(luaLibFeatures);
66+
}
67+
}
68+
69+
return header + this.printBlock(block);
4470
}
4571

4672
private pushIndent(): void {

src/LuaTransformer.ts

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -72,32 +72,15 @@ export class LuaTransformer {
7272
}
7373

7474
// TODO make all other methods private???
75-
public transformSourceFile(node: ts.SourceFile): tstl.Block {
75+
public transformSourceFile(node: ts.SourceFile): [tstl.Block, Set<LuaLibFeature>] {
7676
this.setupState();
7777

7878
this.currentSourceFile = node;
7979
this.isModule = tsHelper.isFileModule(node);
8080

8181
const statements = this.transformStatements(node.statements);
8282

83-
const luaLibStatements = [];
84-
if ((this.options.luaLibImport === LuaLibImportKind.Require && this.luaLibFeatureSet.size > 0)
85-
|| this.options.luaLibImport === LuaLibImportKind.Always) {
86-
// require helper functions
87-
const requireStatement = tstl.createExpressionStatement((tstl.createCallExpression(
88-
tstl.createIdentifier("require"),
89-
[tstl.createStringLiteral("lualib_bundle")]
90-
)));
91-
luaLibStatements.push(requireStatement);
92-
}
93-
94-
// Inline lualib features
95-
if (this.options.luaLibImport === LuaLibImportKind.Inline && this.luaLibFeatureSet.size > 0) {
96-
//result += "\n" + "-- Lua Library Imports\n";
97-
luaLibStatements.push(tstl.createIdentifier(LuaLib.loadFeatures(this.luaLibFeatureSet)));
98-
}
99-
100-
return tstl.createBlock(luaLibStatements.concat(statements), undefined, node);
83+
return [tstl.createBlock(statements, undefined, node), this.luaLibFeatureSet];
10184
}
10285

10386
public transformStatement(node: ts.Statement): StatementVisitResult {

src/LuaTranspiler.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export class LuaTranspiler {
2020
this.program = program;
2121
this.options = this.program.getCompilerOptions() as CompilerOptions;
2222
this.luaTransformer = createTransformer(this.program);
23-
this.luaPrinter = new LuaPrinter();
23+
this.luaPrinter = new LuaPrinter(this.options);
2424
}
2525

2626
private reportErrors(): number {
@@ -102,9 +102,9 @@ export class LuaTranspiler {
102102

103103
public transpileSourceFile(sourceFile: ts.SourceFile): string {
104104
// Transform AST
105-
const luaAST = this.luaTransformer.transformSourceFile(sourceFile);
105+
const [luaAST, lualibFeatureSet] = this.luaTransformer.transformSourceFile(sourceFile);
106106
// Print AST
107-
return this.luaPrinter.print(luaAST);
107+
return this.luaPrinter.print(luaAST, lualibFeatureSet);
108108
}
109109

110110
public reportDiagnostic(diagnostic: ts.Diagnostic): void {

test/src/util.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,21 @@ import * as ts from "typescript";
33

44
import { Expect } from "alsatian";
55

6-
import { transpileString } from "../../src/Compiler";
7-
import { LuaTarget } from "../../src/CompilerOptions";
6+
import { transpileString as compilerTranspileString } from "../../src/Compiler";
7+
import { CompilerOptions, LuaTarget, LuaLibImportKind } from "../../src/CompilerOptions";
88
import { createTransformer } from "../../src/TransformerFactory";
99

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

1212
import * as fs from "fs";
1313
import { LuaTransformer } from "../../src/LuaTransformer";
1414

15-
export { transpileString };
15+
export function transpileString(str: string, options?: CompilerOptions): string {
16+
if (options && options.addHeader === undefined) {
17+
options.addHeader = false;
18+
}
19+
return compilerTranspileString(str, options);
20+
}
1621

1722
export function executeLua(luaStr: string, withLib = true): any {
1823
if (withLib) {

0 commit comments

Comments
 (0)