UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

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