forked from microsoft/TypeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyzer.ts
More file actions
418 lines (376 loc) · 16.5 KB
/
analyzer.ts
File metadata and controls
418 lines (376 loc) · 16.5 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
/// <no-default-lib/>
/// <reference path="..\..\lib\lib.core.d.ts"/>
/// <reference path="analyzerEnv.ts"/>
/// <reference path="..\compiler\types.ts"/>
/// <reference path="..\compiler\core.ts"/>
/// <reference path="..\compiler\sys.ts"/>
/// <reference path="..\compiler\commandLineParser.ts"/>
/// <reference path="services.ts"/>
/// <reference path="utilities.ts"/>
/// <reference path="shims.ts"/>
interface AnalyzisError {
message: string;
stack: string;
}
interface Hyperlink {
sourceFile: string;
start: number;
symbolId: string;
}
interface ClassifiedRange {
classification: string;
start: number;
length: number;
hyperlinks: Hyperlink[];
definitionSymbolId?: string;
definitionKind?: string;
searchString?: string;
fullName?: string;
}
interface AnalyzedFile {
fileName: string;
syntacticClassifications: ClassifiedRange[];
semanticClassifications: ClassifiedRange[];
fileSymbolId: string;
}
interface ProcessedFile {
index: number;
fileName: string;
}
function log<T>(text: string, f: () => T): T {
let start = new Date().getTime();
let result = f();
let end = new Date().getTime();
ts.sys.write(text + ": " + (end - start) + " ms" + ts.sys.newLine)
return result;
}
function analyze(libFileName: string, files: string[], outputFolder: string): ProcessedFile[]{
let sourceFileVersion = "1";
let sourceFileIsOpen = false;
let program = log("createProgram", () => createProgram(files));
let fileNames = ts.map(program.getSourceFiles(), f => f.fileName);
let scriptSnapshots: ts.Map<ts.IScriptSnapshot> = {};
let checker = log("getTypeChecker", () => program.getTypeChecker());
ts.forEach(program.getSourceFiles(), f => {
scriptSnapshots[f.fileName] = ts.ScriptSnapshot.fromString(f.text);
});
const hostCancellationToken: ts.HostCancellationToken = {
isCancellationRequested: () => false
}
let host: ts.LanguageServiceHost = {
getCancellationToken: () => hostCancellationToken,
getCompilationSettings: () => { return {}; },
getDefaultLibFileName: () => libFileName,
getCurrentDirectory: () => ".",
getLocalizedDiagnosticMessages: () => undefined,
getScriptFileNames: () => fileNames,
getScriptVersion: _ => sourceFileVersion,
getScriptSnapshot: name => scriptSnapshots[name],
log: (s) => { },
trace: (s) => { },
error: (s) => { }
};
const cancellationToken: ts.CancellationToken = {
isCancellationRequested: () => false,
throwIfCancellationRequested: () => {}
}
let documentRegistry: ts.DocumentRegistry = {
acquireDocument: (fileName, settings, snapshot, version) => program.getSourceFile(fileName),
releaseDocument: (fileName, settings) => { },
updateDocument: (fileName, settings, snapshot, version) => program.getSourceFile(fileName),
reportStats: () => ""
};
let ls = ts.createLanguageService(host, documentRegistry);
let sourceFiles = program.getSourceFiles();
let processedFiles: ProcessedFile[] = [];
for (let i = 0, len = sourceFiles.length; i < len; ++i) {
var f = sourceFiles[i];
var fileSpan = ts.createTextSpan(0, f.text.length);
let result = log("getClassifications '" + f.fileName + "'", () => {
let syntacticClassifications = ls.getSyntacticClassifications(f.fileName, fileSpan);
let convertedSyntactic = convertClassifications(syntacticClassifications, f, /*addHyperlinks*/ true);
let semanticClassifications = ls.getSemanticClassifications(f.fileName, fileSpan);
let convertedSemantic = convertClassifications(semanticClassifications, f, /*addHyperlinks*/ false);
return {
fileName: f.fileName,
syntacticClassifications: convertedSyntactic,
semanticClassifications: convertedSemantic,
fileSymbolId: makeSymbolId(f.fileName, 0)
};
});
let json = JSON.stringify(result);
let path = ts.combinePaths(outputFolder, i + ".json");
ts.sys.writeFile(path, json, /*writeByteOrderMark*/ false);
processedFiles.push({ fileName: f.fileName, index: i });
}
return processedFiles;
// local functions
function skipClassification(c: string) {
switch (c) {
case ts.ClassificationTypeNames.whiteSpace:
case ts.ClassificationTypeNames.punctuation:
case ts.ClassificationTypeNames.operator:
return true;
default:
return false;
}
}
function getDeclarationForName(n: ts.Node): ts.Declaration {
switch (n.kind) {
case ts.SyntaxKind.ConstructorKeyword:
return <ts.Declaration>n.parent;
case ts.SyntaxKind.Identifier:
case ts.SyntaxKind.NumericLiteral:
case ts.SyntaxKind.StringLiteral:
if (n.parent && ts.isDeclaration(n.parent) && (<ts.Declaration>n.parent).name === n) {
return <ts.Declaration>n.parent;
}
}
return undefined;
}
function getDeclarationName(decl: ts.Node): string {
switch (decl.kind) {
case ts.SyntaxKind.TypeAliasDeclaration:
return "type alias";
case ts.SyntaxKind.Constructor:
return "constructor";
case ts.SyntaxKind.TypeParameter:
return "type parameter";
case ts.SyntaxKind.Parameter:
return "parameter";
case ts.SyntaxKind.VariableDeclaration:
return "variable";
case ts.SyntaxKind.PropertySignature:
case ts.SyntaxKind.PropertyDeclaration:
case ts.SyntaxKind.PropertyAssignment:
return "property";
case ts.SyntaxKind.EnumMember:
return "enum member"
case ts.SyntaxKind.MethodSignature:
case ts.SyntaxKind.MethodDeclaration:
return "method";
case ts.SyntaxKind.FunctionDeclaration:
return "function";
case ts.SyntaxKind.GetAccessor:
return "get accessor";
case ts.SyntaxKind.SetAccessor:
return "set accessor";
case ts.SyntaxKind.ClassDeclaration:
return "class";
case ts.SyntaxKind.InterfaceDeclaration:
return "interface";
case ts.SyntaxKind.EnumDeclaration:
return "enum";
case ts.SyntaxKind.ModuleDeclaration:
return "module";
case ts.SyntaxKind.ImportDeclaration:
return "import";
}
}
function getQualifiedName(decl: ts.Declaration): string {
// TODO: should be revised when TS have local types
let curr: ts.Node = decl;
let name = "";
while (curr) {
switch (curr.kind) {
case ts.SyntaxKind.Constructor:
if (curr !== decl) {
return "constructor"
}
else {
name = "constructor";
}
curr = curr.parent;
break;
case ts.SyntaxKind.Parameter:
case ts.SyntaxKind.TypeParameter:
case ts.SyntaxKind.VariableDeclaration:
case ts.SyntaxKind.FunctionDeclaration:
return ts.declarationNameToString((<ts.Declaration>decl).name); // take a shortcut
case ts.SyntaxKind.GetAccessor:
case ts.SyntaxKind.SetAccessor:
case ts.SyntaxKind.PropertyDeclaration:
case ts.SyntaxKind.PropertySignature:
case ts.SyntaxKind.PropertyAssignment:
if (curr.parent && curr.parent.kind === ts.SyntaxKind.TypeLiteral) {
return ts.declarationNameToString((<ts.Declaration>decl).name); // take a shortcut
}
case ts.SyntaxKind.EnumMember:
case ts.SyntaxKind.ClassDeclaration:
case ts.SyntaxKind.InterfaceDeclaration:
case ts.SyntaxKind.EnumDeclaration:
case ts.SyntaxKind.ModuleDeclaration:
case ts.SyntaxKind.ImportDeclaration:
let currName = ts.declarationNameToString((<ts.Declaration>curr).name);
name = name.length ? currName + "." + name : currName;
default:
curr = curr.parent;
}
}
return name;
}
function convertClassifications(classifications: ts.ClassifiedSpan[], f: ts.SourceFile, addHyperlinks: boolean): ClassifiedRange[]{
let ranges: ClassifiedRange[] = [];
if (addHyperlinks) {
ts.forEach(f.referencedFiles, r => {
let hyperlinks = addHyperlinksForDefinition(r.pos, /*hyperlinks*/ undefined);
// push hyperlinks for tripleslash refs
if (hyperlinks) {
let range: ClassifiedRange = {
start: r.pos,
length: r.end - r.pos,
hyperlinks: hyperlinks,
classification: ts.ClassificationTypeNames.stringLiteral,
}
ranges.push(range);
}
});
}
ts.forEach(classifications, c => {
if (skipClassification(c.classificationType)) {
return;
}
let classification = c.classificationType;
let start = c.textSpan.start;
let length = c.textSpan.length;
let hyperlinks: Hyperlink[];
let definitionSymbolId: string;
let definitionKind: string;
let fullName: string;
let searchString: string;
if (addHyperlinks) {
switch (c.classificationType) {
case ts.ClassificationTypeNames.comment:
case ts.ClassificationTypeNames.operator:
case ts.ClassificationTypeNames.punctuation:
case ts.ClassificationTypeNames.whiteSpace:
break;
default:
let token = ts.getTokenAtPosition(f, start);
// yield definition info only for constructors
if (c.classificationType === ts.ClassificationTypeNames.keyword && token && token.kind !== ts.SyntaxKind.ConstructorKeyword) {
break;
}
let declaration = token && getDeclarationForName(token);
if (declaration) {
searchString = declaration.name && ts.declarationNameToString(declaration.name);
definitionKind = getDeclarationName(declaration);
fullName = getQualifiedName(declaration);
if (declaration.name) {
definitionSymbolId = makeSymbolId(f.fileName, declaration.name.getStart());
}
else {
definitionSymbolId = makeSymbolId(f.fileName, declaration.getStart());
}
}
else if (token.kind === ts.SyntaxKind.Identifier && token.parent && token.parent.kind === ts.SyntaxKind.LabeledStatement) {
// label
definitionKind = "label";
fullName = ts.declarationNameToString(<ts.Identifier>token);
definitionSymbolId = makeSymbolId(f.fileName, token.getStart());
}
else {
hyperlinks = addHyperlinksForDefinition(start, hyperlinks);
}
}
}
let converted: ClassifiedRange = {
classification: classification,
start: start,
length: length,
hyperlinks: hyperlinks,
definitionSymbolId: definitionSymbolId,
searchString: searchString,
fullName: fullName,
definitionKind: definitionKind
};
ranges.push(converted);
});
return ranges;
}
function addHyperlinksForDefinition(start: number, hyperlinks: Hyperlink[]): Hyperlink[] {
let defs = ls.getDefinitionAtPosition(f.fileName, start);
if (defs) {
ts.forEach(defs, d => {
if (!hyperlinks) {
hyperlinks = [];
}
let defStart = d.textSpan.start;
let defFile = program.getSourceFile(d.fileName);
let token = ts.getTouchingToken(defFile, defStart, /*includeItemAtEndPosition*/ undefined);
if (token && token.kind !== ts.SyntaxKind.SourceFile) {
// point definition to name if possible
let target =
ts.isDeclaration(token)
? (<ts.Declaration>token).name
: token.parent && ts.isDeclaration(token.parent)
? (<ts.Declaration>token.parent).name
: token;
if (target) {
defStart = target.getStart();
}
}
let link: Hyperlink = {
sourceFile: d.fileName,
start: d.textSpan.start,
symbolId: makeSymbolId(d.fileName, defStart)
};
hyperlinks.push(link);
});
}
return hyperlinks;
}
function makeSymbolId(fileName: string, start: number): string {
return fileName + "|" + start;
}
function createProgram(files: string[]): ts.Program {
let host: ts.CompilerHost = {
getSourceFile: (filename, languageVersion) => {
if (ts.sys.fileExists(filename)) {
let text = ts.sys.readFile(filename);
let sourceFile = ts.createSourceFile(filename, text, languageVersion);
sourceFile.version = sourceFileVersion;
return sourceFile;
}
},
getCancellationToken: () => cancellationToken,
getCanonicalFileName: (filename) => ts.sys.useCaseSensitiveFileNames ? filename : filename.toLowerCase(),
useCaseSensitiveFileNames: () => ts.sys.useCaseSensitiveFileNames,
getNewLine: () => "\r\n",
getDefaultLibFileName: (): string => {
return libFileName;
},
writeFile: (filename, data, writeByteOrderMark) => {
throw new Error("NYI");
},
getCurrentDirectory: (): string => {
return ts.sys.getCurrentDirectory();
},
fileExists: fileName => ts.sys.fileExists(fileName),
readFile: fileName => ts.sys.readFile(fileName)
};
return ts.createProgram(files, { target: ts.ScriptTarget.ES5 }, host)
}
}
function analyzeShim(json: string): boolean {
let args = <{ fileNames: string[]; libFile: string; outputFolder?: string }>JSON.parse(json);
let outputFolder = args.outputFolder || "output";
if (!ts.sys.directoryExists(outputFolder)) {
ts.sys.createDirectory(outputFolder);
}
try {
let results = analyze(args.libFile, args.fileNames, outputFolder);
ts.sys.writeFile(ts.combinePaths(outputFolder, "ok.json"), JSON.stringify(results));
return true;
}
catch (e) {
ts.sys.writeFile(ts.combinePaths(outputFolder, "error.json"), JSON.stringify({ message: e.message, stack: e.stack }));
return false;
}
}
declare let module: any;
declare let process: any;
if (typeof module !== "undefined" && module.exports && process.argv.length === 3) {
analyzeShim(ts.sys.readFile(process.argv[2]));
}
analyzeShim