forked from TypeScriptToLua/TypeScriptToLua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugins.ts
More file actions
62 lines (52 loc) · 2.04 KB
/
plugins.ts
File metadata and controls
62 lines (52 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import * as ts from "typescript";
import { EmitHost } from "..";
import { CompilerOptions } from "../CompilerOptions";
import { Printer } from "../LuaPrinter";
import { Visitors } from "../transformation/context";
import { getConfigDirectory, ProcessedFile, resolvePlugin } from "./utils";
export interface Plugin {
/**
* An augmentation to the map of visitors that transform TypeScript AST to Lua AST.
*
* Key is a `SyntaxKind` of a processed node.
*/
visitors?: Visitors;
/**
* A function that converts Lua AST to a string.
*
* At most one custom printer can be provided across all plugins.
*/
printer?: Printer;
/**
* This function is called before transpilation of the TypeScript program starts.
*/
beforeTransform?: (program: ts.Program, options: CompilerOptions, emitHost: EmitHost) => ts.Diagnostic[] | void;
/**
* This function is called after TypeScriptToLua has translated the input program to Lua.
*/
afterPrint?: (
program: ts.Program,
options: CompilerOptions,
emitHost: EmitHost,
result: ProcessedFile[]
) => ts.Diagnostic[] | void;
}
export function getPlugins(program: ts.Program, diagnostics: ts.Diagnostic[], customPlugins: Plugin[]): Plugin[] {
const pluginsFromOptions: Plugin[] = [];
const options = program.getCompilerOptions() as CompilerOptions;
for (const [index, pluginOption] of (options.luaPlugins ?? []).entries()) {
const optionName = `tstl.luaPlugins[${index}]`;
const { error: resolveError, result: factory } = resolvePlugin(
"plugin",
`${optionName}.name`,
getConfigDirectory(options),
pluginOption.name,
pluginOption.import
);
if (resolveError) diagnostics.push(resolveError);
if (factory === undefined) continue;
const plugin = typeof factory === "function" ? factory(pluginOption) : factory;
pluginsFromOptions.push(plugin);
}
return [...customPlugins, ...pluginsFromOptions];
}