ts-simple-ast
Version:
TypeScript compiler wrapper for AST navigation and code generation.
139 lines (137 loc) • 5.28 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const ts = require("typescript");
/**
* Wrapper around the TypeChecker.
*/
class TypeChecker {
/** @internal */
constructor(global) {
this.global = global;
}
/**
* Gets the compiler's TypeChecker.
*/
get compilerObject() {
return this._compilerObject;
}
/**
* Resets the type checker.
* @internal
*/
reset(typeChecker) {
this._compilerObject = typeChecker;
}
/**
* Gets the apparent type of a type.
* @param type - Type to get the apparent type of.
*/
getApparentType(type) {
return this.global.compilerFactory.getType(this.compilerObject.getApparentType(type.compilerType));
}
/**
* Gets the constant value of a declaration.
* @param node - Node to get the constant value from.
*/
getConstantValue(node) {
return this.compilerObject.getConstantValue(node.compilerNode);
}
/**
* Gets the fully qualified name of a symbol.
* @param symbol - Symbol to get the fully qualified name of.
*/
getFullyQualifiedName(symbol) {
return this.compilerObject.getFullyQualifiedName(symbol.compilerSymbol);
}
/**
* Gets the type at the specified location.
* @param node - Node to get the type for.
*/
getTypeAtLocation(node) {
return this.global.compilerFactory.getType(this.compilerObject.getTypeAtLocation(node.compilerNode));
}
/**
* Gets the contextual type of an expression.
* @param expression - Expression.
*/
getContextualType(expression) {
const contextualType = this.compilerObject.getContextualType(expression.compilerNode);
return contextualType == null ? undefined : this.global.compilerFactory.getType(contextualType);
}
/**
* Gets the type of a symbol at the specified location.
* @param symbol - Symbol to get the type for.
* @param node - Location to get the type for.
*/
getTypeOfSymbolAtLocation(symbol, node) {
return this.global.compilerFactory.getType(this.compilerObject.getTypeOfSymbolAtLocation(symbol.compilerSymbol, node.compilerNode));
}
/**
* Gets the declared type of a symbol.
* @param symbol - Symbol to get the type for.
*/
getDeclaredTypeOfSymbol(symbol) {
return this.global.compilerFactory.getType(this.compilerObject.getDeclaredTypeOfSymbol(symbol.compilerSymbol));
}
/**
* Gets the symbol at the specified location or undefined if none exists.
* @param node - Node to get the symbol for.
*/
getSymbolAtLocation(node) {
const compilerSymbol = this.compilerObject.getSymbolAtLocation(node.compilerNode);
return compilerSymbol == null ? undefined : this.global.compilerFactory.getSymbol(compilerSymbol);
}
/**
* Gets the aliased symbol of a symbol.
* @param symbol - Symbol to get the alias symbol of.
*/
getAliasedSymbol(symbol) {
if (!symbol.hasFlags(ts.SymbolFlags.Alias))
return undefined;
const tsAliasSymbol = this.compilerObject.getAliasedSymbol(symbol.compilerSymbol);
return tsAliasSymbol == null ? undefined : this.global.compilerFactory.getSymbol(tsAliasSymbol);
}
/**
* Gets the properties of a type.
* @param type - Type.
*/
getPropertiesOfType(type) {
return this.compilerObject.getPropertiesOfType(type.compilerType).map(p => this.global.compilerFactory.getSymbol(p));
}
/**
* Gets the type text
* @param type - Type to get the text of.
* @param enclosingNode - Enclosing node.
* @param typeFormatFlags - Type format flags.
*/
getTypeText(type, enclosingNode, typeFormatFlags) {
if (typeFormatFlags == null)
typeFormatFlags = this.getDefaultTypeFormatFlags(enclosingNode);
const compilerNode = enclosingNode == null ? undefined : enclosingNode.compilerNode;
return this.compilerObject.typeToString(type.compilerType, compilerNode, typeFormatFlags);
}
/**
* Gets the return type of a signature.
* @param signature - Signature to get the return type of.
*/
getReturnTypeOfSignature(signature) {
return this.global.compilerFactory.getType(this.compilerObject.getReturnTypeOfSignature(signature.compilerSignature));
}
/**
* Gets a signature from a node.
* @param node - Node to get the signature from.
*/
getSignatureFromNode(node) {
const signature = this.compilerObject.getSignatureFromDeclaration(node.compilerNode);
return signature == null ? undefined : this.global.compilerFactory.getSignature(signature);
}
getDefaultTypeFormatFlags(enclosingNode) {
let formatFlags = (ts.TypeFormatFlags.UseTypeOfFunction | ts.TypeFormatFlags.NoTruncation | ts.TypeFormatFlags.UseFullyQualifiedType |
ts.TypeFormatFlags.WriteTypeArgumentsOfSignature);
if (enclosingNode != null && enclosingNode.getKind() === ts.SyntaxKind.TypeAliasDeclaration)
formatFlags |= ts.TypeFormatFlags.InTypeAlias;
return formatFlags;
}
}
exports.TypeChecker = TypeChecker;
//# sourceMappingURL=TypeChecker.js.map