-
-
Notifications
You must be signed in to change notification settings - Fork 184
Expand file tree
/
Copy pathplugins.ts
More file actions
110 lines (94 loc) · 3.67 KB
/
plugins.ts
File metadata and controls
110 lines (94 loc) · 3.67 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import * as ts from "typescript";
import { EmitHost } from "..";
import { CompilerOptions } from "../CompilerOptions";
import { Printer } from "../LuaPrinter";
import { Visitors } from "../transformation/context";
import { EmitFile, getConfigDirectory, ProcessedFile, resolvePlugin } from "./utils";
import * as performance from "../measure-performance";
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 translating the input program to Lua, but before resolving dependencies or bundling.
*/
afterPrint?: (
program: ts.Program,
options: CompilerOptions,
emitHost: EmitHost,
result: ProcessedFile[]
) => ts.Diagnostic[] | void;
/**
* This function is called after translating the input program to Lua, after resolving dependencies and after bundling.
*/
beforeEmit?: (
program: ts.Program,
options: CompilerOptions,
emitHost: EmitHost,
result: EmitFile[]
) => ts.Diagnostic[] | void;
/**
* This function is called after translating the input program to Lua, after resolving dependencies, after bundling and writing files to disk.
*/
afterEmit?: (
program: ts.Program,
options: CompilerOptions,
emitHost: EmitHost,
result: EmitFile[]
) => ts.Diagnostic[] | void;
/**
* This function is called when trying to resolve the .lua file corresponding to a Lua require statement. Allows you to provide
* your own module resolution logic. If return value is undefined, regular module resolution is done.
*/
moduleResolution?: (
moduleIdentifier: string,
requiringFile: string,
options: CompilerOptions,
emitHost: EmitHost
) => string | undefined;
}
export function getPlugins(program: ts.Program): { diagnostics: ts.Diagnostic[]; plugins: Plugin[] } {
performance.startSection("getPlugins");
const diagnostics: ts.Diagnostic[] = [];
const pluginsFromOptions: Plugin[] = [];
const options = program.getCompilerOptions() as CompilerOptions;
for (const [index, pluginOption] of (options.luaPlugins ?? []).entries()) {
const optionName = `tstl.luaPlugins[${index}]`;
const factory = (() => {
if ("plugin" in pluginOption) {
return pluginOption.plugin;
} else {
const { error: resolveError, result: factory } = resolvePlugin(
"plugin",
`${optionName}.name`,
getConfigDirectory(options),
pluginOption.name,
pluginOption.import
);
if (resolveError) diagnostics.push(resolveError);
return factory;
}
})();
if (factory === undefined) continue;
const plugin = typeof factory === "function" ? factory(pluginOption) : factory;
pluginsFromOptions.push(plugin);
}
if (options.tstlVerbose) {
console.log(`Loaded ${pluginsFromOptions.length} plugins`);
}
performance.endSection("getPlugins");
return { diagnostics, plugins: pluginsFromOptions };
}