forked from TypeScriptToLua/TypeScriptToLua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTranspile.ts
More file actions
148 lines (125 loc) · 5.21 KB
/
Transpile.ts
File metadata and controls
148 lines (125 loc) · 5.21 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
import * as ts from "typescript";
import { CompilerOptions } from "./CompilerOptions";
import * as diagnosticFactories from "./diagnostics";
import { Block } from "./LuaAST";
import { LuaPrinter } from "./LuaPrinter";
import { LuaTransformer } from "./LuaTransformer";
import { TranspileError } from "./TranspileError";
import { getCustomTransformers } from "./TSTransformers";
export interface TranspiledFile {
fileName: string;
luaAst?: Block;
lua?: string;
sourceMap?: string;
declaration?: string;
declarationMap?: string;
}
export interface TranspileResult {
diagnostics: ts.Diagnostic[];
transpiledFiles: TranspiledFile[];
}
export interface TranspileOptions {
program: ts.Program;
sourceFiles?: ts.SourceFile[];
customTransformers?: ts.CustomTransformers;
transformer?: LuaTransformer;
printer?: LuaPrinter;
emitHost?: EmitHost;
}
export interface EmitHost {
readFile(path: string): string | undefined;
}
export function transpile({
program,
sourceFiles: targetSourceFiles,
customTransformers = {},
emitHost = ts.sys,
transformer = new LuaTransformer(program),
printer = new LuaPrinter(program.getCompilerOptions(), emitHost),
}: TranspileOptions): TranspileResult {
const options = program.getCompilerOptions() as CompilerOptions;
const diagnostics: ts.Diagnostic[] = [];
let transpiledFiles: TranspiledFile[] = [];
const updateTranspiledFile = (fileName: string, update: Omit<TranspiledFile, "fileName">) => {
const file = transpiledFiles.find(f => f.fileName === fileName);
if (file) {
Object.assign(file, update);
} else {
transpiledFiles.push({ fileName, ...update });
}
};
if (options.noEmitOnError) {
const preEmitDiagnostics = [...program.getOptionsDiagnostics(), ...program.getGlobalDiagnostics()];
if (targetSourceFiles) {
for (const sourceFile of targetSourceFiles) {
preEmitDiagnostics.push(...program.getSyntacticDiagnostics(sourceFile));
preEmitDiagnostics.push(...program.getSemanticDiagnostics(sourceFile));
}
} else {
preEmitDiagnostics.push(...program.getSyntacticDiagnostics());
preEmitDiagnostics.push(...program.getSemanticDiagnostics());
}
if (preEmitDiagnostics.length === 0 && (options.declaration || options.composite)) {
preEmitDiagnostics.push(...program.getDeclarationDiagnostics());
}
if (preEmitDiagnostics.length > 0) {
return { diagnostics: preEmitDiagnostics, transpiledFiles };
}
}
const processSourceFile = (sourceFile: ts.SourceFile) => {
try {
const [luaAst, lualibFeatureSet] = transformer.transform(sourceFile);
if (!options.noEmit && !options.emitDeclarationOnly) {
const [lua, sourceMap] = printer.print(luaAst, lualibFeatureSet, sourceFile.fileName);
updateTranspiledFile(sourceFile.fileName, { luaAst, lua, sourceMap });
}
} catch (err) {
if (!(err instanceof TranspileError)) throw err;
diagnostics.push(diagnosticFactories.transpileError(err));
updateTranspiledFile(sourceFile.fileName, {
lua: `error(${JSON.stringify(err.message)})\n`,
sourceMap: "",
});
}
};
const transformers = getCustomTransformers(program, diagnostics, customTransformers, processSourceFile);
const writeFile: ts.WriteFileCallback = (fileName, data, _bom, _onError, sourceFiles = []) => {
for (const sourceFile of sourceFiles) {
const isDeclaration = fileName.endsWith(".d.ts");
const isDeclarationMap = fileName.endsWith(".d.ts.map");
if (isDeclaration) {
updateTranspiledFile(sourceFile.fileName, { declaration: data });
} else if (isDeclarationMap) {
updateTranspiledFile(sourceFile.fileName, { declarationMap: data });
}
}
};
const isEmittableJsonFile = (sourceFile: ts.SourceFile) =>
sourceFile.flags & ts.NodeFlags.JsonFile &&
!options.emitDeclarationOnly &&
!program.isSourceFileFromExternalLibrary(sourceFile);
// We always have to emit to get transformer diagnostics
const oldNoEmit = options.noEmit;
options.noEmit = false;
if (targetSourceFiles) {
for (const file of targetSourceFiles) {
if (isEmittableJsonFile(file)) {
processSourceFile(file);
} else {
diagnostics.push(...program.emit(file, writeFile, undefined, false, transformers).diagnostics);
}
}
} else {
diagnostics.push(...program.emit(undefined, writeFile, undefined, false, transformers).diagnostics);
// JSON files don't get through transformers and aren't written when outDir is the same as rootDir
program
.getSourceFiles()
.filter(isEmittableJsonFile)
.forEach(processSourceFile);
}
options.noEmit = oldNoEmit;
if (options.noEmit || (options.noEmitOnError && diagnostics.length > 0)) {
transpiledFiles = [];
}
return { diagnostics, transpiledFiles };
}