forked from TypeScriptToLua/TypeScriptToLua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransformers.ts
More file actions
179 lines (151 loc) · 6.72 KB
/
transformers.ts
File metadata and controls
179 lines (151 loc) · 6.72 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import * as ts from "typescript";
// TODO: Don't depend on CLI?
import * as cliDiagnostics from "../cli/diagnostics";
import { CompilerOptions, TransformerImport } from "../CompilerOptions";
import * as diagnosticFactories from "./diagnostics";
import { getConfigDirectory, resolvePlugin } from "./utils";
export function getTransformers(
program: ts.Program,
diagnostics: ts.Diagnostic[],
customTransformers: ts.CustomTransformers,
onSourceFile: (sourceFile: ts.SourceFile) => void
): ts.CustomTransformers {
const luaTransformer: ts.TransformerFactory<ts.SourceFile> = () => sourceFile => {
onSourceFile(sourceFile);
return ts.createSourceFile(sourceFile.fileName, "", ts.ScriptTarget.ESNext);
};
const transformersFromOptions = loadTransformersFromOptions(program, diagnostics);
const afterDeclarations = [
...(transformersFromOptions.afterDeclarations ?? []),
...(customTransformers.afterDeclarations ?? []),
];
const options = program.getCompilerOptions() as CompilerOptions;
if (options.noImplicitSelf) {
afterDeclarations.unshift(noImplicitSelfTransformer);
}
return {
afterDeclarations,
before: [
...(customTransformers.before ?? []),
...(transformersFromOptions.before ?? []),
...(transformersFromOptions.after ?? []),
...(customTransformers.after ?? []),
luaTransformer,
],
};
}
export const noImplicitSelfTransformer: ts.TransformerFactory<ts.SourceFile | ts.Bundle> = () => node => {
const transformSourceFile: ts.Transformer<ts.SourceFile> = node => {
const empty = ts.factory.createNotEmittedStatement(undefined!);
ts.addSyntheticLeadingComment(empty, ts.SyntaxKind.MultiLineCommentTrivia, "* @noSelfInFile ", true);
return ts.factory.updateSourceFile(node, [empty, ...node.statements], node.isDeclarationFile);
};
return ts.isBundle(node)
? ts.factory.updateBundle(node, node.sourceFiles.map(transformSourceFile))
: transformSourceFile(node);
};
function loadTransformersFromOptions(program: ts.Program, diagnostics: ts.Diagnostic[]): ts.CustomTransformers {
const customTransformers: Required<ts.CustomTransformers> = {
before: [],
after: [],
afterDeclarations: [],
};
const options = program.getCompilerOptions() as CompilerOptions;
if (!options.plugins) return customTransformers;
for (const [index, transformerImport] of options.plugins.entries()) {
if (!("transform" in transformerImport)) continue;
const optionName = `compilerOptions.plugins[${index}]`;
const { error: resolveError, result: factory } = resolvePlugin(
"transformer",
`${optionName}.transform`,
getConfigDirectory(options),
transformerImport.transform,
transformerImport.import
);
if (resolveError) diagnostics.push(resolveError);
if (factory === undefined) continue;
const { error: loadError, transformer } = loadTransformer(optionName, program, factory, transformerImport);
if (loadError) diagnostics.push(loadError);
if (transformer === undefined) continue;
if (transformer.before) {
customTransformers.before.push(transformer.before);
}
if (transformer.after) {
customTransformers.after.push(transformer.after);
}
if (transformer.afterDeclarations) {
customTransformers.afterDeclarations.push(transformer.afterDeclarations);
}
}
return customTransformers;
}
type ProgramTransformerFactory = (program: ts.Program, options: Record<string, any>) => Transformer;
type ConfigTransformerFactory = (options: Record<string, any>) => Transformer;
type CompilerOptionsTransformerFactory = (
compilerOptions: CompilerOptions,
options: Record<string, any>
) => Transformer;
type TypeCheckerTransformerFactory = (typeChecker: ts.TypeChecker, options: Record<string, any>) => Transformer;
type RawTransformerFactory = Transformer;
type Transformer = GroupTransformer | ts.TransformerFactory<ts.SourceFile>;
interface GroupTransformer {
before?: ts.TransformerFactory<ts.SourceFile>;
after?: ts.TransformerFactory<ts.SourceFile>;
afterDeclarations?: ts.TransformerFactory<ts.SourceFile | ts.Bundle>;
}
function loadTransformer(
optionPath: string,
program: ts.Program,
factory: unknown,
{ transform, after = false, afterDeclarations = false, type = "program", ...extraOptions }: TransformerImport
): { error?: ts.Diagnostic; transformer?: GroupTransformer } {
let transformer: Transformer;
switch (type) {
case "program":
transformer = (factory as ProgramTransformerFactory)(program, extraOptions);
break;
case "config":
transformer = (factory as ConfigTransformerFactory)(extraOptions);
break;
case "checker":
transformer = (factory as TypeCheckerTransformerFactory)(program.getTypeChecker(), extraOptions);
break;
case "raw":
transformer = factory as RawTransformerFactory;
break;
case "compilerOptions":
transformer = (factory as CompilerOptionsTransformerFactory)(program.getCompilerOptions(), extraOptions);
break;
default: {
const optionName = `--${optionPath}.type`;
return { error: cliDiagnostics.argumentForOptionMustBe(optionName, "program") };
}
}
if (typeof after !== "boolean") {
const optionName = `${optionPath}.after`;
return { error: cliDiagnostics.compilerOptionRequiresAValueOfType(optionName, "boolean") };
}
if (typeof afterDeclarations !== "boolean") {
const optionName = `${optionPath}.afterDeclarations`;
return { error: cliDiagnostics.compilerOptionRequiresAValueOfType(optionName, "boolean") };
}
if (typeof transformer === "function") {
let wrappedTransformer: GroupTransformer;
if (after) {
wrappedTransformer = { after: transformer };
} else if (afterDeclarations) {
wrappedTransformer = { afterDeclarations: transformer as ts.TransformerFactory<ts.SourceFile | ts.Bundle> };
} else {
wrappedTransformer = { before: transformer };
}
return { transformer: wrappedTransformer };
} else {
const isValidGroupTransformer =
typeof transformer === "object" &&
(transformer.before ?? transformer.after ?? transformer.afterDeclarations) !== undefined;
if (!isValidGroupTransformer) {
return { error: diagnosticFactories.transformerShouldBeATsTransformerFactory(transform) };
}
}
return { transformer };
}