1
|
"use strict";
|
2
|
/**
|
3
|
* @license
|
4
|
* Copyright Google Inc. All Rights Reserved.
|
5
|
*
|
6
|
* Use of this source code is governed by an MIT-style license that can be
|
7
|
* found in the LICENSE file at https://angular.io/license
|
8
|
*/
|
9
|
Object.defineProperty(exports, "__esModule", { value: true });
|
10
|
var fs = require("fs");
|
11
|
var path = require("path");
|
12
|
var ts = require("typescript");
|
13
|
var symbols_1 = require("./symbols");
|
14
|
// In TypeScript 2.1 these flags moved
|
15
|
// These helpers work for both 2.0 and 2.1.
|
16
|
var isPrivate = ts.ModifierFlags ?
|
17
|
(function (node) {
|
18
|
return !!(ts.getCombinedModifierFlags(node) & ts.ModifierFlags.Private);
|
19
|
}) :
|
20
|
(function (node) { return !!(node.flags & ts.NodeFlags.Private); });
|
21
|
var isReferenceType = ts.ObjectFlags ?
|
22
|
(function (type) {
|
23
|
return !!(type.flags & ts.TypeFlags.Object &&
|
24
|
type.objectFlags & ts.ObjectFlags.Reference);
|
25
|
}) :
|
26
|
(function (type) { return !!(type.flags & ts.TypeFlags.Reference); });
|
27
|
function getSymbolQuery(program, checker, source, fetchPipes) {
|
28
|
return new TypeScriptSymbolQuery(program, checker, source, fetchPipes);
|
29
|
}
|
30
|
exports.getSymbolQuery = getSymbolQuery;
|
31
|
function getClassMembers(program, checker, staticSymbol) {
|
32
|
var declaration = getClassFromStaticSymbol(program, staticSymbol);
|
33
|
if (declaration) {
|
34
|
var type = checker.getTypeAtLocation(declaration);
|
35
|
var node = program.getSourceFile(staticSymbol.filePath);
|
36
|
return new TypeWrapper(type, { node: node, program: program, checker: checker }).members();
|
37
|
}
|
38
|
}
|
39
|
exports.getClassMembers = getClassMembers;
|
40
|
function getClassMembersFromDeclaration(program, checker, source, declaration) {
|
41
|
var type = checker.getTypeAtLocation(declaration);
|
42
|
return new TypeWrapper(type, { node: source, program: program, checker: checker }).members();
|
43
|
}
|
44
|
exports.getClassMembersFromDeclaration = getClassMembersFromDeclaration;
|
45
|
function getClassFromStaticSymbol(program, type) {
|
46
|
var source = program.getSourceFile(type.filePath);
|
47
|
if (source) {
|
48
|
return ts.forEachChild(source, function (child) {
|
49
|
if (child.kind === ts.SyntaxKind.ClassDeclaration) {
|
50
|
var classDeclaration = child;
|
51
|
if (classDeclaration.name != null && classDeclaration.name.text === type.name) {
|
52
|
return classDeclaration;
|
53
|
}
|
54
|
}
|
55
|
});
|
56
|
}
|
57
|
return undefined;
|
58
|
}
|
59
|
exports.getClassFromStaticSymbol = getClassFromStaticSymbol;
|
60
|
function getPipesTable(source, program, checker, pipes) {
|
61
|
return new PipesTable(pipes, { program: program, checker: checker, node: source });
|
62
|
}
|
63
|
exports.getPipesTable = getPipesTable;
|
64
|
var TypeScriptSymbolQuery = (function () {
|
65
|
function TypeScriptSymbolQuery(program, checker, source, fetchPipes) {
|
66
|
this.program = program;
|
67
|
this.checker = checker;
|
68
|
this.source = source;
|
69
|
this.fetchPipes = fetchPipes;
|
70
|
this.typeCache = new Map();
|
71
|
}
|
72
|
TypeScriptSymbolQuery.prototype.getTypeKind = function (symbol) { return typeKindOf(this.getTsTypeOf(symbol)); };
|
73
|
TypeScriptSymbolQuery.prototype.getBuiltinType = function (kind) {
|
74
|
var result = this.typeCache.get(kind);
|
75
|
if (!result) {
|
76
|
var type = getBuiltinTypeFromTs(kind, { checker: this.checker, node: this.source, program: this.program });
|
77
|
result =
|
78
|
new TypeWrapper(type, { program: this.program, checker: this.checker, node: this.source });
|
79
|
this.typeCache.set(kind, result);
|
80
|
}
|
81
|
return result;
|
82
|
};
|
83
|
TypeScriptSymbolQuery.prototype.getTypeUnion = function () {
|
84
|
var types = [];
|
85
|
for (var _i = 0; _i < arguments.length; _i++) {
|
86
|
types[_i] = arguments[_i];
|
87
|
}
|
88
|
// No API exists so return any if the types are not all the same type.
|
89
|
var result = undefined;
|
90
|
if (types.length) {
|
91
|
result = types[0];
|
92
|
for (var i = 1; i < types.length; i++) {
|
93
|
if (types[i] != result) {
|
94
|
result = undefined;
|
95
|
break;
|
96
|
}
|
97
|
}
|
98
|
}
|
99
|
return result || this.getBuiltinType(symbols_1.BuiltinType.Any);
|
100
|
};
|
101
|
TypeScriptSymbolQuery.prototype.getArrayType = function (type) { return this.getBuiltinType(symbols_1.BuiltinType.Any); };
|
102
|
TypeScriptSymbolQuery.prototype.getElementType = function (type) {
|
103
|
if (type instanceof TypeWrapper) {
|
104
|
var elementType = getTypeParameterOf(type.tsType, 'Array');
|
105
|
if (elementType) {
|
106
|
return new TypeWrapper(elementType, type.context);
|
107
|
}
|
108
|
}
|
109
|
};
|
110
|
TypeScriptSymbolQuery.prototype.getNonNullableType = function (symbol) {
|
111
|
if (symbol instanceof TypeWrapper && (typeof this.checker.getNonNullableType == 'function')) {
|
112
|
var tsType = symbol.tsType;
|
113
|
var nonNullableType = this.checker.getNonNullableType(tsType);
|
114
|
if (nonNullableType != tsType) {
|
115
|
return new TypeWrapper(nonNullableType, symbol.context);
|
116
|
}
|
117
|
else if (nonNullableType == tsType) {
|
118
|
return symbol;
|
119
|
}
|
120
|
}
|
121
|
return this.getBuiltinType(symbols_1.BuiltinType.Any);
|
122
|
};
|
123
|
TypeScriptSymbolQuery.prototype.getPipes = function () {
|
124
|
var result = this.pipesCache;
|
125
|
if (!result) {
|
126
|
result = this.pipesCache = this.fetchPipes();
|
127
|
}
|
128
|
return result;
|
129
|
};
|
130
|
TypeScriptSymbolQuery.prototype.getTemplateContext = function (type) {
|
131
|
var context = { node: this.source, program: this.program, checker: this.checker };
|
132
|
var typeSymbol = findClassSymbolInContext(type, context);
|
133
|
if (typeSymbol) {
|
134
|
var contextType = this.getTemplateRefContextType(typeSymbol);
|
135
|
if (contextType)
|
136
|
return new SymbolWrapper(contextType, context).members();
|
137
|
}
|
138
|
};
|
139
|
TypeScriptSymbolQuery.prototype.getTypeSymbol = function (type) {
|
140
|
var context = { node: this.source, program: this.program, checker: this.checker };
|
141
|
var typeSymbol = findClassSymbolInContext(type, context);
|
142
|
return typeSymbol && new SymbolWrapper(typeSymbol, context);
|
143
|
};
|
144
|
TypeScriptSymbolQuery.prototype.createSymbolTable = function (symbols) {
|
145
|
var result = new MapSymbolTable();
|
146
|
result.addAll(symbols.map(function (s) { return new DeclaredSymbol(s); }));
|
147
|
return result;
|
148
|
};
|
149
|
TypeScriptSymbolQuery.prototype.mergeSymbolTable = function (symbolTables) {
|
150
|
var result = new MapSymbolTable();
|
151
|
for (var _i = 0, symbolTables_1 = symbolTables; _i < symbolTables_1.length; _i++) {
|
152
|
var symbolTable = symbolTables_1[_i];
|
153
|
result.addAll(symbolTable.values());
|
154
|
}
|
155
|
return result;
|
156
|
};
|
157
|
TypeScriptSymbolQuery.prototype.getSpanAt = function (line, column) {
|
158
|
return spanAt(this.source, line, column);
|
159
|
};
|
160
|
TypeScriptSymbolQuery.prototype.getTemplateRefContextType = function (typeSymbol) {
|
161
|
var type = this.checker.getTypeOfSymbolAtLocation(typeSymbol, this.source);
|
162
|
var constructor = type.symbol && type.symbol.members &&
|
163
|
getFromSymbolTable(type.symbol.members, '__constructor');
|
164
|
if (constructor) {
|
165
|
var constructorDeclaration = constructor.declarations[0];
|
166
|
for (var _i = 0, _a = constructorDeclaration.parameters; _i < _a.length; _i++) {
|
167
|
var parameter = _a[_i];
|
168
|
var type_1 = this.checker.getTypeAtLocation(parameter.type);
|
169
|
if (type_1.symbol.name == 'TemplateRef' && isReferenceType(type_1)) {
|
170
|
var typeReference = type_1;
|
171
|
if (typeReference.typeArguments.length === 1) {
|
172
|
return typeReference.typeArguments[0].symbol;
|
173
|
}
|
174
|
}
|
175
|
}
|
176
|
}
|
177
|
};
|
178
|
TypeScriptSymbolQuery.prototype.getTsTypeOf = function (symbol) {
|
179
|
var type = this.getTypeWrapper(symbol);
|
180
|
return type && type.tsType;
|
181
|
};
|
182
|
TypeScriptSymbolQuery.prototype.getTypeWrapper = function (symbol) {
|
183
|
var type = undefined;
|
184
|
if (symbol instanceof TypeWrapper) {
|
185
|
type = symbol;
|
186
|
}
|
187
|
else if (symbol.type instanceof TypeWrapper) {
|
188
|
type = symbol.type;
|
189
|
}
|
190
|
return type;
|
191
|
};
|
192
|
return TypeScriptSymbolQuery;
|
193
|
}());
|
194
|
function typeCallable(type) {
|
195
|
var signatures = type.getCallSignatures();
|
196
|
return signatures && signatures.length != 0;
|
197
|
}
|
198
|
function signaturesOf(type, context) {
|
199
|
return type.getCallSignatures().map(function (s) { return new SignatureWrapper(s, context); });
|
200
|
}
|
201
|
function selectSignature(type, context, types) {
|
202
|
// TODO: Do a better job of selecting the right signature.
|
203
|
var signatures = type.getCallSignatures();
|
204
|
return signatures.length ? new SignatureWrapper(signatures[0], context) : undefined;
|
205
|
}
|
206
|
var TypeWrapper = (function () {
|
207
|
function TypeWrapper(tsType, context) {
|
208
|
this.tsType = tsType;
|
209
|
this.context = context;
|
210
|
if (!tsType) {
|
211
|
throw Error('Internal: null type');
|
212
|
}
|
213
|
}
|
214
|
Object.defineProperty(TypeWrapper.prototype, "name", {
|
215
|
get: function () {
|
216
|
var symbol = this.tsType.symbol;
|
217
|
return (symbol && symbol.name) || '<anonymous>';
|
218
|
},
|
219
|
enumerable: true,
|
220
|
configurable: true
|
221
|
});
|
222
|
Object.defineProperty(TypeWrapper.prototype, "kind", {
|
223
|
get: function () { return 'type'; },
|
224
|
enumerable: true,
|
225
|
configurable: true
|
226
|
});
|
227
|
Object.defineProperty(TypeWrapper.prototype, "language", {
|
228
|
get: function () { return 'typescript'; },
|
229
|
enumerable: true,
|
230
|
configurable: true
|
231
|
});
|
232
|
Object.defineProperty(TypeWrapper.prototype, "type", {
|
233
|
get: function () { return undefined; },
|
234
|
enumerable: true,
|
235
|
configurable: true
|
236
|
});
|
237
|
Object.defineProperty(TypeWrapper.prototype, "container", {
|
238
|
get: function () { return undefined; },
|
239
|
enumerable: true,
|
240
|
configurable: true
|
241
|
});
|
242
|
Object.defineProperty(TypeWrapper.prototype, "public", {
|
243
|
get: function () { return true; },
|
244
|
enumerable: true,
|
245
|
configurable: true
|
246
|
});
|
247
|
Object.defineProperty(TypeWrapper.prototype, "callable", {
|
248
|
get: function () { return typeCallable(this.tsType); },
|
249
|
enumerable: true,
|
250
|
configurable: true
|
251
|
});
|
252
|
Object.defineProperty(TypeWrapper.prototype, "nullable", {
|
253
|
get: function () {
|
254
|
return this.context.checker.getNonNullableType(this.tsType) != this.tsType;
|
255
|
},
|
256
|
enumerable: true,
|
257
|
configurable: true
|
258
|
});
|
259
|
Object.defineProperty(TypeWrapper.prototype, "definition", {
|
260
|
get: function () { return definitionFromTsSymbol(this.tsType.getSymbol()); },
|
261
|
enumerable: true,
|
262
|
configurable: true
|
263
|
});
|
264
|
TypeWrapper.prototype.members = function () {
|
265
|
return new SymbolTableWrapper(this.tsType.getProperties(), this.context);
|
266
|
};
|
267
|
TypeWrapper.prototype.signatures = function () { return signaturesOf(this.tsType, this.context); };
|
268
|
TypeWrapper.prototype.selectSignature = function (types) {
|
269
|
return selectSignature(this.tsType, this.context, types);
|
270
|
};
|
271
|
TypeWrapper.prototype.indexed = function (argument) { return undefined; };
|
272
|
return TypeWrapper;
|
273
|
}());
|
274
|
var SymbolWrapper = (function () {
|
275
|
function SymbolWrapper(symbol, context) {
|
276
|
this.context = context;
|
277
|
this.symbol = symbol && context && (symbol.flags & ts.SymbolFlags.Alias) ?
|
278
|
context.checker.getAliasedSymbol(symbol) :
|
279
|
symbol;
|
280
|
}
|
281
|
Object.defineProperty(SymbolWrapper.prototype, "name", {
|
282
|
get: function () { return this.symbol.name; },
|
283
|
enumerable: true,
|
284
|
configurable: true
|
285
|
});
|
286
|
Object.defineProperty(SymbolWrapper.prototype, "kind", {
|
287
|
get: function () { return this.callable ? 'method' : 'property'; },
|
288
|
enumerable: true,
|
289
|
configurable: true
|
290
|
});
|
291
|
Object.defineProperty(SymbolWrapper.prototype, "language", {
|
292
|
get: function () { return 'typescript'; },
|
293
|
enumerable: true,
|
294
|
configurable: true
|
295
|
});
|
296
|
Object.defineProperty(SymbolWrapper.prototype, "type", {
|
297
|
get: function () { return new TypeWrapper(this.tsType, this.context); },
|
298
|
enumerable: true,
|
299
|
configurable: true
|
300
|
});
|
301
|
Object.defineProperty(SymbolWrapper.prototype, "container", {
|
302
|
get: function () { return getContainerOf(this.symbol, this.context); },
|
303
|
enumerable: true,
|
304
|
configurable: true
|
305
|
});
|
306
|
Object.defineProperty(SymbolWrapper.prototype, "public", {
|
307
|
get: function () {
|
308
|
// Symbols that are not explicitly made private are public.
|
309
|
return !isSymbolPrivate(this.symbol);
|
310
|
},
|
311
|
enumerable: true,
|
312
|
configurable: true
|
313
|
});
|
314
|
Object.defineProperty(SymbolWrapper.prototype, "callable", {
|
315
|
get: function () { return typeCallable(this.tsType); },
|
316
|
enumerable: true,
|
317
|
configurable: true
|
318
|
});
|
319
|
Object.defineProperty(SymbolWrapper.prototype, "nullable", {
|
320
|
get: function () { return false; },
|
321
|
enumerable: true,
|
322
|
configurable: true
|
323
|
});
|
324
|
Object.defineProperty(SymbolWrapper.prototype, "definition", {
|
325
|
get: function () { return definitionFromTsSymbol(this.symbol); },
|
326
|
enumerable: true,
|
327
|
configurable: true
|
328
|
});
|
329
|
SymbolWrapper.prototype.members = function () {
|
330
|
if (!this._members) {
|
331
|
if ((this.symbol.flags & (ts.SymbolFlags.Class | ts.SymbolFlags.Interface)) != 0) {
|
332
|
var declaredType = this.context.checker.getDeclaredTypeOfSymbol(this.symbol);
|
333
|
var typeWrapper = new TypeWrapper(declaredType, this.context);
|
334
|
this._members = typeWrapper.members();
|
335
|
}
|
336
|
else {
|
337
|
this._members = new SymbolTableWrapper(this.symbol.members, this.context);
|
338
|
}
|
339
|
}
|
340
|
return this._members;
|
341
|
};
|
342
|
SymbolWrapper.prototype.signatures = function () { return signaturesOf(this.tsType, this.context); };
|
343
|
SymbolWrapper.prototype.selectSignature = function (types) {
|
344
|
return selectSignature(this.tsType, this.context, types);
|
345
|
};
|
346
|
SymbolWrapper.prototype.indexed = function (argument) { return undefined; };
|
347
|
Object.defineProperty(SymbolWrapper.prototype, "tsType", {
|
348
|
get: function () {
|
349
|
var type = this._tsType;
|
350
|
if (!type) {
|
351
|
type = this._tsType =
|
352
|
this.context.checker.getTypeOfSymbolAtLocation(this.symbol, this.context.node);
|
353
|
}
|
354
|
return type;
|
355
|
},
|
356
|
enumerable: true,
|
357
|
configurable: true
|
358
|
});
|
359
|
return SymbolWrapper;
|
360
|
}());
|
361
|
var DeclaredSymbol = (function () {
|
362
|
function DeclaredSymbol(declaration) {
|
363
|
this.declaration = declaration;
|
364
|
}
|
365
|
Object.defineProperty(DeclaredSymbol.prototype, "name", {
|
366
|
get: function () { return this.declaration.name; },
|
367
|
enumerable: true,
|
368
|
configurable: true
|
369
|
});
|
370
|
Object.defineProperty(DeclaredSymbol.prototype, "kind", {
|
371
|
get: function () { return this.declaration.kind; },
|
372
|
enumerable: true,
|
373
|
configurable: true
|
374
|
});
|
375
|
Object.defineProperty(DeclaredSymbol.prototype, "language", {
|
376
|
get: function () { return 'ng-template'; },
|
377
|
enumerable: true,
|
378
|
configurable: true
|
379
|
});
|
380
|
Object.defineProperty(DeclaredSymbol.prototype, "container", {
|
381
|
get: function () { return undefined; },
|
382
|
enumerable: true,
|
383
|
configurable: true
|
384
|
});
|
385
|
Object.defineProperty(DeclaredSymbol.prototype, "type", {
|
386
|
get: function () { return this.declaration.type; },
|
387
|
enumerable: true,
|
388
|
configurable: true
|
389
|
});
|
390
|
Object.defineProperty(DeclaredSymbol.prototype, "callable", {
|
391
|
get: function () { return this.declaration.type.callable; },
|
392
|
enumerable: true,
|
393
|
configurable: true
|
394
|
});
|
395
|
Object.defineProperty(DeclaredSymbol.prototype, "nullable", {
|
396
|
get: function () { return false; },
|
397
|
enumerable: true,
|
398
|
configurable: true
|
399
|
});
|
400
|
Object.defineProperty(DeclaredSymbol.prototype, "public", {
|
401
|
get: function () { return true; },
|
402
|
enumerable: true,
|
403
|
configurable: true
|
404
|
});
|
405
|
Object.defineProperty(DeclaredSymbol.prototype, "definition", {
|
406
|
get: function () { return this.declaration.definition; },
|
407
|
enumerable: true,
|
408
|
configurable: true
|
409
|
});
|
410
|
DeclaredSymbol.prototype.members = function () { return this.declaration.type.members(); };
|
411
|
DeclaredSymbol.prototype.signatures = function () { return this.declaration.type.signatures(); };
|
412
|
DeclaredSymbol.prototype.selectSignature = function (types) {
|
413
|
return this.declaration.type.selectSignature(types);
|
414
|
};
|
415
|
DeclaredSymbol.prototype.indexed = function (argument) { return undefined; };
|
416
|
return DeclaredSymbol;
|
417
|
}());
|
418
|
var SignatureWrapper = (function () {
|
419
|
function SignatureWrapper(signature, context) {
|
420
|
this.signature = signature;
|
421
|
this.context = context;
|
422
|
}
|
423
|
Object.defineProperty(SignatureWrapper.prototype, "arguments", {
|
424
|
get: function () {
|
425
|
return new SymbolTableWrapper(this.signature.getParameters(), this.context);
|
426
|
},
|
427
|
enumerable: true,
|
428
|
configurable: true
|
429
|
});
|
430
|
Object.defineProperty(SignatureWrapper.prototype, "result", {
|
431
|
get: function () { return new TypeWrapper(this.signature.getReturnType(), this.context); },
|
432
|
enumerable: true,
|
433
|
configurable: true
|
434
|
});
|
435
|
return SignatureWrapper;
|
436
|
}());
|
437
|
var SignatureResultOverride = (function () {
|
438
|
function SignatureResultOverride(signature, resultType) {
|
439
|
this.signature = signature;
|
440
|
this.resultType = resultType;
|
441
|
}
|
442
|
Object.defineProperty(SignatureResultOverride.prototype, "arguments", {
|
443
|
get: function () { return this.signature.arguments; },
|
444
|
enumerable: true,
|
445
|
configurable: true
|
446
|
});
|
447
|
Object.defineProperty(SignatureResultOverride.prototype, "result", {
|
448
|
get: function () { return this.resultType; },
|
449
|
enumerable: true,
|
450
|
configurable: true
|
451
|
});
|
452
|
return SignatureResultOverride;
|
453
|
}());
|
454
|
var toSymbolTable = isTypescriptVersion('2.2') ?
|
455
|
(function (symbols) {
|
456
|
var result = new Map();
|
457
|
for (var _i = 0, symbols_2 = symbols; _i < symbols_2.length; _i++) {
|
458
|
var symbol = symbols_2[_i];
|
459
|
result.set(symbol.name, symbol);
|
460
|
}
|
461
|
return result;
|
462
|
}) :
|
463
|
(function (symbols) {
|
464
|
var result = {};
|
465
|
for (var _i = 0, symbols_3 = symbols; _i < symbols_3.length; _i++) {
|
466
|
var symbol = symbols_3[_i];
|
467
|
result[symbol.name] = symbol;
|
468
|
}
|
469
|
return result;
|
470
|
});
|
471
|
function toSymbols(symbolTable) {
|
472
|
if (!symbolTable)
|
473
|
return [];
|
474
|
var table = symbolTable;
|
475
|
if (typeof table.values === 'function') {
|
476
|
return Array.from(table.values());
|
477
|
}
|
478
|
var result = [];
|
479
|
var own = typeof table.hasOwnProperty === 'function' ?
|
480
|
function (name) { return table.hasOwnProperty(name); } :
|
481
|
function (name) { return !!table[name]; };
|
482
|
for (var name_1 in table) {
|
483
|
if (own(name_1)) {
|
484
|
result.push(table[name_1]);
|
485
|
}
|
486
|
}
|
487
|
return result;
|
488
|
}
|
489
|
var SymbolTableWrapper = (function () {
|
490
|
function SymbolTableWrapper(symbols, context) {
|
491
|
this.context = context;
|
492
|
symbols = symbols || [];
|
493
|
if (Array.isArray(symbols)) {
|
494
|
this.symbols = symbols;
|
495
|
this.symbolTable = toSymbolTable(symbols);
|
496
|
}
|
497
|
else {
|
498
|
this.symbols = toSymbols(symbols);
|
499
|
this.symbolTable = symbols;
|
500
|
}
|
501
|
}
|
502
|
Object.defineProperty(SymbolTableWrapper.prototype, "size", {
|
503
|
get: function () { return this.symbols.length; },
|
504
|
enumerable: true,
|
505
|
configurable: true
|
506
|
});
|
507
|
SymbolTableWrapper.prototype.get = function (key) {
|
508
|
var symbol = getFromSymbolTable(this.symbolTable, key);
|
509
|
return symbol ? new SymbolWrapper(symbol, this.context) : undefined;
|
510
|
};
|
511
|
SymbolTableWrapper.prototype.has = function (key) {
|
512
|
var table = this.symbolTable;
|
513
|
return (typeof table.has === 'function') ? table.has(key) : table[key] != null;
|
514
|
};
|
515
|
SymbolTableWrapper.prototype.values = function () {
|
516
|
var _this = this;
|
517
|
return this.symbols.map(function (s) { return new SymbolWrapper(s, _this.context); });
|
518
|
};
|
519
|
return SymbolTableWrapper;
|
520
|
}());
|
521
|
var MapSymbolTable = (function () {
|
522
|
function MapSymbolTable() {
|
523
|
this.map = new Map();
|
524
|
this._values = [];
|
525
|
}
|
526
|
Object.defineProperty(MapSymbolTable.prototype, "size", {
|
527
|
get: function () { return this.map.size; },
|
528
|
enumerable: true,
|
529
|
configurable: true
|
530
|
});
|
531
|
MapSymbolTable.prototype.get = function (key) { return this.map.get(key); };
|
532
|
MapSymbolTable.prototype.add = function (symbol) {
|
533
|
if (this.map.has(symbol.name)) {
|
534
|
var previous = this.map.get(symbol.name);
|
535
|
this._values[this._values.indexOf(previous)] = symbol;
|
536
|
}
|
537
|
this.map.set(symbol.name, symbol);
|
538
|
this._values.push(symbol);
|
539
|
};
|
540
|
MapSymbolTable.prototype.addAll = function (symbols) {
|
541
|
for (var _i = 0, symbols_4 = symbols; _i < symbols_4.length; _i++) {
|
542
|
var symbol = symbols_4[_i];
|
543
|
this.add(symbol);
|
544
|
}
|
545
|
};
|
546
|
MapSymbolTable.prototype.has = function (key) { return this.map.has(key); };
|
547
|
MapSymbolTable.prototype.values = function () {
|
548
|
// Switch to this.map.values once iterables are supported by the target language.
|
549
|
return this._values;
|
550
|
};
|
551
|
return MapSymbolTable;
|
552
|
}());
|
553
|
var PipesTable = (function () {
|
554
|
function PipesTable(pipes, context) {
|
555
|
this.pipes = pipes;
|
556
|
this.context = context;
|
557
|
}
|
558
|
Object.defineProperty(PipesTable.prototype, "size", {
|
559
|
get: function () { return this.pipes.length; },
|
560
|
enumerable: true,
|
561
|
configurable: true
|
562
|
});
|
563
|
PipesTable.prototype.get = function (key) {
|
564
|
var pipe = this.pipes.find(function (pipe) { return pipe.name == key; });
|
565
|
if (pipe) {
|
566
|
return new PipeSymbol(pipe, this.context);
|
567
|
}
|
568
|
};
|
569
|
PipesTable.prototype.has = function (key) { return this.pipes.find(function (pipe) { return pipe.name == key; }) != null; };
|
570
|
PipesTable.prototype.values = function () {
|
571
|
var _this = this;
|
572
|
return this.pipes.map(function (pipe) { return new PipeSymbol(pipe, _this.context); });
|
573
|
};
|
574
|
return PipesTable;
|
575
|
}());
|
576
|
var PipeSymbol = (function () {
|
577
|
function PipeSymbol(pipe, context) {
|
578
|
this.pipe = pipe;
|
579
|
this.context = context;
|
580
|
}
|
581
|
Object.defineProperty(PipeSymbol.prototype, "name", {
|
582
|
get: function () { return this.pipe.name; },
|
583
|
enumerable: true,
|
584
|
configurable: true
|
585
|
});
|
586
|
Object.defineProperty(PipeSymbol.prototype, "kind", {
|
587
|
get: function () { return 'pipe'; },
|
588
|
enumerable: true,
|
589
|
configurable: true
|
590
|
});
|
591
|
Object.defineProperty(PipeSymbol.prototype, "language", {
|
592
|
get: function () { return 'typescript'; },
|
593
|
enumerable: true,
|
594
|
configurable: true
|
595
|
});
|
596
|
Object.defineProperty(PipeSymbol.prototype, "type", {
|
597
|
get: function () { return new TypeWrapper(this.tsType, this.context); },
|
598
|
enumerable: true,
|
599
|
configurable: true
|
600
|
});
|
601
|
Object.defineProperty(PipeSymbol.prototype, "container", {
|
602
|
get: function () { return undefined; },
|
603
|
enumerable: true,
|
604
|
configurable: true
|
605
|
});
|
606
|
Object.defineProperty(PipeSymbol.prototype, "callable", {
|
607
|
get: function () { return true; },
|
608
|
enumerable: true,
|
609
|
configurable: true
|
610
|
});
|
611
|
Object.defineProperty(PipeSymbol.prototype, "nullable", {
|
612
|
get: function () { return false; },
|
613
|
enumerable: true,
|
614
|
configurable: true
|
615
|
});
|
616
|
Object.defineProperty(PipeSymbol.prototype, "public", {
|
617
|
get: function () { return true; },
|
618
|
enumerable: true,
|
619
|
configurable: true
|
620
|
});
|
621
|
Object.defineProperty(PipeSymbol.prototype, "definition", {
|
622
|
get: function () { return definitionFromTsSymbol(this.tsType.getSymbol()); },
|
623
|
enumerable: true,
|
624
|
configurable: true
|
625
|
});
|
626
|
PipeSymbol.prototype.members = function () { return EmptyTable.instance; };
|
627
|
PipeSymbol.prototype.signatures = function () { return signaturesOf(this.tsType, this.context); };
|
628
|
PipeSymbol.prototype.selectSignature = function (types) {
|
629
|
var signature = selectSignature(this.tsType, this.context, types);
|
630
|
if (types.length == 1) {
|
631
|
var parameterType = types[0];
|
632
|
if (parameterType instanceof TypeWrapper) {
|
633
|
var resultType = undefined;
|
634
|
switch (this.name) {
|
635
|
case 'async':
|
636
|
switch (parameterType.name) {
|
637
|
case 'Observable':
|
638
|
case 'Promise':
|
639
|
case 'EventEmitter':
|
640
|
resultType = getTypeParameterOf(parameterType.tsType, parameterType.name);
|
641
|
break;
|
642
|
default:
|
643
|
resultType = getBuiltinTypeFromTs(symbols_1.BuiltinType.Any, this.context);
|
644
|
break;
|
645
|
}
|
646
|
break;
|
647
|
case 'slice':
|
648
|
resultType = getTypeParameterOf(parameterType.tsType, 'Array');
|
649
|
break;
|
650
|
}
|
651
|
if (resultType) {
|
652
|
signature = new SignatureResultOverride(signature, new TypeWrapper(resultType, parameterType.context));
|
653
|
}
|
654
|
}
|
655
|
}
|
656
|
return signature;
|
657
|
};
|
658
|
PipeSymbol.prototype.indexed = function (argument) { return undefined; };
|
659
|
Object.defineProperty(PipeSymbol.prototype, "tsType", {
|
660
|
get: function () {
|
661
|
var type = this._tsType;
|
662
|
if (!type) {
|
663
|
var classSymbol = this.findClassSymbol(this.pipe.type.reference);
|
664
|
if (classSymbol) {
|
665
|
type = this._tsType = this.findTransformMethodType(classSymbol);
|
666
|
}
|
667
|
if (!type) {
|
668
|
type = this._tsType = getBuiltinTypeFromTs(symbols_1.BuiltinType.Any, this.context);
|
669
|
}
|
670
|
}
|
671
|
return type;
|
672
|
},
|
673
|
enumerable: true,
|
674
|
configurable: true
|
675
|
});
|
676
|
PipeSymbol.prototype.findClassSymbol = function (type) {
|
677
|
return findClassSymbolInContext(type, this.context);
|
678
|
};
|
679
|
PipeSymbol.prototype.findTransformMethodType = function (classSymbol) {
|
680
|
var classType = this.context.checker.getDeclaredTypeOfSymbol(classSymbol);
|
681
|
if (classType) {
|
682
|
var transform = classType.getProperty('transform');
|
683
|
if (transform) {
|
684
|
return this.context.checker.getTypeOfSymbolAtLocation(transform, this.context.node);
|
685
|
}
|
686
|
}
|
687
|
};
|
688
|
return PipeSymbol;
|
689
|
}());
|
690
|
function findClassSymbolInContext(type, context) {
|
691
|
var sourceFile = context.program.getSourceFile(type.filePath);
|
692
|
if (sourceFile) {
|
693
|
var moduleSymbol = sourceFile.module || sourceFile.symbol;
|
694
|
var exports_1 = context.checker.getExportsOfModule(moduleSymbol);
|
695
|
return (exports_1 || []).find(function (symbol) { return symbol.name == type.name; });
|
696
|
}
|
697
|
}
|
698
|
var EmptyTable = (function () {
|
699
|
function EmptyTable() {
|
700
|
}
|
701
|
Object.defineProperty(EmptyTable.prototype, "size", {
|
702
|
get: function () { return 0; },
|
703
|
enumerable: true,
|
704
|
configurable: true
|
705
|
});
|
706
|
EmptyTable.prototype.get = function (key) { return undefined; };
|
707
|
EmptyTable.prototype.has = function (key) { return false; };
|
708
|
EmptyTable.prototype.values = function () { return []; };
|
709
|
return EmptyTable;
|
710
|
}());
|
711
|
EmptyTable.instance = new EmptyTable();
|
712
|
function findTsConfig(fileName) {
|
713
|
var dir = path.dirname(fileName);
|
714
|
while (fs.existsSync(dir)) {
|
715
|
var candidate = path.join(dir, 'tsconfig.json');
|
716
|
if (fs.existsSync(candidate))
|
717
|
return candidate;
|
718
|
var parentDir = path.dirname(dir);
|
719
|
if (parentDir === dir)
|
720
|
break;
|
721
|
dir = parentDir;
|
722
|
}
|
723
|
}
|
724
|
function isBindingPattern(node) {
|
725
|
return !!node && (node.kind === ts.SyntaxKind.ArrayBindingPattern ||
|
726
|
node.kind === ts.SyntaxKind.ObjectBindingPattern);
|
727
|
}
|
728
|
function walkUpBindingElementsAndPatterns(node) {
|
729
|
while (node && (node.kind === ts.SyntaxKind.BindingElement || isBindingPattern(node))) {
|
730
|
node = node.parent;
|
731
|
}
|
732
|
return node;
|
733
|
}
|
734
|
function getCombinedNodeFlags(node) {
|
735
|
node = walkUpBindingElementsAndPatterns(node);
|
736
|
var flags = node.flags;
|
737
|
if (node.kind === ts.SyntaxKind.VariableDeclaration) {
|
738
|
node = node.parent;
|
739
|
}
|
740
|
if (node && node.kind === ts.SyntaxKind.VariableDeclarationList) {
|
741
|
flags |= node.flags;
|
742
|
node = node.parent;
|
743
|
}
|
744
|
if (node && node.kind === ts.SyntaxKind.VariableStatement) {
|
745
|
flags |= node.flags;
|
746
|
}
|
747
|
return flags;
|
748
|
}
|
749
|
function isSymbolPrivate(s) {
|
750
|
return !!s.valueDeclaration && isPrivate(s.valueDeclaration);
|
751
|
}
|
752
|
function getBuiltinTypeFromTs(kind, context) {
|
753
|
var type;
|
754
|
var checker = context.checker;
|
755
|
var node = context.node;
|
756
|
switch (kind) {
|
757
|
case symbols_1.BuiltinType.Any:
|
758
|
type = checker.getTypeAtLocation(setParents({
|
759
|
kind: ts.SyntaxKind.AsExpression,
|
760
|
expression: { kind: ts.SyntaxKind.TrueKeyword },
|
761
|
type: { kind: ts.SyntaxKind.AnyKeyword }
|
762
|
}, node));
|
763
|
break;
|
764
|
case symbols_1.BuiltinType.Boolean:
|
765
|
type =
|
766
|
checker.getTypeAtLocation(setParents({ kind: ts.SyntaxKind.TrueKeyword }, node));
|
767
|
break;
|
768
|
case symbols_1.BuiltinType.Null:
|
769
|
type =
|
770
|
checker.getTypeAtLocation(setParents({ kind: ts.SyntaxKind.NullKeyword }, node));
|
771
|
break;
|
772
|
case symbols_1.BuiltinType.Number:
|
773
|
var numeric = { kind: ts.SyntaxKind.NumericLiteral };
|
774
|
setParents({ kind: ts.SyntaxKind.ExpressionStatement, expression: numeric }, node);
|
775
|
type = checker.getTypeAtLocation(numeric);
|
776
|
break;
|
777
|
case symbols_1.BuiltinType.String:
|
778
|
type = checker.getTypeAtLocation(setParents({ kind: ts.SyntaxKind.NoSubstitutionTemplateLiteral }, node));
|
779
|
break;
|
780
|
case symbols_1.BuiltinType.Undefined:
|
781
|
type = checker.getTypeAtLocation(setParents({
|
782
|
kind: ts.SyntaxKind.VoidExpression,
|
783
|
expression: { kind: ts.SyntaxKind.NumericLiteral }
|
784
|
}, node));
|
785
|
break;
|
786
|
default:
|
787
|
throw new Error("Internal error, unhandled literal kind " + kind + ":" + symbols_1.BuiltinType[kind]);
|
788
|
}
|
789
|
return type;
|
790
|
}
|
791
|
function setParents(node, parent) {
|
792
|
node.parent = parent;
|
793
|
ts.forEachChild(node, function (child) { return setParents(child, node); });
|
794
|
return node;
|
795
|
}
|
796
|
function spanOf(node) {
|
797
|
return { start: node.getStart(), end: node.getEnd() };
|
798
|
}
|
799
|
function shrink(span, offset) {
|
800
|
if (offset == null)
|
801
|
offset = 1;
|
802
|
return { start: span.start + offset, end: span.end - offset };
|
803
|
}
|
804
|
function spanAt(sourceFile, line, column) {
|
805
|
if (line != null && column != null) {
|
806
|
var position_1 = ts.getPositionOfLineAndCharacter(sourceFile, line, column);
|
807
|
var findChild = function findChild(node) {
|
808
|
if (node.kind > ts.SyntaxKind.LastToken && node.pos <= position_1 && node.end > position_1) {
|
809
|
var betterNode = ts.forEachChild(node, findChild);
|
810
|
return betterNode || node;
|
811
|
}
|
812
|
};
|
813
|
var node = ts.forEachChild(sourceFile, findChild);
|
814
|
if (node) {
|
815
|
return { start: node.getStart(), end: node.getEnd() };
|
816
|
}
|
817
|
}
|
818
|
}
|
819
|
function definitionFromTsSymbol(symbol) {
|
820
|
var declarations = symbol.declarations;
|
821
|
if (declarations) {
|
822
|
return declarations.map(function (declaration) {
|
823
|
var sourceFile = declaration.getSourceFile();
|
824
|
return {
|
825
|
fileName: sourceFile.fileName,
|
826
|
span: { start: declaration.getStart(), end: declaration.getEnd() }
|
827
|
};
|
828
|
});
|
829
|
}
|
830
|
}
|
831
|
function parentDeclarationOf(node) {
|
832
|
while (node) {
|
833
|
switch (node.kind) {
|
834
|
case ts.SyntaxKind.ClassDeclaration:
|
835
|
case ts.SyntaxKind.InterfaceDeclaration:
|
836
|
return node;
|
837
|
case ts.SyntaxKind.SourceFile:
|
838
|
return undefined;
|
839
|
}
|
840
|
node = node.parent;
|
841
|
}
|
842
|
}
|
843
|
function getContainerOf(symbol, context) {
|
844
|
if (symbol.getFlags() & ts.SymbolFlags.ClassMember && symbol.declarations) {
|
845
|
for (var _i = 0, _a = symbol.declarations; _i < _a.length; _i++) {
|
846
|
var declaration = _a[_i];
|
847
|
var parent_1 = parentDeclarationOf(declaration);
|
848
|
if (parent_1) {
|
849
|
var type = context.checker.getTypeAtLocation(parent_1);
|
850
|
if (type) {
|
851
|
return new TypeWrapper(type, context);
|
852
|
}
|
853
|
}
|
854
|
}
|
855
|
}
|
856
|
}
|
857
|
function getTypeParameterOf(type, name) {
|
858
|
if (type && type.symbol && type.symbol.name == name) {
|
859
|
var typeArguments = type.typeArguments;
|
860
|
if (typeArguments && typeArguments.length <= 1) {
|
861
|
return typeArguments[0];
|
862
|
}
|
863
|
}
|
864
|
}
|
865
|
function typeKindOf(type) {
|
866
|
if (type) {
|
867
|
if (type.flags & ts.TypeFlags.Any) {
|
868
|
return symbols_1.BuiltinType.Any;
|
869
|
}
|
870
|
else if (type.flags & (ts.TypeFlags.String | ts.TypeFlags.StringLike | ts.TypeFlags.StringLiteral)) {
|
871
|
return symbols_1.BuiltinType.String;
|
872
|
}
|
873
|
else if (type.flags & (ts.TypeFlags.Number | ts.TypeFlags.NumberLike)) {
|
874
|
return symbols_1.BuiltinType.Number;
|
875
|
}
|
876
|
else if (type.flags & (ts.TypeFlags.Undefined)) {
|
877
|
return symbols_1.BuiltinType.Undefined;
|
878
|
}
|
879
|
else if (type.flags & (ts.TypeFlags.Null)) {
|
880
|
return symbols_1.BuiltinType.Null;
|
881
|
}
|
882
|
else if (type.flags & ts.TypeFlags.Union) {
|
883
|
// If all the constituent types of a union are the same kind, it is also that kind.
|
884
|
var candidate = null;
|
885
|
var unionType = type;
|
886
|
if (unionType.types.length > 0) {
|
887
|
candidate = typeKindOf(unionType.types[0]);
|
888
|
for (var _i = 0, _a = unionType.types; _i < _a.length; _i++) {
|
889
|
var subType = _a[_i];
|
890
|
if (candidate != typeKindOf(subType)) {
|
891
|
return symbols_1.BuiltinType.Other;
|
892
|
}
|
893
|
}
|
894
|
}
|
895
|
if (candidate != null) {
|
896
|
return candidate;
|
897
|
}
|
898
|
}
|
899
|
else if (type.flags & ts.TypeFlags.TypeParameter) {
|
900
|
return symbols_1.BuiltinType.Unbound;
|
901
|
}
|
902
|
}
|
903
|
return symbols_1.BuiltinType.Other;
|
904
|
}
|
905
|
function getFromSymbolTable(symbolTable, key) {
|
906
|
var table = symbolTable;
|
907
|
var symbol;
|
908
|
if (typeof table.get === 'function') {
|
909
|
// TS 2.2 uses a Map
|
910
|
symbol = table.get(key);
|
911
|
}
|
912
|
else {
|
913
|
// TS pre-2.2 uses an object
|
914
|
symbol = table[key];
|
915
|
}
|
916
|
return symbol;
|
917
|
}
|
918
|
function toNumbers(value) {
|
919
|
return value ? value.split('.').map(function (v) { return +v; }) : [];
|
920
|
}
|
921
|
function compareNumbers(a, b) {
|
922
|
for (var i = 0; i < a.length && i < b.length; i++) {
|
923
|
if (a[i] > b[i])
|
924
|
return 1;
|
925
|
if (a[i] < b[i])
|
926
|
return -1;
|
927
|
}
|
928
|
return 0;
|
929
|
}
|
930
|
function isTypescriptVersion(low, high) {
|
931
|
var tsNumbers = toNumbers(ts.version);
|
932
|
return compareNumbers(toNumbers(low), tsNumbers) <= 0 &&
|
933
|
compareNumbers(toNumbers(high), tsNumbers) >= 0;
|
934
|
}
|
935
|
//# sourceMappingURL=typescript_symbols.js.map
|