forked from TypeScriptToLua/TypeScriptToLua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompiler.ts
More file actions
154 lines (129 loc) · 5.58 KB
/
Compiler.ts
File metadata and controls
154 lines (129 loc) · 5.58 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
import * as fs from "fs";
import * as path from "path";
import * as ts from "typescript";
import * as CommandLineParser from "./CommandLineParser";
import {CompilerOptions, LuaLibImportKind, LuaTarget} from "./CompilerOptions";
import {LuaTranspiler} from "./LuaTranspiler";
export function compile(argv: string[]): void {
const parseResult = CommandLineParser.parseCommandLine(argv);
if (parseResult.isValid === true) {
if (parseResult.result.options.help) {
console.log(CommandLineParser.getHelpString());
return;
}
if (parseResult.result.options.version) {
console.log(CommandLineParser.version);
return;
}
/* istanbul ignore if: tested in test/compiler/watchmode.spec with subproccess */
if (parseResult.result.options.watch) {
watchWithOptions(parseResult.result.fileNames, parseResult.result.options);
} else {
compileFilesWithOptions(parseResult.result.fileNames, parseResult.result.options);
}
} else {
console.error(`Invalid CLI input: ${parseResult.errorMessage}`);
}
}
/* istanbul ignore next: tested in test/compiler/watchmode.spec with subproccess */
export function watchWithOptions(fileNames: string[], options: CompilerOptions): void {
let host: ts.WatchCompilerHost<ts.SemanticDiagnosticsBuilderProgram>;
let config = false;
if (options.project) {
config = true;
host = ts.createWatchCompilerHost(options.project, options, ts.sys, ts.createSemanticDiagnosticsBuilderProgram);
} else {
host = ts.createWatchCompilerHost(fileNames, options, ts.sys, ts.createSemanticDiagnosticsBuilderProgram);
}
host.afterProgramCreate = program => {
const transpiler = new LuaTranspiler(program.getProgram());
const status = transpiler.emitFilesAndReportErrors();
const errorDiagnostic: ts.Diagnostic = {
category: undefined,
code: 6194,
file: undefined,
length: 0,
messageText: "Found 0 errors. Watching for file changes.",
start: 0,
};
if (status !== 0) {
errorDiagnostic.messageText = "Found Errors. Watching for file changes.";
errorDiagnostic.code = 6193;
}
host.onWatchStatusChange(errorDiagnostic, host.getNewLine(), program.getCompilerOptions());
};
if (config) {
ts.createWatchProgram(
host as ts.WatchCompilerHostOfConfigFile<ts.SemanticDiagnosticsBuilderProgram>
);
} else {
ts.createWatchProgram(
host as ts.WatchCompilerHostOfFilesAndCompilerOptions<ts.SemanticDiagnosticsBuilderProgram>
);
}
}
export function compileFilesWithOptions(fileNames: string[], options: CompilerOptions): void {
const program = ts.createProgram(fileNames, options);
const transpiler = new LuaTranspiler(program);
transpiler.emitFilesAndReportErrors();
}
const libCache: {[key: string]: string} = {};
const defaultCompilerOptions: CompilerOptions = {
luaLibImport: LuaLibImportKind.Require,
luaTarget: LuaTarget.Lua53,
};
export function createStringCompilerProgram(
input: string, options: CompilerOptions = defaultCompilerOptions, filePath = "file.ts"): ts.Program {
const compilerHost = {
directoryExists: () => true,
fileExists: (fileName): boolean => true,
getCanonicalFileName: fileName => fileName,
getCurrentDirectory: () => "",
getDefaultLibFileName: () => "lib.es6.d.ts",
getDirectories: () => [],
getNewLine: () => "\n",
getSourceFile: (filename: string) => {
if (filename === filePath) {
return ts.createSourceFile(filename, input, ts.ScriptTarget.Latest, false);
}
if (filename.indexOf(".d.ts") !== -1) {
if (!libCache[filename]) {
const typeScriptDir = path.dirname(require.resolve("typescript"));
const filePath = path.join(typeScriptDir, filename);
if (fs.existsSync(filePath)) {
libCache[filename] = fs.readFileSync(filePath).toString();
} else {
const pathWithLibPrefix = path.join(typeScriptDir, "lib." + filename);
libCache[filename] = fs.readFileSync(pathWithLibPrefix).toString();
}
}
return ts.createSourceFile(filename, libCache[filename], ts.ScriptTarget.Latest, false);
}
return undefined;
},
readFile: () => "",
useCaseSensitiveFileNames: () => false,
// Don't write output
writeFile: (name, text, writeByteOrderMark) => undefined,
};
return ts.createProgram([filePath], options, compilerHost);
}
export function transpileString(
str: string,
options: CompilerOptions = defaultCompilerOptions,
ignoreDiagnostics = false,
filePath = "file.ts"
): string {
const program = createStringCompilerProgram(str, options, filePath);
if (!ignoreDiagnostics) {
const diagnostics = ts.getPreEmitDiagnostics(program);
const typeScriptErrors = diagnostics.filter(diag => diag.category === ts.DiagnosticCategory.Error);
if (typeScriptErrors.length > 0) {
typeScriptErrors.forEach(e => console.warn(e.messageText));
throw new Error("Encountered invalid TypeScript.");
}
}
const transpiler = new LuaTranspiler(program);
const result = transpiler.transpileSourceFile(program.getSourceFile(filePath));
return result.trim();
}