Skip to content

fix bad printer example #84

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 19, 2021
Merged
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
17 changes: 13 additions & 4 deletions docs/api/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,27 @@ export default plugin;

### `printer`

Printer is a function that overrides standard implementation of Lua AST printer. It receives some information about the file and transformed Lua AST. See [Printer](printer.md) page for more information.
`printer` is a function that overrides the standard implementation of the Lua AST printer. It receives some information about the file and the transformed Lua AST. See the [LuaPrinter](printer.md) page for more information.

Example:

```ts
import * as ts from "typescript";
import * as tstl from "typescript-to-lua";

class CustomLuaPrinter extends tstl.LuaPrinter {}
const CUSTOM_COMMENT_HEADER = "-- This code was generated with a custom plugin!\n";

class CustomLuaPrinter extends tstl.LuaPrinter {
printCustom(file: tstl.File) {
const printResult = this.print(file);
printResult.code = CUSTOM_COMMENT_HEADER + printResult.code;
return printResult;
}
}

const plugin: tstl.Plugin = {
printer: (program, emitHost, fileName, block, luaLibFeatures) =>
new CustomLuaPrinter(program.getCompilerOptions(), emitHost, fileName).print(block, luaLibFeatures),
printer: (program: ts.Program, emitHost: tstl.EmitHost, fileName: string, file: tstl.File) =>
new CustomLuaPrinter(emitHost, program, fileName).printCustom(file),
};

export default plugin;
Expand Down