-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathmain.ts
More file actions
610 lines (558 loc) · 19.9 KB
/
main.ts
File metadata and controls
610 lines (558 loc) · 19.9 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
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
/**
* @fileoverview Wrapper for TypeScript parser, inspired by
* [typescript-eslint-parser](https://github.com/eslint/typescript-eslint-parser).
*
* The wrapper can be invoked with a single argument, which is interpreted as a file name.
* The file is parsed and the AST dumped to stdout as described below.
*
* When run without arguments, the wrapper enters server mode, expecting JSON-encoded requests
* on `stdin`, one per line.
*
* Each request should have a `command` property, which should either be `parse` or `quit`.
*
* A `quit` request causes the wrapper to terminate.
*
* A `parse` request should have a `filename` property, which should be the path of a
* TypeScript file.
*
* In response to a `parse` request, the wrapper invokes the TypeScript parser to parse
* the given file, and dumps a JSON representation of the resulting AST to stdout,
* with the following adjustments:
*
* - the `kind` and `operator` properties have symbolic string values instead of numeric
* values (the latter change between versions of the TypeScript compiler, the former
* don't);
* - position information is given in terms of line/column pairs instead of offsets;
* - declarations have a `declarationKind` property indicating whether they are `var`,
* `let` or `const` declarations;
* - `parent` pointers are omitted (since they cannot be serialised to JSON);
* - tokens and comments are added to the AST root node in a `tokens` property.
*/
"use strict";
import * as pathlib from "path";
import * as readline from "readline";
import * as ts from "./typescript";
import * as ast_extractor from "./ast_extractor";
import { Project } from "./common";
import { VirtualSourceRoot } from "./virtual_source_root";
// Remove limit on stack trace depth.
Error.stackTraceLimit = Infinity;
interface ParseCommand {
command: "parse";
filename: string;
}
interface LoadCommand {
tsConfig: string;
sourceRoot: string | null;
virtualSourceRoot: string | null;
packageEntryPoints: [string, string][];
packageJsonFiles: [string, string][];
}
interface ResetCommand {
command: "reset";
}
interface QuitCommand {
command: "quit";
}
interface PrepareFilesCommand {
command: "prepare-files";
filenames: string[];
}
interface GetMetadataCommand {
command: "get-metadata";
}
type Command = ParseCommand | ResetCommand | QuitCommand | PrepareFilesCommand | GetMetadataCommand;
/** The state to be shared between commands. */
class State {
public project: Project = null;
/** List of files that have been requested. */
public pendingFiles: string[] = [];
public pendingFileIndex = 0;
/** Next response to be delivered. */
public pendingResponse: string = null;
/** Map from `package.json` files to their contents. */
public parsedPackageJson = new Map<string, any>();
/** Map from `package.json` files to the file referenced in its `types` or `typings` field. */
public packageTypings = new Map<string, string | undefined>();
/** Map from file path to the enclosing `package.json` file, if any. Will not traverse outside node_modules. */
public enclosingPackageJson = new Map<string, string | undefined>();
}
let state = new State();
const reloadMemoryThresholdMb = getEnvironmentVariable("SEMMLE_TYPESCRIPT_MEMORY_THRESHOLD", Number, 1000);
function getPackageJson(file: string): any {
let cache = state.parsedPackageJson;
if (cache.has(file)) return cache.get(file);
let result = getPackageJsonRaw(file);
cache.set(file, result);
return result;
}
function getPackageJsonRaw(file: string): any {
if (!ts.sys.fileExists(file)) return undefined;
try {
let json = JSON.parse(ts.sys.readFile(file));
if (typeof json !== 'object') return undefined;
return json;
} catch (e) {
return undefined;
}
}
function getPackageTypings(file: string): string | undefined {
let cache = state.packageTypings;
if (cache.has(file)) return cache.get(file);
let result = getPackageTypingsRaw(file);
cache.set(file, result);
return result;
}
function getPackageTypingsRaw(packageJsonFile: string): string | undefined {
let json = getPackageJson(packageJsonFile);
if (json == null) return undefined;
let typings = json.types || json.typings; // "types" and "typings" are aliases
if (typeof typings !== 'string') return undefined;
let absolutePath = pathlib.join(pathlib.dirname(packageJsonFile), typings);
if (ts.sys.directoryExists(absolutePath)) {
absolutePath = pathlib.join(absolutePath, 'index.d.ts');
} else if (!absolutePath.endsWith('.ts')) {
absolutePath += '.d.ts';
}
if (!ts.sys.fileExists(absolutePath)) return undefined;
return ts.sys.resolvePath(absolutePath);
}
function getEnclosingPackageJson(file: string): string | undefined {
let cache = state.packageTypings;
if (cache.has(file)) return cache.get(file);
let result = getEnclosingPackageJsonRaw(file);
cache.set(file, result);
return result;
}
function getEnclosingPackageJsonRaw(file: string): string | undefined {
let packageJson = pathlib.join(file, 'package.json');
if (ts.sys.fileExists(packageJson)) {
return packageJson;
}
if (pathlib.basename(file) === 'node_modules') {
return undefined;
}
let dirname = pathlib.dirname(file);
if (dirname.length < file.length) {
return getEnclosingPackageJson(dirname);
}
return undefined;
}
/**
* Debugging method for finding cycles in the TypeScript AST. Should not be used in production.
*
* If cycles are found, the whitelist in `astProperties` is too permissive.
*/
// tslint:disable-next-line:no-unused-variable
function checkCycle(root: any) {
let path: string[] = [];
function visit(obj: any): boolean {
if (obj == null || typeof obj !== "object") return false;
if (obj.$cycle_visiting) {
return true;
}
obj.$cycle_visiting = true;
for (let k in obj) {
if (!obj.hasOwnProperty(k)) continue;
// Ignore numeric and whitelisted properties.
if (+k !== +k && !astPropertySet.has(k)) continue;
if (k === "$cycle_visiting") continue;
let cycle = visit(obj[k]);
if (cycle) {
path.push(k);
return true;
}
}
obj.$cycle_visiting = undefined;
return false;
}
visit(root);
if (path.length > 0) {
path.reverse();
console.log(JSON.stringify({ type: "error", message: "Cycle = " + path.join(".") }));
}
}
/** Property names to extract from the TypeScript AST. */
const astProperties: string[] = [
"$declarationKind",
"$end",
"$lineStarts",
"$overloadIndex",
"$pos",
"$tokens",
"argument",
"argumentExpression",
"arguments",
"assertsModifier",
"asteriskToken",
"attributes",
"block",
"body",
"caseBlock",
"catchClause",
"checkType",
"children",
"clauses",
"closingElement",
"closingFragment",
"condition",
"constraint",
"constructor",
"declarationList",
"declarations",
"default",
"delete",
"dotDotDotToken",
"elements",
"elementType",
"elementTypes",
"elseStatement",
"escapedText",
"exclamationToken",
"exportClause",
"expression",
"exprName",
"extendsType",
"falseType",
"finallyBlock",
"flags",
"head",
"heritageClauses",
"importClause",
"incrementor",
"indexType",
"init",
"initializer",
"isExportEquals",
"isTypeOf",
"isTypeOnly",
"keywordToken",
"kind",
"label",
"left",
"literal",
"members",
"messageText",
"modifiers",
"moduleReference",
"moduleSpecifier",
"name",
"namedBindings",
"objectType",
"openingElement",
"openingFragment",
"operand",
"operator",
"operatorToken",
"parameterName",
"parameters",
"parseDiagnostics",
"phaseModifier",
"properties",
"propertyName",
"qualifier",
"questionDotToken",
"questionToken",
"right",
"selfClosing",
"statement",
"statements",
"tag",
"tagName",
"template",
"templateSpans",
"text",
"thenStatement",
"token",
"tokenPos",
"trueType",
"tryBlock",
"type",
"typeArguments",
"typeName",
"typeParameter",
"typeParameters",
"types",
"variableDeclaration",
"whenFalse",
"whenTrue",
];
/** Property names used in a parse command response, in addition to the AST itself. */
const astMetaProperties: string[] = [
"ast",
"type",
];
/** Property names to extract in an AST response. */
const astPropertySet = new Set([...astProperties, ...astMetaProperties]);
/**
* Converts (part of) an AST to a JSON string, ignoring properties we're not interested in.
*/
function stringifyAST(obj: any) {
return JSON.stringify(obj, (k, v) => {
// Filter out properties that aren't numeric, empty, or whitelisted.
// Note `k` is the empty string for the root object, which is also covered by +k === +k.
return (+k === +k || astPropertySet.has(k)) ? v : undefined;
});
}
function extractFile(filename: string): string {
let ast = getAstForFile(filename);
return stringifyAST({
type: "ast",
ast,
});
}
function prepareNextFile() {
if (state.pendingResponse != null) return;
if (state.pendingFileIndex < state.pendingFiles.length) {
checkMemoryUsage();
let nextFilename = state.pendingFiles[state.pendingFileIndex];
state.pendingResponse = extractFile(nextFilename);
}
}
function handleParseCommand(command: ParseCommand, checkPending = true) {
let filename = command.filename;
let expectedFilename = state.pendingFiles[state.pendingFileIndex];
if (expectedFilename !== filename && checkPending) {
// File was requested out of order. This happens in rare cases because the Java process decided against extracting it,
// for example because it was too large. Just recover and accept that some work was wasted.
state.pendingResponse = null;
state.pendingFileIndex = state.pendingFiles.indexOf(filename);
}
++state.pendingFileIndex;
let response = state.pendingResponse || extractFile(command.filename);
state.pendingResponse = null;
process.stdout.write(response + "\n", () => {
// Start working on the next file as soon as the old one is flushed.
// Note that if we didn't wait for flushing, this would block the I/O
// loop and delay flushing.
prepareNextFile();
});
}
/**
* Returns true if the given source file has an actual AST, as opposed to
* a SourceFile that is a "redirect" to another SourceFile.
*
* The TypeScript API should not expose such redirecting SourceFiles,
* but we sometimes get them anyway.
*/
function isExtractableSourceFile(ast: ast_extractor.AugmentedSourceFile): boolean {
return ast.redirectInfo == null;
}
/**
* Gets the AST and source code for the given file, either from
* an already-open project, or by parsing the file.
*/
function getAstForFile(filename: string): ts.SourceFile {
let { ast, code } = parseSingleFile(filename);
if (ast != null && isExtractableSourceFile(ast)) {
ast_extractor.augmentAst(ast, code, null);
}
return ast;
}
function parseSingleFile(filename: string): { ast: ts.SourceFile, code: string } {
let code = ts.sys.readFile(filename);
// create a compiler host that only allows access to `filename`
let compilerHost: ts.CompilerHost = {
fileExists() { return true; },
getCanonicalFileName() { return filename; },
getCurrentDirectory() { return ""; },
getDefaultLibFileName() { return "lib.d.ts"; },
getNewLine() { return "\n"; },
getSourceFile() {
return ts.createSourceFile(filename, code, ts.ScriptTarget.Latest, true);
},
readFile() { return null; },
useCaseSensitiveFileNames() { return true; },
writeFile() { return null; },
getDirectories() { return []; },
};
// parse `filename` with minimial checking
let compilerOptions: ts.CompilerOptions = {
experimentalDecorators: true,
experimentalAsyncFunctions: true,
jsx: ts.JsxEmit.Preserve,
noResolve: true,
};
let program = ts.createProgram([filename], compilerOptions, compilerHost);
let ast = program.getSourceFile(filename);
return { ast, code };
}
/**
* Matches a path segment referencing a package in a node_modules folder, and extracts
* two capture groups: the package name, and the relative path in the package.
*
* For example `lib/node_modules/@foo/bar/src/index.js` extracts the capture groups [`@foo/bar`, `src/index.js`].
*/
const nodeModulesRex = /[/\\]node_modules[/\\]((?:@[\w.-]+[/\\])?\w[\w.-]*)[/\\](.*)/;
interface LoadedConfig {
config: ts.ParsedCommandLine;
basePath: string;
packageEntryPoints: Map<string, string>;
packageJsonFiles: Map<string, string>;
virtualSourceRoot: VirtualSourceRoot;
ownFiles: string[];
}
function loadTsConfig(command: LoadCommand): LoadedConfig {
let tsConfig = ts.readConfigFile(command.tsConfig, ts.sys.readFile);
let basePath = pathlib.dirname(command.tsConfig);
let packageEntryPoints = new Map(command.packageEntryPoints);
let packageJsonFiles = new Map(command.packageJsonFiles);
let virtualSourceRoot = new VirtualSourceRoot(command.sourceRoot, command.virtualSourceRoot);
/**
* Rewrites path segments of form `node_modules/PACK/suffix` to be relative to
* the location of package PACK in the source tree, if it exists.
*/
function redirectNodeModulesPath(path: string) {
let nodeModulesMatch = nodeModulesRex.exec(path);
if (nodeModulesMatch == null) return null;
let packageName = nodeModulesMatch[1];
let packageJsonFile = packageJsonFiles.get(packageName);
if (packageJsonFile == null) return null;
let packageDir = pathlib.dirname(packageJsonFile);
let suffix = nodeModulesMatch[2];
let finalPath = pathlib.join(packageDir, suffix);
if (!ts.sys.fileExists(finalPath)) return null;
return finalPath;
}
/**
* Create the host passed to the tsconfig.json parser.
*
* We override its file system access in case there is an "extends"
* clause pointing into "./node_modules", which must be redirected to
* the location of an installed package or a checked-in package.
*/
let parseConfigHost: ts.ParseConfigHost = {
useCaseSensitiveFileNames: true,
readDirectory: (rootDir, extensions, excludes?, includes?, depth?) => {
// Perform the glob matching in both real and virtual source roots.
let exclusions = excludes == null ? [] : [...excludes];
if (virtualSourceRoot.virtualSourceRoot != null) {
// qltest puts the virtual source root inside the real source root (.testproj).
// Make sure we don't find files inside the virtual source root in this pass.
exclusions.push(virtualSourceRoot.virtualSourceRoot);
}
let originalResults = ts.sys.readDirectory(rootDir, extensions, exclusions, includes, depth)
let virtualDir = virtualSourceRoot.toVirtualPath(rootDir);
if (virtualDir == null) {
return originalResults;
}
// Make sure glob matching does not to discover anything in node_modules.
let virtualExclusions = excludes == null ? [] : [...excludes];
virtualExclusions.push('**/node_modules/**/*');
let virtualResults = ts.sys.readDirectory(virtualDir, extensions, virtualExclusions, includes, depth)
return [...originalResults, ...virtualResults];
},
fileExists: (path: string) => {
return ts.sys.fileExists(path)
|| virtualSourceRoot.toVirtualPathIfFileExists(path) != null
|| redirectNodeModulesPath(path) != null;
},
readFile: (path: string) => {
if (!ts.sys.fileExists(path)) {
let virtualPath = virtualSourceRoot.toVirtualPathIfFileExists(path);
if (virtualPath != null) return ts.sys.readFile(virtualPath);
virtualPath = redirectNodeModulesPath(path);
if (virtualPath != null) return ts.sys.readFile(virtualPath);
}
return ts.sys.readFile(path);
}
};
let config = ts.parseJsonConfigFileContent(tsConfig.config, parseConfigHost, basePath);
let ownFiles = config.fileNames.map(file => pathlib.resolve(file));
return { config, basePath, packageJsonFiles, packageEntryPoints, virtualSourceRoot, ownFiles };
}
function handleResetCommand(command: ResetCommand) {
reset();
console.log(JSON.stringify({
type: "reset-done",
}));
}
function handlePrepareFilesCommand(command: PrepareFilesCommand) {
state.pendingFiles = command.filenames;
state.pendingFileIndex = 0;
state.pendingResponse = null;
process.stdout.write('{"type":"ok"}\n', () => {
prepareNextFile();
});
}
function handleGetMetadataCommand(command: GetMetadataCommand) {
console.log(JSON.stringify({
type: "metadata",
syntaxKinds: ts.SyntaxKind,
nodeFlags: ts.NodeFlags,
}));
}
function reset() {
state = new State();
}
function getEnvironmentVariable<T>(name: string, parse: (x: string) => T, defaultValue: T) {
let value = process.env[name];
return value != null ? parse(value) : defaultValue;
}
/**
* Whether the memory usage was last observed to be above the threshold for restarting the TypeScript compiler.
*
* This is to prevent repeatedly restarting the compiler if the GC does not immediately bring us below the
* threshold again.
*/
let hasReloadedSinceExceedingThreshold = false;
/**
* If memory usage has moved above a the threshold, reboot the TypeScript compiler instance.
*
* Make sure to call this only when stdout has been flushed.
*/
function checkMemoryUsage() {
let bytesUsed = process.memoryUsage().heapUsed;
let megabytesUsed = bytesUsed / 1000000;
if (!hasReloadedSinceExceedingThreshold && megabytesUsed > reloadMemoryThresholdMb && state.project != null) {
console.warn('Restarting TypeScript compiler due to memory usage');
state.project.reload();
hasReloadedSinceExceedingThreshold = true;
}
else if (hasReloadedSinceExceedingThreshold && megabytesUsed < reloadMemoryThresholdMb) {
hasReloadedSinceExceedingThreshold = false;
}
}
function runReadLineInterface() {
reset();
let rl = readline.createInterface({ input: process.stdin, output: process.stdout });
rl.on("line", (line: string) => {
let req: Command = JSON.parse(line);
switch (req.command) {
case "parse":
handleParseCommand(req);
break;
case "prepare-files":
handlePrepareFilesCommand(req);
break;
case "reset":
handleResetCommand(req);
break;
case "get-metadata":
handleGetMetadataCommand(req);
break;
case "quit":
rl.close();
break;
default:
throw new Error("Unknown command " + (req as any).command + ".");
}
});
}
// Parse command-line arguments.
if (process.argv.length > 2) {
let argument = process.argv[2];
if (argument === "--version") {
console.log("parser-wrapper with TypeScript " + ts.version);
} else if (pathlib.extname(argument) === ".ts" || pathlib.extname(argument) === ".tsx") {
handleParseCommand({
command: "parse",
filename: argument,
}, false);
} else {
console.error("Unrecognized file or flag: " + argument);
}
process.exit(0);
} else {
runReadLineInterface();
}