forked from denoland/deno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompiler_main.js
376 lines (332 loc) · 8.78 KB
/
compiler_main.js
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
// Because we're bootstrapping the TypeScript compiler without dependencies on
// Node, this is written in JavaScript, but leverages JSDoc that can be
// understood by the TypeScript language service, so it allows type safety
// checking in VSCode.
const ASSETS = "$asset$";
/**
* @param {string} configText
* @param {Array<string>} rootNames
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function main(configText, rootNames) {
ops = Deno.core.ops();
println(`>>> ts version ${ts.version}`);
println(`>>> rootNames ${rootNames}`);
const host = new Host();
assert(rootNames.length > 0);
const { options, diagnostics } = configure(configText);
handleDiagnostics(host, diagnostics);
println(`>>> TS config: ${JSON.stringify(options)}`);
const program = ts.createProgram(rootNames, options, host);
handleDiagnostics(
host,
ts.getPreEmitDiagnostics(program).filter(({ code }) => {
// TS1063: An export assignment cannot be used in a namespace.
if (code === 1063) return false;
// TS2691: An import path cannot end with a '.ts' extension. Consider
// importing 'bad-module' instead.
if (code === 2691) return false;
// TS5009: Cannot find the common subdirectory path for the input files.
if (code === 5009) return false;
return true;
})
);
const emitResult = program.emit();
handleDiagnostics(host, emitResult.diagnostics);
dispatch(
"setEmitResult",
Object.assign(emitResult, { tsVersion: ts.version })
);
}
/**
* @param {...string} s
*/
function println(...s) {
Deno.core.print(s.join(" ") + "\n");
}
/**
* @returns {never}
*/
function unreachable() {
throw Error("unreachable");
}
/**
* @param {unknown} cond
* @returns {asserts cond}
*/
function assert(cond) {
if (!cond) {
throw Error("assert");
}
}
/**
* @param {Uint8Array | null} ui8
*/
function decodeAscii(ui8) {
let out = "";
if (!ui8) {
return out;
}
for (let i = 0; i < ui8.length; i++) {
out += String.fromCharCode(ui8[i]);
}
return out;
}
/**
* @param {string} str
*/
function encode(str) {
const charCodes = str.split("").map(c => c.charCodeAt(0));
const ui8 = new Uint8Array(charCodes);
return ui8;
}
//
/** **Warning!** Op ids must be acquired from Rust using `Deno.core.ops()`
* before dispatching any action.
* @type {Record<string, number>}
*/
let ops;
/**
* @type {Map<string, string>}
*/
const moduleMap = new Map();
const externalSpecifierRegEx = /^file:\/{3}\S+\/js(\/\S+\.ts)$/;
/**
* This is a minimal implementation of a compiler host to be able to allow the
* creation of runtime bundles. Some of the methods are implemented in a way
* to just appease the TypeScript compiler, not to necessarily be a general
* purpose implementation.
*
* @implements {ts.CompilerHost}
*/
class Host {
/**
* @param {string} _fileName
*/
fileExists(_fileName) {
return true;
}
/**
* @param {string} _fileName
*/
readFile(_fileName) {
unreachable();
return undefined;
}
useCaseSensitiveFileNames() {
return false;
}
/**
* @param {ts.CompilerOptions} _options
*/
getDefaultLibFileName(_options) {
return "lib.deno_core.d.ts";
}
getDefaultLibLocation() {
return ASSETS;
}
getCurrentDirectory() {
return ".";
}
/**
* @param {string} fileName
* @param {ts.ScriptTarget} languageVersion
* @param {(message: string) => void} _onError
* @param {boolean} shouldCreateNewSourceFile
*/
getSourceFile(
fileName,
languageVersion,
_onError,
shouldCreateNewSourceFile
) {
assert(!shouldCreateNewSourceFile); // We haven't yet encountered this.
// This hacks around the fact that TypeScript tries to magically guess the
// d.ts filename.
if (fileName.startsWith("$typeRoots$")) {
assert(fileName.startsWith("$typeRoots$/"));
assert(fileName.endsWith("/index.d.ts"));
fileName = fileName
.replace("$typeRoots$/", "")
.replace("/index.d.ts", "");
}
// This looks up any modules that have been mapped to internal names
if (moduleMap.has(fileName)) {
fileName = moduleMap.get(fileName);
}
const { sourceCode, moduleName } = dispatch("readFile", {
fileName,
languageVersion,
shouldCreateNewSourceFile
});
// If we match the external specifier regex, we will then create an internal
// specifier and then use that when creating the source file
let internalModuleName = moduleName;
const result = externalSpecifierRegEx.exec(moduleName);
if (result) {
const [, specifier] = result;
const internalSpecifier = `$deno$${specifier}`;
moduleMap.set(internalSpecifier, moduleName);
internalModuleName = internalSpecifier;
}
const sourceFile = ts.createSourceFile(
internalModuleName,
sourceCode,
languageVersion
);
sourceFile.moduleName = internalModuleName;
return sourceFile;
}
/**
* @param {string} fileName
* @param {string} data
* @param {boolean} _writeByteOrderMark
* @param {((message: string) => void)?} _onError
* @param {ReadonlyArray<ts.SourceFile>?} sourceFiles
*/
writeFile(
fileName,
data,
_writeByteOrderMark,
_onError = null,
sourceFiles = null
) {
if (sourceFiles == null) {
return;
}
const moduleName = sourceFiles[sourceFiles.length - 1].moduleName;
return dispatch("writeFile", { fileName, moduleName, data });
}
/**
* @param {string} _fileName
* @param {ts.Path} _path
* @param {ts.ScriptTarget} _languageVersion
* @param {*} _onError
* @param {boolean} _shouldCreateNewSourceFile
*/
getSourceFileByPath(
_fileName,
_path,
_languageVersion,
_onError,
_shouldCreateNewSourceFile
) {
unreachable();
return undefined;
}
/**
* @param {string} fileName
*/
getCanonicalFileName(fileName) {
return fileName;
}
getNewLine() {
return "\n";
}
/**
* @param {string[]} moduleNames
* @param {string} containingFile
* @return {Array<ts.ResolvedModule | undefined>}
*/
resolveModuleNames(moduleNames, containingFile) {
// If the containing file is an internal specifier, map it back to the
// external specifier
containingFile = moduleMap.has(containingFile)
? moduleMap.get(containingFile)
: containingFile;
/** @type {string[]} */
const resolvedNames = dispatch("resolveModuleNames", {
moduleNames,
containingFile
});
/** @type {ts.ResolvedModule[]} */
const r = resolvedNames.map(resolvedFileName => {
const extension = getExtension(resolvedFileName);
return { resolvedFileName, extension };
});
return r;
}
}
/**
* @param {string} configurationText
*/
function configure(configurationText) {
const { config, error } = ts.parseConfigFileTextToJson(
"tsconfig.json",
configurationText
);
if (error) {
return { options: {}, diagnostics: [error] };
}
const { options, errors } = ts.convertCompilerOptionsFromJson(
config.compilerOptions,
""
);
return {
options,
diagnostics: errors.length ? errors : undefined
};
}
/**
* @param {string} opName
* @param {Record<string,any>} obj
*/
function dispatch(opName, obj) {
const opId = ops[opName];
if (!opId) {
throw new Error(`Unknown op: ${opName}`);
}
const s = JSON.stringify(obj);
const msg = encode(s);
const resUi8 = Deno.core.dispatch(opId, msg);
const resStr = decodeAscii(resUi8);
const res = JSON.parse(resStr);
if (!res["ok"]) {
throw Error(`${opName} failed ${res["err"]}. Args: ${JSON.stringify(obj)}`);
}
return res["ok"];
}
/**
* @param {number} code
*/
function exit(code) {
dispatch("exit", { code });
return unreachable();
}
// Maximum number of diagnostics to display.
const MAX_ERRORS = 5;
/**
* @param {ts.CompilerHost} host
* @param {ReadonlyArray<ts.Diagnostic> | undefined} diagnostics
*/
function handleDiagnostics(host, diagnostics) {
if (diagnostics && diagnostics.length) {
let rest = 0;
if (diagnostics.length > MAX_ERRORS) {
rest = diagnostics.length - MAX_ERRORS;
diagnostics = diagnostics.slice(0, MAX_ERRORS);
}
const msg = ts.formatDiagnosticsWithColorAndContext(diagnostics, host);
println(msg);
if (rest) {
println(`And ${rest} other errors.`);
}
exit(1);
}
}
/** Returns the TypeScript Extension enum for a given media type.
* @param {string} fileName
* @returns {ts.Extension}
*/
function getExtension(fileName) {
if (fileName.endsWith(".d.ts")) {
return ts.Extension.Dts;
} else if (fileName.endsWith(".ts")) {
return ts.Extension.Ts;
} else if (fileName.endsWith(".js")) {
return ts.Extension.Js;
} else {
throw TypeError(`Cannot resolve extension for ${fileName}`);
}
}