Skip to content

Commit 25dd8c8

Browse files
authored
Make printer slightly easier to use, add better printer example as test (#1173)
1 parent 7dc3e92 commit 25dd8c8

File tree

3 files changed

+45
-10
lines changed

3 files changed

+45
-10
lines changed

src/LuaPrinter.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,9 @@ export class LuaPrinter {
158158
private static rightAssociativeOperators = new Set([lua.SyntaxKind.ConcatOperator, lua.SyntaxKind.PowerOperator]);
159159

160160
private currentIndent = "";
161-
private luaFile: string;
162-
private relativeSourcePath: string;
163-
private options: CompilerOptions;
161+
protected luaFile: string;
162+
protected relativeSourcePath: string;
163+
protected options: CompilerOptions;
164164

165165
public static readonly sourceMapTracebackPlaceholder = "{#SourceMapTraceback}";
166166

@@ -226,7 +226,7 @@ export class LuaPrinter {
226226
return `__TS__SourceMapTraceBack(debug.getinfo(1).short_src, ${mapString});`;
227227
}
228228

229-
private printFile(file: lua.File): SourceNode {
229+
protected printFile(file: lua.File): SourceNode {
230230
let header = file.trivia;
231231

232232
if (!this.options.noHeader) {

test/transpile/plugins/plugins.spec.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,30 @@ import * as util from "../../util";
44
test("printer", () => {
55
util.testModule``
66
.setOptions({ luaPlugins: [{ name: path.join(__dirname, "printer.ts") }] })
7-
.tap(builder => expect(builder.getMainLuaCodeChunk()).toMatch("Plugin"));
7+
.tap(builder => expect(builder.getMainLuaCodeChunk()).toMatch("-- Custom printer plugin:"));
8+
});
9+
10+
test("printer in bundle", () => {
11+
const { transpiledFiles } = util.testBundle`
12+
import "./otherfile";
13+
14+
const foo = "foo";
15+
`
16+
.addExtraFile(
17+
"otherfile.ts",
18+
`
19+
const bar = "bar";
20+
`
21+
)
22+
.setOptions({ luaPlugins: [{ name: path.join(__dirname, "printer.ts") }] })
23+
.expectToHaveNoDiagnostics()
24+
.getLuaResult();
25+
26+
expect(transpiledFiles).toHaveLength(1);
27+
const lua = transpiledFiles[0].lua!;
28+
29+
expect(lua).toContain("-- Custom printer plugin: main.lua");
30+
expect(lua).toContain("-- Custom printer plugin: otherfile.lua");
831
});
932

1033
test("visitor", () => {

test/transpile/plugins/printer.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
1+
import { SourceNode } from "source-map";
12
import * as tstl from "../../../src";
23

4+
class CustomPrinter extends tstl.LuaPrinter {
5+
/* Override printFile */
6+
protected printFile(file: tstl.File): SourceNode {
7+
const originalResult = super.printFile(file);
8+
// Add header comment at the top of the file
9+
return this.createSourceNode(file, [`-- Custom printer plugin: ${this.luaFile}\n`, originalResult]);
10+
}
11+
12+
/* Override printBoolean */
13+
public printBooleanLiteral(expression: tstl.BooleanLiteral): SourceNode {
14+
// Print any boolean as 'true'
15+
return this.createSourceNode(expression, "true");
16+
}
17+
}
18+
319
const plugin: tstl.Plugin = {
4-
printer(program, emitHost, fileName, ...args) {
5-
const result = new tstl.LuaPrinter(emitHost, program, fileName).print(...args);
6-
result.code = `-- Plugin\n${result.code}`;
7-
return result;
8-
},
20+
printer: (program, emitHost, fileName, file) => new CustomPrinter(emitHost, program, fileName).print(file),
921
};
1022

1123
// eslint-disable-next-line import/no-default-export

0 commit comments

Comments
 (0)