Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/LuaPrinter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,9 @@ export class LuaPrinter {
private static rightAssociativeOperators = new Set([lua.SyntaxKind.ConcatOperator, lua.SyntaxKind.PowerOperator]);

private currentIndent = "";
private luaFile: string;
private relativeSourcePath: string;
private options: CompilerOptions;
protected luaFile: string;
protected relativeSourcePath: string;
protected options: CompilerOptions;

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

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

private printFile(file: lua.File): SourceNode {
protected printFile(file: lua.File): SourceNode {
let header = file.trivia;

if (!this.options.noHeader) {
Expand Down
25 changes: 24 additions & 1 deletion test/transpile/plugins/plugins.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,30 @@ import * as util from "../../util";
test("printer", () => {
util.testModule``
.setOptions({ luaPlugins: [{ name: path.join(__dirname, "printer.ts") }] })
.tap(builder => expect(builder.getMainLuaCodeChunk()).toMatch("Plugin"));
.tap(builder => expect(builder.getMainLuaCodeChunk()).toMatch("-- Custom printer plugin:"));
});

test("printer in bundle", () => {
const { transpiledFiles } = util.testBundle`
import "./otherfile";

const foo = "foo";
`
.addExtraFile(
"otherfile.ts",
`
const bar = "bar";
`
)
.setOptions({ luaPlugins: [{ name: path.join(__dirname, "printer.ts") }] })
.expectToHaveNoDiagnostics()
.getLuaResult();

expect(transpiledFiles).toHaveLength(1);
const lua = transpiledFiles[0].lua!;

expect(lua).toContain("-- Custom printer plugin: main.lua");
expect(lua).toContain("-- Custom printer plugin: otherfile.lua");
});

test("visitor", () => {
Expand Down
22 changes: 17 additions & 5 deletions test/transpile/plugins/printer.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
import { SourceNode } from "source-map";
import * as tstl from "../../../src";

class CustomPrinter extends tstl.LuaPrinter {
/* Override printFile */
protected printFile(file: tstl.File): SourceNode {
const originalResult = super.printFile(file);
// Add header comment at the top of the file
return this.createSourceNode(file, [`-- Custom printer plugin: ${this.luaFile}\n`, originalResult]);
}

/* Override printBoolean */
public printBooleanLiteral(expression: tstl.BooleanLiteral): SourceNode {
// Print any boolean as 'true'
return this.createSourceNode(expression, "true");
}
}

const plugin: tstl.Plugin = {
printer(program, emitHost, fileName, ...args) {
const result = new tstl.LuaPrinter(emitHost, program, fileName).print(...args);
result.code = `-- Plugin\n${result.code}`;
return result;
},
printer: (program, emitHost, fileName, file) => new CustomPrinter(emitHost, program, fileName).print(file),
};

// eslint-disable-next-line import/no-default-export
Expand Down