UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

263 lines (262 loc) 9.15 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var typescript_1 = require("./../../typescript"); var errors = require("./../../errors"); var utils_1 = require("./../../utils"); var Type = /** @class */ (function () { /** * Initializes a new instance of Type. * @internal * @param global - Global container. * @param type - Compiler type. */ function Type(global, type) { this.global = global; this._compilerType = type; } Object.defineProperty(Type.prototype, "compilerType", { /** * Gets the underlying compiler type. */ get: function () { return this._compilerType; }, enumerable: true, configurable: true }); /** * Gets the type text. * @param enclosingNode - The enclosing node. * @param typeFormatFlags - Format flags for the type text. */ Type.prototype.getText = function (enclosingNode, typeFormatFlags) { return this.global.typeChecker.getTypeText(this, enclosingNode, typeFormatFlags); }; /** * Gets the alias symbol if it exists. */ Type.prototype.getAliasSymbol = function () { return this.compilerType.aliasSymbol == null ? undefined : this.global.compilerFactory.getSymbol(this.compilerType.aliasSymbol); }; /** * Gets the alias symbol if it exists, or throws. */ Type.prototype.getAliasSymbolOrThrow = function () { return errors.throwIfNullOrUndefined(this.getAliasSymbol(), "Expected to find an alias symbol."); }; /** * Gets the alias type arguments. */ Type.prototype.getAliasTypeArguments = function () { var _this = this; var aliasTypeArgs = this.compilerType.aliasTypeArguments || []; return aliasTypeArgs.map(function (t) { return _this.global.compilerFactory.getType(t); }); }; /** * Gets the apparent type. */ Type.prototype.getApparentType = function () { return this.global.typeChecker.getApparentType(this); }; /** * Gets the array type */ Type.prototype.getArrayType = function () { if (!this.isArrayType()) return undefined; return this.getTypeArguments()[0]; }; /** * Gets the base types. */ Type.prototype.getBaseTypes = function () { var _this = this; var baseTypes = this.compilerType.getBaseTypes() || []; return baseTypes.map(function (t) { return _this.global.compilerFactory.getType(t); }); }; /** * Gets the call signatures. */ Type.prototype.getCallSignatures = function () { var _this = this; return this.compilerType.getCallSignatures().map(function (s) { return _this.global.compilerFactory.getSignature(s); }); }; /** * Gets the construct signatures. */ Type.prototype.getConstructSignatures = function () { var _this = this; return this.compilerType.getConstructSignatures().map(function (s) { return _this.global.compilerFactory.getSignature(s); }); }; /** * Gets the properties of the type. */ Type.prototype.getProperties = function () { var _this = this; return this.compilerType.getProperties().map(function (s) { return _this.global.compilerFactory.getSymbol(s); }); }; Type.prototype.getProperty = function (nameOrFindFunction) { return utils_1.getSymbolByNameOrFindFunction(this.getProperties(), nameOrFindFunction); }; /** * Gets the apparent properties of the type. */ Type.prototype.getApparentProperties = function () { var _this = this; return this.compilerType.getApparentProperties().map(function (s) { return _this.global.compilerFactory.getSymbol(s); }); }; Type.prototype.getApparentProperty = function (nameOrFindFunction) { return utils_1.getSymbolByNameOrFindFunction(this.getApparentProperties(), nameOrFindFunction); }; /** * Gets the non-nullable type. */ Type.prototype.getNonNullableType = function () { return this.global.compilerFactory.getType(this.compilerType.getNonNullableType()); }; /** * Gets the number index type. */ Type.prototype.getNumberIndexType = function () { var numberIndexType = this.compilerType.getNumberIndexType(); return numberIndexType == null ? undefined : this.global.compilerFactory.getType(numberIndexType); }; /** * Gets the string index type. */ Type.prototype.getStringIndexType = function () { var stringIndexType = this.compilerType.getStringIndexType(); return stringIndexType == null ? undefined : this.global.compilerFactory.getType(stringIndexType); }; /** * Gets the target type of a type reference if it exists. */ Type.prototype.getTargetType = function () { var targetType = this.compilerType.target || undefined; return targetType == null ? undefined : this.global.compilerFactory.getType(targetType); }; /** * Gets the target type of a type reference or throws if it doesn't exist. */ Type.prototype.getTargetTypeOrThrow = function () { return errors.throwIfNullOrUndefined(this.getTargetType(), "Expected to find the target type."); }; /** * Gets type arguments. */ Type.prototype.getTypeArguments = function () { var _this = this; var typeArguments = this.compilerType.typeArguments || []; return typeArguments.map(function (t) { return _this.global.compilerFactory.getType(t); }); }; /** * Gets the union types. */ Type.prototype.getUnionTypes = function () { var _this = this; if (!this.isUnionType()) return []; return this.compilerType.types.map(function (t) { return _this.global.compilerFactory.getType(t); }); }; /** * Gets the intersection types. */ Type.prototype.getIntersectionTypes = function () { var _this = this; if (!this.isIntersectionType()) return []; return this.compilerType.types.map(function (t) { return _this.global.compilerFactory.getType(t); }); }; /** * Gets the symbol of the type. */ Type.prototype.getSymbol = function () { var tsSymbol = this.compilerType.getSymbol(); return tsSymbol == null ? undefined : this.global.compilerFactory.getSymbol(tsSymbol); }; /** * Gets the symbol of the type or throws. */ Type.prototype.getSymbolOrThrow = function () { return errors.throwIfNullOrUndefined(this.getSymbol(), "Expected to find a symbol."); }; /** * Gets if this is an anonymous type. */ Type.prototype.isAnonymousType = function () { return (this.getObjectFlags() & typescript_1.ObjectFlags.Anonymous) !== 0; }; /** * Gets if this is an array type. */ Type.prototype.isArrayType = function () { var symbol = this.getSymbol(); if (symbol == null) return false; return symbol.getName() === "Array" && this.getTypeArguments().length === 1; }; /** * Gets if this is a boolean type. */ Type.prototype.isBooleanType = function () { return (this.compilerType.flags & typescript_1.TypeFlags.Boolean) !== 0; }; /** * Gets if this is an enum type. */ Type.prototype.isEnumType = function () { return (this.compilerType.flags & typescript_1.TypeFlags.Enum) !== 0; }; /** * Gets if this is an interface type. */ Type.prototype.isInterfaceType = function () { return (this.getObjectFlags() & typescript_1.ObjectFlags.Interface) !== 0; }; /** * Gets if this is an intersection type. */ Type.prototype.isIntersectionType = function () { return (this.compilerType.flags & typescript_1.TypeFlags.Intersection) !== 0; }; /** * Gets if this is an object type. */ Type.prototype.isObjectType = function () { return (this.compilerType.flags & typescript_1.TypeFlags.Object) !== 0; }; /** * Gets if this is a type parameter. */ Type.prototype.isTypeParameter = function () { return (this.compilerType.flags & typescript_1.TypeFlags.TypeParameter) !== 0; }; /** * Gets if this is a union type. */ Type.prototype.isUnionType = function () { return (this.compilerType.flags & typescript_1.TypeFlags.Union) !== 0; }; /** * Gets if this is the undefined type. */ Type.prototype.isUndefinedType = function () { return (this.compilerType.flags & typescript_1.TypeFlags.Undefined) !== 0; }; /** * Gets the type flags. */ Type.prototype.getFlags = function () { return this.compilerType.flags; }; /** * Gets the object flags. */ Type.prototype.getObjectFlags = function () { if (!this.isObjectType()) return 0; return this.compilerType.objectFlags || 0; }; return Type; }()); exports.Type = Type;