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
46 lines (38 loc) · 1.5 KB
/
plugins.ts
File metadata and controls
46 lines (38 loc) · 1.5 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
import * as ts from "typescript";
import { CompilerOptions } from "../CompilerOptions";
import { Printer } from "../LuaPrinter";
import { Visitors } from "../transformation/context";
import { getConfigDirectory, 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;
}
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];
}