Skip to content

Commit 1866425

Browse files
authored
Improve printer plugin example (#85)
* Improve printer plugin example * Remove eslint comment
1 parent aab2763 commit 1866425

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

docs/api/plugins.md

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,22 +80,30 @@ export default plugin;
8080
Example:
8181

8282
```ts
83+
import { SourceNode } from "source-map";
8384
import * as ts from "typescript";
8485
import * as tstl from "typescript-to-lua";
8586

8687
const CUSTOM_COMMENT_HEADER = "-- This code was generated with a custom plugin!\n";
8788

88-
class CustomLuaPrinter extends tstl.LuaPrinter {
89-
printCustom(file: tstl.File) {
90-
const printResult = this.print(file);
91-
printResult.code = CUSTOM_COMMENT_HEADER + printResult.code;
92-
return printResult;
89+
class CustomPrinter extends tstl.LuaPrinter {
90+
/* Override printFile */
91+
protected printFile(file: tstl.File): SourceNode {
92+
const originalResult = super.printFile(file);
93+
// Add header comment at the top of the file
94+
return this.createSourceNode(file, [`${CUSTOM_COMMENT_HEADER} ${this.luaFile}\n`, originalResult]);
95+
}
96+
97+
/* Override printBoolean */
98+
public printBooleanLiteral(expression: tstl.BooleanLiteral): SourceNode {
99+
// Print any boolean as 'true'
100+
return this.createSourceNode(expression, "true");
93101
}
94102
}
95103

96104
const plugin: tstl.Plugin = {
97105
printer: (program: ts.Program, emitHost: tstl.EmitHost, fileName: string, file: tstl.File) =>
98-
new CustomLuaPrinter(emitHost, program, fileName).printCustom(file),
106+
new CustomPrinter(emitHost, program, fileName).print(file),
99107
};
100108

101109
export default plugin;

0 commit comments

Comments
 (0)