sicua
Version:
A tool for analyzing project structure and dependencies
144 lines (143 loc) • 4.74 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TypeEvaluator = void 0;
const typescript_1 = __importDefault(require("typescript"));
const nodeTypeGuards_1 = require("../ast/nodeTypeGuards");
class TypeEvaluator {
constructor(typeChecker) {
this.typeChecker = typeChecker;
}
/**
* Gets the type string for a node
*/
evaluateType(node) {
const type = this.typeChecker.getTypeAtLocation(node);
return this.typeChecker.typeToString(type);
}
/**
* Extracts type properties from an interface or type
*/
extractTypeProperties(type) {
const properties = {};
if (typescript_1.default.isTypeLiteralNode(type)) {
type.members.forEach((member) => {
if (typescript_1.default.isPropertySignature(member) && member.type) {
const propName = member.name.getText();
properties[propName] = this.getTypeString(member.type);
}
});
}
return properties;
}
/**
* Evaluates union type components
*/
evaluateUnionType(type) {
return type.types.map((t) => this.getTypeString(t));
}
/**
* Gets return type of a function-like declaration
*/
getFunctionReturnType(node) {
if (node.type) {
return this.getTypeString(node.type);
}
const signature = this.typeChecker.getSignatureFromDeclaration(node);
if (signature) {
const returnType = this.typeChecker.getReturnTypeOfSignature(signature);
return this.typeChecker.typeToString(returnType);
}
return undefined;
}
/**
* Gets parameter types of a function-like declaration
*/
getFunctionParameterTypes(node) {
const paramTypes = {};
node.parameters.forEach((param) => {
if (typescript_1.default.isIdentifier(param.name) && param.type) {
paramTypes[param.name.text] = this.getTypeString(param.type);
}
});
return paramTypes;
}
/**
* Gets the base type(s) of a class/interface
*/
getBaseTypes(node) {
const baseTypes = [];
if (node.heritageClauses) {
node.heritageClauses.forEach((clause) => {
clause.types.forEach((type) => {
const baseType = this.typeChecker.getTypeAtLocation(type.expression);
baseTypes.push(this.typeChecker.typeToString(baseType));
});
});
}
return baseTypes;
}
/**
* Checks if a type is assignable to another type
*/
isTypeAssignableTo(source, target) {
const sourceType = this.typeChecker.getTypeAtLocation(source);
const targetType = this.typeChecker.getTypeAtLocation(target);
return this.typeChecker.isTypeAssignableTo(sourceType, targetType);
}
/**
* Gets resolved type arguments for a generic type
*/
getTypeArguments(node) {
if (!node.typeArguments)
return [];
return node.typeArguments.map((arg) => this.getTypeString(arg));
}
/**
* Helper method to convert TypeNode to string
*/
getTypeString(type) {
if (nodeTypeGuards_1.NodeTypeGuards.isTypeReference(type)) {
return type.typeName.getText();
}
else if (typescript_1.default.isLiteralTypeNode(type)) {
return type.literal.getText();
}
else if (typescript_1.default.isUnionTypeNode(type)) {
return type.types.map((t) => this.getTypeString(t)).join(" | ");
}
else if (typescript_1.default.isArrayTypeNode(type)) {
return `${this.getTypeString(type.elementType)}[]`;
}
else {
return type.getText();
}
}
/**
* Gets inferred type of a node
*/
getInferredType(node) {
const type = this.typeChecker.getTypeAtLocation(node);
return this.typeChecker.typeToString(type);
}
/**
* Checks if a type is a Promise type
*/
isPromiseType(node) {
const type = this.typeChecker.getTypeAtLocation(node);
const symbol = type.getSymbol();
return symbol?.getName() === "Promise";
}
/**
* Gets the declared type of a variable/parameter
*/
getDeclaredType(declaration) {
if (declaration.type) {
return this.getTypeString(declaration.type);
}
return undefined;
}
}
exports.TypeEvaluator = TypeEvaluator;