@angular/language-service
Version:
Angular - language services
1,071 lines • 122 kB
JavaScript
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
(function (factory) {
if (typeof module === "object" && typeof module.exports === "object") {
var v = factory(require, exports);
if (v !== undefined) module.exports = v;
}
else if (typeof define === "function" && define.amd) {
define("@angular/language-service/src/typescript_symbols", ["require", "exports", "tslib", "path", "typescript", "@angular/language-service/src/symbols"], factory);
}
})(function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPipesTable = exports.getClassMembersFromDeclaration = exports.getClassMembers = exports.getSymbolQuery = void 0;
var tslib_1 = require("tslib");
var path = require("path");
var ts = require("typescript");
var symbols_1 = require("@angular/language-service/src/symbols");
// In TypeScript 2.1 these flags moved
// These helpers work for both 2.0 and 2.1.
var isPrivate = ts.ModifierFlags ?
(function (node) {
return !!(ts.getCombinedModifierFlags(node) & ts.ModifierFlags.Private);
}) :
(function (node) { return !!(node.flags & ts.NodeFlags.Private); });
var isReferenceType = ts.ObjectFlags ?
(function (type) {
return !!(type.flags & ts.TypeFlags.Object &&
type.objectFlags & ts.ObjectFlags.Reference);
}) :
(function (type) { return !!(type.flags & ts.TypeFlags.Reference); });
function getSymbolQuery(program, checker, source, fetchPipes) {
return new TypeScriptSymbolQuery(program, checker, source, fetchPipes);
}
exports.getSymbolQuery = getSymbolQuery;
function getClassMembers(program, checker, staticSymbol) {
var declaration = getClassFromStaticSymbol(program, staticSymbol);
if (declaration) {
var type = checker.getTypeAtLocation(declaration);
var node = program.getSourceFile(staticSymbol.filePath);
if (node) {
return new TypeWrapper(type, { node: node, program: program, checker: checker }).members();
}
}
}
exports.getClassMembers = getClassMembers;
function getClassMembersFromDeclaration(program, checker, source, declaration) {
var type = checker.getTypeAtLocation(declaration);
return new TypeWrapper(type, { node: source, program: program, checker: checker }).members();
}
exports.getClassMembersFromDeclaration = getClassMembersFromDeclaration;
function getPipesTable(source, program, checker, pipes) {
return new PipesTable(pipes, { program: program, checker: checker, node: source });
}
exports.getPipesTable = getPipesTable;
function getClassFromStaticSymbol(program, type) {
var source = program.getSourceFile(type.filePath);
if (source) {
return ts.forEachChild(source, function (child) {
if (child.kind === ts.SyntaxKind.ClassDeclaration) {
var classDeclaration = child;
if (classDeclaration.name != null && classDeclaration.name.text === type.name) {
return classDeclaration;
}
}
});
}
return undefined;
}
var TypeScriptSymbolQuery = /** @class */ (function () {
function TypeScriptSymbolQuery(program, checker, source, fetchPipes) {
this.program = program;
this.checker = checker;
this.source = source;
this.fetchPipes = fetchPipes;
this.typeCache = new Map();
}
TypeScriptSymbolQuery.prototype.getTypeKind = function (symbol) {
var type = symbol instanceof TypeWrapper ? symbol.tsType : undefined;
return typeKindOf(type);
};
TypeScriptSymbolQuery.prototype.getBuiltinType = function (kind) {
var result = this.typeCache.get(kind);
if (!result) {
var type = getTsTypeFromBuiltinType(kind, {
checker: this.checker,
node: this.source,
program: this.program,
});
result =
new TypeWrapper(type, { program: this.program, checker: this.checker, node: this.source });
this.typeCache.set(kind, result);
}
return result;
};
TypeScriptSymbolQuery.prototype.getTypeUnion = function () {
var types = [];
for (var _i = 0; _i < arguments.length; _i++) {
types[_i] = arguments[_i];
}
// No API exists so return any if the types are not all the same type.
var result = undefined;
if (types.length) {
result = types[0];
for (var i = 1; i < types.length; i++) {
if (types[i] != result) {
result = undefined;
break;
}
}
}
return result || this.getBuiltinType(symbols_1.BuiltinType.Any);
};
TypeScriptSymbolQuery.prototype.getArrayType = function (_type) {
return this.getBuiltinType(symbols_1.BuiltinType.Any);
};
TypeScriptSymbolQuery.prototype.getElementType = function (type) {
if (type instanceof TypeWrapper) {
var ty = type.tsType;
var tyArgs = type.typeArguments();
// TODO(ayazhafiz): Track https://github.com/microsoft/TypeScript/issues/37711 to expose
// `isArrayLikeType` as a public method.
if (!this.checker.isArrayLikeType(ty) || (tyArgs === null || tyArgs === void 0 ? void 0 : tyArgs.length) !== 1)
return;
return tyArgs[0];
}
};
TypeScriptSymbolQuery.prototype.getNonNullableType = function (symbol) {
if (symbol instanceof TypeWrapper && (typeof this.checker.getNonNullableType == 'function')) {
var tsType = symbol.tsType;
var nonNullableType = this.checker.getNonNullableType(tsType);
if (nonNullableType != tsType) {
return new TypeWrapper(nonNullableType, symbol.context);
}
else if (nonNullableType == tsType) {
return symbol;
}
}
return this.getBuiltinType(symbols_1.BuiltinType.Any);
};
TypeScriptSymbolQuery.prototype.getPipes = function () {
var result = this.pipesCache;
if (!result) {
result = this.pipesCache = this.fetchPipes();
}
return result;
};
TypeScriptSymbolQuery.prototype.getTemplateContext = function (type) {
var context = { node: this.source, program: this.program, checker: this.checker };
var typeSymbol = findClassSymbolInContext(type, context);
if (typeSymbol) {
var contextType = this.getTemplateRefContextType(typeSymbol, context);
if (contextType)
return contextType.members();
}
};
TypeScriptSymbolQuery.prototype.getTypeSymbol = function (type) {
var context = { node: this.source, program: this.program, checker: this.checker };
var typeSymbol = findClassSymbolInContext(type, context);
return typeSymbol && new SymbolWrapper(typeSymbol, context);
};
TypeScriptSymbolQuery.prototype.createSymbolTable = function (symbols) {
var result = new MapSymbolTable();
result.addAll(symbols.map(function (s) { return new DeclaredSymbol(s); }));
return result;
};
TypeScriptSymbolQuery.prototype.mergeSymbolTable = function (symbolTables) {
var e_1, _a;
var result = new MapSymbolTable();
try {
for (var symbolTables_1 = tslib_1.__values(symbolTables), symbolTables_1_1 = symbolTables_1.next(); !symbolTables_1_1.done; symbolTables_1_1 = symbolTables_1.next()) {
var symbolTable = symbolTables_1_1.value;
result.addAll(symbolTable.values());
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (symbolTables_1_1 && !symbolTables_1_1.done && (_a = symbolTables_1.return)) _a.call(symbolTables_1);
}
finally { if (e_1) throw e_1.error; }
}
return result;
};
TypeScriptSymbolQuery.prototype.getSpanAt = function (line, column) {
return spanAt(this.source, line, column);
};
TypeScriptSymbolQuery.prototype.getTemplateRefContextType = function (typeSymbol, context) {
var e_2, _a;
var type = this.checker.getTypeOfSymbolAtLocation(typeSymbol, this.source);
var constructor = type.symbol && type.symbol.members &&
getFromSymbolTable(type.symbol.members, '__constructor');
if (constructor) {
var constructorDeclaration = constructor.declarations[0];
try {
for (var _b = tslib_1.__values(constructorDeclaration.parameters), _c = _b.next(); !_c.done; _c = _b.next()) {
var parameter = _c.value;
var type_1 = this.checker.getTypeAtLocation(parameter.type);
if (type_1.symbol.name == 'TemplateRef' && isReferenceType(type_1)) {
var typeWrapper = new TypeWrapper(type_1, context);
var typeArguments = typeWrapper.typeArguments();
if (typeArguments && typeArguments.length === 1) {
return typeArguments[0];
}
}
}
}
catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_2) throw e_2.error; }
}
}
};
return TypeScriptSymbolQuery;
}());
function typeCallable(type) {
var signatures = type.getCallSignatures();
return signatures && signatures.length != 0;
}
function signaturesOf(type, context) {
return type.getCallSignatures().map(function (s) { return new SignatureWrapper(s, context); });
}
function selectSignature(type, context, types) {
// TODO: Do a better job of selecting the right signature. TypeScript does not currently support a
// Type Relationship API (see https://github.com/angular/vscode-ng-language-service/issues/143).
// Consider creating a TypeCheckBlock host in the language service that may also act as a
// scratchpad for type comparisons.
var signatures = type.getCallSignatures();
var passedInTypes = types.map(function (type) {
if (type instanceof TypeWrapper) {
return type.tsType;
}
});
// Try to select a matching signature in which all parameter types match.
// Note that this is just a best-effort approach, because we're checking for
// strict type equality rather than compatibility.
// For example, if the signature contains a ReadonlyArray<number> and the
// passed parameter type is an Array<number>, this will fail.
function allParameterTypesMatch(signature) {
var tc = context.checker;
return signature.getParameters().every(function (parameter, i) {
var type = tc.getTypeOfSymbolAtLocation(parameter, parameter.valueDeclaration);
return type === passedInTypes[i];
});
}
var exactMatch = signatures.find(allParameterTypesMatch);
if (exactMatch) {
return new SignatureWrapper(exactMatch, context);
}
// If not, fallback to a naive selection
return signatures.length ? new SignatureWrapper(signatures[0], context) : undefined;
}
var TypeWrapper = /** @class */ (function () {
function TypeWrapper(tsType, context) {
this.tsType = tsType;
this.context = context;
this.kind = 'type';
this.language = 'typescript';
this.type = undefined;
this.container = undefined;
this.public = true;
if (!tsType) {
throw Error('Internal: null type');
}
}
Object.defineProperty(TypeWrapper.prototype, "name", {
get: function () {
return this.context.checker.typeToString(this.tsType);
},
enumerable: false,
configurable: true
});
Object.defineProperty(TypeWrapper.prototype, "callable", {
get: function () {
return typeCallable(this.tsType);
},
enumerable: false,
configurable: true
});
Object.defineProperty(TypeWrapper.prototype, "nullable", {
get: function () {
return this.context.checker.getNonNullableType(this.tsType) != this.tsType;
},
enumerable: false,
configurable: true
});
Object.defineProperty(TypeWrapper.prototype, "documentation", {
get: function () {
var symbol = this.tsType.getSymbol();
if (!symbol) {
return [];
}
return symbol.getDocumentationComment(this.context.checker);
},
enumerable: false,
configurable: true
});
Object.defineProperty(TypeWrapper.prototype, "definition", {
get: function () {
var symbol = this.tsType.getSymbol();
return symbol ? definitionFromTsSymbol(symbol) : undefined;
},
enumerable: false,
configurable: true
});
TypeWrapper.prototype.members = function () {
// Should call getApparentProperties() instead of getProperties() because
// the former includes properties on the base class whereas the latter does
// not. This provides properties like .bind(), .call(), .apply(), etc for
// functions.
return new SymbolTableWrapper(this.tsType.getApparentProperties(), this.context, this.tsType);
};
TypeWrapper.prototype.signatures = function () {
return signaturesOf(this.tsType, this.context);
};
TypeWrapper.prototype.selectSignature = function (types) {
return selectSignature(this.tsType, this.context, types);
};
TypeWrapper.prototype.indexed = function (type, value) {
if (!(type instanceof TypeWrapper))
return;
var typeKind = typeKindOf(type.tsType);
switch (typeKind) {
case symbols_1.BuiltinType.Number:
var nType = this.tsType.getNumberIndexType();
if (nType) {
// get the right tuple type by value, like 'var t: [number, string];'
if (nType.isUnion()) {
// return undefined if array index out of bound.
return nType.types[value] && new TypeWrapper(nType.types[value], this.context);
}
return new TypeWrapper(nType, this.context);
}
return undefined;
case symbols_1.BuiltinType.String:
var sType = this.tsType.getStringIndexType();
return sType && new TypeWrapper(sType, this.context);
}
};
TypeWrapper.prototype.typeArguments = function () {
var _this = this;
if (!isReferenceType(this.tsType))
return;
var typeReference = this.tsType;
var typeArguments;
typeArguments = this.context.checker.getTypeArguments(typeReference);
if (!typeArguments)
return undefined;
return typeArguments.map(function (ta) { return new TypeWrapper(ta, _this.context); });
};
return TypeWrapper;
}());
// If stringIndexType a primitive type(e.g. 'string'), the Symbol is undefined;
// and in AstType.resolvePropertyRead method, the Symbol.type should get the right type.
var StringIndexTypeWrapper = /** @class */ (function (_super) {
tslib_1.__extends(StringIndexTypeWrapper, _super);
function StringIndexTypeWrapper() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.type = new TypeWrapper(_this.tsType, _this.context);
return _this;
}
return StringIndexTypeWrapper;
}(TypeWrapper));
var SymbolWrapper = /** @class */ (function () {
function SymbolWrapper(symbol,
/** TypeScript type context of the symbol. */
context,
/**
* Type of the TypeScript symbol, if known. If not provided, the type of the symbol
* will be determined dynamically; see `SymbolWrapper#tsType`.
*/
_tsType) {
this.context = context;
this._tsType = _tsType;
this.nullable = false;
this.language = 'typescript';
this.symbol = symbol && context && (symbol.flags & ts.SymbolFlags.Alias) ?
context.checker.getAliasedSymbol(symbol) :
symbol;
}
Object.defineProperty(SymbolWrapper.prototype, "name", {
get: function () {
return this.symbol.name;
},
enumerable: false,
configurable: true
});
Object.defineProperty(SymbolWrapper.prototype, "kind", {
get: function () {
return this.callable ? 'method' : 'property';
},
enumerable: false,
configurable: true
});
Object.defineProperty(SymbolWrapper.prototype, "type", {
get: function () {
return new TypeWrapper(this.tsType, this.context);
},
enumerable: false,
configurable: true
});
Object.defineProperty(SymbolWrapper.prototype, "container", {
get: function () {
return getContainerOf(this.symbol, this.context);
},
enumerable: false,
configurable: true
});
Object.defineProperty(SymbolWrapper.prototype, "public", {
get: function () {
// Symbols that are not explicitly made private are public.
return !isSymbolPrivate(this.symbol);
},
enumerable: false,
configurable: true
});
Object.defineProperty(SymbolWrapper.prototype, "callable", {
get: function () {
return typeCallable(this.tsType);
},
enumerable: false,
configurable: true
});
Object.defineProperty(SymbolWrapper.prototype, "definition", {
get: function () {
return definitionFromTsSymbol(this.symbol);
},
enumerable: false,
configurable: true
});
Object.defineProperty(SymbolWrapper.prototype, "documentation", {
get: function () {
return this.symbol.getDocumentationComment(this.context.checker);
},
enumerable: false,
configurable: true
});
SymbolWrapper.prototype.members = function () {
if (!this._members) {
if ((this.symbol.flags & (ts.SymbolFlags.Class | ts.SymbolFlags.Interface)) != 0) {
var declaredType = this.context.checker.getDeclaredTypeOfSymbol(this.symbol);
var typeWrapper = new TypeWrapper(declaredType, this.context);
this._members = typeWrapper.members();
}
else {
this._members = new SymbolTableWrapper(this.symbol.members, this.context, this.tsType);
}
}
return this._members;
};
SymbolWrapper.prototype.signatures = function () {
return signaturesOf(this.tsType, this.context);
};
SymbolWrapper.prototype.selectSignature = function (types) {
return selectSignature(this.tsType, this.context, types);
};
SymbolWrapper.prototype.indexed = function (_argument) {
return undefined;
};
SymbolWrapper.prototype.typeArguments = function () {
return this.type.typeArguments();
};
Object.defineProperty(SymbolWrapper.prototype, "tsType", {
get: function () {
var type = this._tsType;
if (!type) {
type = this._tsType =
this.context.checker.getTypeOfSymbolAtLocation(this.symbol, this.context.node);
}
return type;
},
enumerable: false,
configurable: true
});
return SymbolWrapper;
}());
var DeclaredSymbol = /** @class */ (function () {
function DeclaredSymbol(declaration) {
this.declaration = declaration;
this.language = 'ng-template';
this.nullable = false;
this.public = true;
}
Object.defineProperty(DeclaredSymbol.prototype, "name", {
get: function () {
return this.declaration.name;
},
enumerable: false,
configurable: true
});
Object.defineProperty(DeclaredSymbol.prototype, "kind", {
get: function () {
return this.declaration.kind;
},
enumerable: false,
configurable: true
});
Object.defineProperty(DeclaredSymbol.prototype, "container", {
get: function () {
return undefined;
},
enumerable: false,
configurable: true
});
Object.defineProperty(DeclaredSymbol.prototype, "type", {
get: function () {
return this.declaration.type;
},
enumerable: false,
configurable: true
});
Object.defineProperty(DeclaredSymbol.prototype, "callable", {
get: function () {
return this.type.callable;
},
enumerable: false,
configurable: true
});
Object.defineProperty(DeclaredSymbol.prototype, "definition", {
get: function () {
return this.declaration.definition;
},
enumerable: false,
configurable: true
});
Object.defineProperty(DeclaredSymbol.prototype, "documentation", {
get: function () {
return this.declaration.type.documentation;
},
enumerable: false,
configurable: true
});
DeclaredSymbol.prototype.members = function () {
return this.type.members();
};
DeclaredSymbol.prototype.signatures = function () {
return this.type.signatures();
};
DeclaredSymbol.prototype.selectSignature = function (types) {
return this.type.selectSignature(types);
};
DeclaredSymbol.prototype.typeArguments = function () {
return this.type.typeArguments();
};
DeclaredSymbol.prototype.indexed = function (_argument) {
return undefined;
};
return DeclaredSymbol;
}());
var SignatureWrapper = /** @class */ (function () {
function SignatureWrapper(signature, context) {
this.signature = signature;
this.context = context;
}
Object.defineProperty(SignatureWrapper.prototype, "arguments", {
get: function () {
return new SymbolTableWrapper(this.signature.getParameters(), this.context);
},
enumerable: false,
configurable: true
});
Object.defineProperty(SignatureWrapper.prototype, "result", {
get: function () {
return new TypeWrapper(this.signature.getReturnType(), this.context);
},
enumerable: false,
configurable: true
});
return SignatureWrapper;
}());
var SignatureResultOverride = /** @class */ (function () {
function SignatureResultOverride(signature, resultType) {
this.signature = signature;
this.resultType = resultType;
}
Object.defineProperty(SignatureResultOverride.prototype, "arguments", {
get: function () {
return this.signature.arguments;
},
enumerable: false,
configurable: true
});
Object.defineProperty(SignatureResultOverride.prototype, "result", {
get: function () {
return this.resultType;
},
enumerable: false,
configurable: true
});
return SignatureResultOverride;
}());
function toSymbolTableFactory(symbols) {
var e_3, _a;
// ∀ Typescript version >= 2.2, `SymbolTable` is implemented as an ES6 `Map`
var result = new Map();
try {
for (var symbols_2 = tslib_1.__values(symbols), symbols_2_1 = symbols_2.next(); !symbols_2_1.done; symbols_2_1 = symbols_2.next()) {
var symbol = symbols_2_1.value;
result.set(symbol.name, symbol);
}
}
catch (e_3_1) { e_3 = { error: e_3_1 }; }
finally {
try {
if (symbols_2_1 && !symbols_2_1.done && (_a = symbols_2.return)) _a.call(symbols_2);
}
finally { if (e_3) throw e_3.error; }
}
return result;
}
function toSymbols(symbolTable) {
if (!symbolTable)
return [];
var table = symbolTable;
if (typeof table.values === 'function') {
return Array.from(table.values());
}
var result = [];
var own = typeof table.hasOwnProperty === 'function' ?
function (name) { return table.hasOwnProperty(name); } :
function (name) { return !!table[name]; };
for (var name_1 in table) {
if (own(name_1)) {
result.push(table[name_1]);
}
}
return result;
}
var SymbolTableWrapper = /** @class */ (function () {
/**
* Creates a queryable table of symbols belonging to a TypeScript entity.
* @param symbols symbols to query belonging to the entity
* @param context program context
* @param type original TypeScript type of entity owning the symbols, if known
*/
function SymbolTableWrapper(symbols, context, type) {
this.context = context;
symbols = symbols || [];
if (Array.isArray(symbols)) {
this.symbols = symbols;
this.symbolTable = toSymbolTableFactory(symbols);
}
else {
this.symbols = toSymbols(symbols);
this.symbolTable = symbols;
}
if (type) {
this.stringIndexType = type.getStringIndexType();
}
}
Object.defineProperty(SymbolTableWrapper.prototype, "size", {
get: function () {
return this.symbols.length;
},
enumerable: false,
configurable: true
});
SymbolTableWrapper.prototype.get = function (key) {
var symbol = getFromSymbolTable(this.symbolTable, key);
if (symbol) {
return new SymbolWrapper(symbol, this.context);
}
if (this.stringIndexType) {
// If the key does not exist as an explicit symbol on the type, it may be accessing a string
// index signature using dot notation:
//
// const obj<T>: { [key: string]: T };
// obj.stringIndex // equivalent to obj['stringIndex'];
//
// In this case, return the type indexed by an arbitrary string key.
return new StringIndexTypeWrapper(this.stringIndexType, this.context);
}
return undefined;
};
SymbolTableWrapper.prototype.has = function (key) {
var table = this.symbolTable;
return ((typeof table.has === 'function') ? table.has(key) : table[key] != null) ||
this.stringIndexType !== undefined;
};
SymbolTableWrapper.prototype.values = function () {
var _this = this;
return this.symbols.map(function (s) { return new SymbolWrapper(s, _this.context); });
};
return SymbolTableWrapper;
}());
var MapSymbolTable = /** @class */ (function () {
function MapSymbolTable() {
this.map = new Map();
this._values = [];
}
Object.defineProperty(MapSymbolTable.prototype, "size", {
get: function () {
return this.map.size;
},
enumerable: false,
configurable: true
});
MapSymbolTable.prototype.get = function (key) {
return this.map.get(key);
};
MapSymbolTable.prototype.add = function (symbol) {
if (this.map.has(symbol.name)) {
var previous = this.map.get(symbol.name);
this._values[this._values.indexOf(previous)] = symbol;
}
this.map.set(symbol.name, symbol);
this._values.push(symbol);
};
MapSymbolTable.prototype.addAll = function (symbols) {
var e_4, _a;
try {
for (var symbols_3 = tslib_1.__values(symbols), symbols_3_1 = symbols_3.next(); !symbols_3_1.done; symbols_3_1 = symbols_3.next()) {
var symbol = symbols_3_1.value;
this.add(symbol);
}
}
catch (e_4_1) { e_4 = { error: e_4_1 }; }
finally {
try {
if (symbols_3_1 && !symbols_3_1.done && (_a = symbols_3.return)) _a.call(symbols_3);
}
finally { if (e_4) throw e_4.error; }
}
};
MapSymbolTable.prototype.has = function (key) {
return this.map.has(key);
};
MapSymbolTable.prototype.values = function () {
// Switch to this.map.values once iterables are supported by the target language.
return this._values;
};
return MapSymbolTable;
}());
var PipesTable = /** @class */ (function () {
function PipesTable(pipes, context) {
this.pipes = pipes;
this.context = context;
}
Object.defineProperty(PipesTable.prototype, "size", {
get: function () {
return this.pipes.length;
},
enumerable: false,
configurable: true
});
PipesTable.prototype.get = function (key) {
var pipe = this.pipes.find(function (pipe) { return pipe.name == key; });
if (pipe) {
return new PipeSymbol(pipe, this.context);
}
};
PipesTable.prototype.has = function (key) {
return this.pipes.find(function (pipe) { return pipe.name == key; }) != null;
};
PipesTable.prototype.values = function () {
var _this = this;
return this.pipes.map(function (pipe) { return new PipeSymbol(pipe, _this.context); });
};
return PipesTable;
}());
// This matches .d.ts files that look like ".../<package-name>/<package-name>.d.ts",
var INDEX_PATTERN = /[\\/]([^\\/]+)[\\/]\1\.d\.ts$/;
var PipeSymbol = /** @class */ (function () {
function PipeSymbol(pipe, context) {
this.pipe = pipe;
this.context = context;
this.kind = 'pipe';
this.language = 'typescript';
this.container = undefined;
this.callable = true;
this.nullable = false;
this.public = true;
}
Object.defineProperty(PipeSymbol.prototype, "name", {
get: function () {
return this.pipe.name;
},
enumerable: false,
configurable: true
});
Object.defineProperty(PipeSymbol.prototype, "type", {
get: function () {
return new TypeWrapper(this.tsType, this.context);
},
enumerable: false,
configurable: true
});
Object.defineProperty(PipeSymbol.prototype, "definition", {
get: function () {
var symbol = this.tsType.getSymbol();
return symbol ? definitionFromTsSymbol(symbol) : undefined;
},
enumerable: false,
configurable: true
});
Object.defineProperty(PipeSymbol.prototype, "documentation", {
get: function () {
var symbol = this.tsType.getSymbol();
if (!symbol) {
return [];
}
return symbol.getDocumentationComment(this.context.checker);
},
enumerable: false,
configurable: true
});
PipeSymbol.prototype.members = function () {
return EmptyTable.instance;
};
PipeSymbol.prototype.signatures = function () {
return signaturesOf(this.tsType, this.context);
};
PipeSymbol.prototype.selectSignature = function (types) {
var signature = selectSignature(this.tsType, this.context, types);
if (types.length > 0) {
var parameterType = types[0];
var resultType = undefined;
switch (this.name) {
case 'async':
// Get type argument of 'Observable', 'Promise', or 'EventEmitter'.
var tArgs = parameterType.typeArguments();
if (tArgs && tArgs.length === 1) {
resultType = tArgs[0];
}
break;
case 'slice':
resultType = parameterType;
break;
}
if (resultType) {
signature = new SignatureResultOverride(signature, resultType);
}
}
return signature;
};
PipeSymbol.prototype.indexed = function (_argument) {
return undefined;
};
PipeSymbol.prototype.typeArguments = function () {
return this.type.typeArguments();
};
Object.defineProperty(PipeSymbol.prototype, "tsType", {
get: function () {
var type = this._tsType;
if (!type) {
var classSymbol = this.findClassSymbol(this.pipe.type.reference);
if (classSymbol) {
type = this._tsType = this.findTransformMethodType(classSymbol);
}
if (!type) {
type = this._tsType = getTsTypeFromBuiltinType(symbols_1.BuiltinType.Any, this.context);
}
}
return type;
},
enumerable: false,
configurable: true
});
PipeSymbol.prototype.findClassSymbol = function (type) {
return findClassSymbolInContext(type, this.context);
};
PipeSymbol.prototype.findTransformMethodType = function (classSymbol) {
var classType = this.context.checker.getDeclaredTypeOfSymbol(classSymbol);
if (classType) {
var transform = classType.getProperty('transform');
if (transform) {
return this.context.checker.getTypeOfSymbolAtLocation(transform, this.context.node);
}
}
};
return PipeSymbol;
}());
function findClassSymbolInContext(type, context) {
var sourceFile = context.program.getSourceFile(type.filePath);
if (!sourceFile) {
// This handles a case where an <packageName>/index.d.ts and a <packageName>/<packageName>.d.ts
// are in the same directory. If we are looking for <packageName>/<packageName> and didn't
// find it, look for <packageName>/index.d.ts as the program might have found that instead.
var p = type.filePath;
var m = p.match(INDEX_PATTERN);
if (m) {
var indexVersion = path.join(path.dirname(p), 'index.d.ts');
sourceFile = context.program.getSourceFile(indexVersion);
}
}
if (sourceFile) {
var moduleSymbol = sourceFile.module || sourceFile.symbol;
var exports_1 = context.checker.getExportsOfModule(moduleSymbol);
return (exports_1 || []).find(function (symbol) { return symbol.name == type.name; });
}
}
var EmptyTable = /** @class */ (function () {
function EmptyTable() {
this.size = 0;
}
EmptyTable.prototype.get = function (_key) {
return undefined;
};
EmptyTable.prototype.has = function (_key) {
return false;
};
EmptyTable.prototype.values = function () {
return [];
};
EmptyTable.instance = new EmptyTable();
return EmptyTable;
}());
function isSymbolPrivate(s) {
return !!s.valueDeclaration && isPrivate(s.valueDeclaration);
}
function getTsTypeFromBuiltinType(builtinType, ctx) {
var syntaxKind;
switch (builtinType) {
case symbols_1.BuiltinType.Any:
syntaxKind = ts.SyntaxKind.AnyKeyword;
break;
case symbols_1.BuiltinType.Boolean:
syntaxKind = ts.SyntaxKind.BooleanKeyword;
break;
case symbols_1.BuiltinType.Null:
syntaxKind = ts.SyntaxKind.NullKeyword;
break;
case symbols_1.BuiltinType.Number:
syntaxKind = ts.SyntaxKind.NumberKeyword;
break;
case symbols_1.BuiltinType.String:
syntaxKind = ts.SyntaxKind.StringKeyword;
break;
case symbols_1.BuiltinType.Undefined:
syntaxKind = ts.SyntaxKind.UndefinedKeyword;
break;
default:
throw new Error("Internal error, unhandled literal kind " + builtinType + ":" + symbols_1.BuiltinType[builtinType]);
}
var node = ts.createNode(syntaxKind);
node.parent = ts.createEmptyStatement();
return ctx.checker.getTypeAtLocation(node);
}
function spanAt(sourceFile, line, column) {
if (line != null && column != null) {
var position_1 = ts.getPositionOfLineAndCharacter(sourceFile, line, column);
var findChild = function findChild(node) {
if (node.kind > ts.SyntaxKind.LastToken && node.pos <= position_1 && node.end > position_1) {
var betterNode = ts.forEachChild(node, findChild);
return betterNode || node;
}
};
var node = ts.forEachChild(sourceFile, findChild);
if (node) {
return { start: node.getStart(), end: node.getEnd() };
}
}
}
function definitionFromTsSymbol(symbol) {
var declarations = symbol.declarations;
if (declarations) {
return declarations.map(function (declaration) {
var sourceFile = declaration.getSourceFile();
return {
fileName: sourceFile.fileName,
span: { start: declaration.getStart(), end: declaration.getEnd() }
};
});
}
}
function parentDeclarationOf(node) {
while (node) {
switch (node.kind) {
case ts.SyntaxKind.ClassDeclaration:
case ts.SyntaxKind.InterfaceDeclaration:
return node;
case ts.SyntaxKind.SourceFile:
return undefined;
}
node = node.parent;
}
}
function getContainerOf(symbol, context) {
var e_5, _a;
if (symbol.getFlags() & ts.SymbolFlags.ClassMember && symbol.declarations) {
try {
for (var _b = tslib_1.__values(symbol.declarations), _c = _b.next(); !_c.done; _c = _b.next()) {
var declaration = _c.value;
var parent_1 = parentDeclarationOf(declaration);
if (parent_1) {
var type = context.checker.getTypeAtLocation(parent_1);
if (type) {
return new TypeWrapper(type, context);
}
}
}
}
catch (e_5_1) { e_5 = { error: e_5_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_5) throw e_5.error; }
}
}
}
function typeKindOf(type) {
var e_6, _a;
if (type) {
if (type.flags & ts.TypeFlags.Any) {
return symbols_1.BuiltinType.Any;
}
else if (type.flags & (ts.TypeFlags.String | ts.TypeFlags.StringLike | ts.TypeFlags.StringLiteral)) {
return symbols_1.BuiltinType.String;
}
else if (type.flags & (ts.TypeFlags.Number | ts.TypeFlags.NumberLike)) {
return symbols_1.BuiltinType.Number;
}
else if (type.flags & ts.TypeFlags.Object) {
return symbols_1.BuiltinType.Object;
}
else if (type.flags & (ts.TypeFlags.Undefined)) {
return symbols_1.BuiltinType.Undefined;
}
else if (type.flags & (ts.TypeFlags.Null)) {
return symbols_1.BuiltinType.Null;
}
else if (type.flags & ts.TypeFlags.Union) {
var unionType = type;
if (unionType.types.length === 0)
return symbols_1.BuiltinType.Other;
var ty = 0;
try {
for (var _b = tslib_1.__values(unionType.types), _c = _b.next(); !_c.done; _c = _b.next()) {
var subType = _c.value;
ty |= typeKindOf(subType);
}
}
catch (e_6_1) { e_6 = { error: e_6_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_6) throw e_6.error; }
}
return ty;
}
else if (type.flags & ts.TypeFlags.TypeParameter) {
return symbols_1.BuiltinType.Unbound;
}
}
return symbols_1.BuiltinType.Other;
}
function getFromSymbolTable(symbolTable, key) {
var table = symbolTable;
var symbol;
if (typeof table.get === 'function') {
// TS 2.2 uses a Map
symbol = table.get(key);
}
else {
// TS pre-2.2 uses an object
symbol = table[key];
}
return symbol;
}
});
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidHlwZXNjcmlwdF9zeW1ib2xzLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vLi4vLi4vLi4vLi4vcGFja2FnZXMvbGFuZ3VhZ2Utc2VydmljZS9zcmMvdHlwZXNjcmlwdF9zeW1ib2xzLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBOzs7Ozs7R0FNRzs7Ozs7Ozs7Ozs7Ozs7SUFHSCwyQkFBNkI7SUFDN0IsK0JBQWlDO0lBRWpDLGlFQUF5STtJQUV6SSxzQ0FBc0M7SUFDdEMsM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