@traversets/code-extractor
Version:
The TypeScript Code Extractor and Analyzer can be handy for RAG (Retrieval-Augmented Generation) systems for codebases. It provides a detailed and structured representation of the codebase that can be converted into embeddings, enabling more effective adv
180 lines (179 loc) • 9.06 kB
TypeScript
import * as ts from "typescript";
import { DeclarationOrFunctionNode, IClassInfo, ICodebaseMap, IEnumInfo, IFunctionInfo, IInterfaceInfo, IProperty, TNode } from "../interfaces";
import { ITypeScriptCodeMapper } from "../interfaces/ts.code.mapper.interface";
import { Result } from "../result";
export declare class TypeScriptCodeMapper implements ITypeScriptCodeMapper {
private program;
private typeChecker;
constructor();
/**
* Initializes a TypeScript program by reading the TS configuration file and creating a new program instance.
* This method sets up the program and type checker for further compilation and analysis.
*/
private initializeTypescriptProgram;
/**
* Extracts information about a TypeScript class declaration.
* This function iterates over the members of the class, identifying methods,
* properties, interfaces, and enums, and compiles this information into an IClassInfo object.
*
* @param node The TypeScript class declaration to extract information from.
* @param sourceFile The source file containing the class declaration.
* @returns An IClassInfo object containing the name, methods, properties, interfaces, and enums of the class.
*/
extractClassMetaData(node: ts.ClassDeclaration, sourceFile: ts.SourceFile): Result<IClassInfo>;
private aggregateFunctions;
/**
* Extracts property information from a TypeScript property declaration and adds it
* to the class or module information object if valid. This aggregation helps build
* a complete representation of the class/module structure.
*/
private aggergateProperties;
/**
* Processes interface declarations and aggregates them into the parent class or module
* information object. Essential for maintaining the hierarchical structure of interfaces
* within their containing scope.
*/
private aggregateInterfaces;
/**
* Extracts and aggregates enum information from enum declarations into the parent
* class or module information object. Helps maintain a complete type system
* representation within the code structure.
*/
private aggregateEnums;
/**
* Retrieves and processes child elements of a class declaration, extracting
* relevant information about methods, properties, interfaces, and enums.
*
* @param node The class declaration node to process.
* @param member The current class element being processed.
* @param sourceFile The source file containing the class declaration.
* @param index The current index within the class declaration.
* @param classInfo The object to store extracted class information.
*/
private processClassMembers;
/**
* Extracts property information from a TypeScript property declaration.
*
* This function takes a node representing a property declaration and its source file,
* and returns an object containing the property's name and type. If the type is not
* explicitly specified, it is inferred from the property declaration.
*
* @param node
* @param sourceFile
* @returns An object with 'name' and 'type' properties.
*/
extractPropertyParameters(node: ts.PropertyDeclaration, sourceFile: ts.SourceFile): Result<IProperty>;
/**
* Extracts the parameters of a function from a given node.
*
* @param node The node containing the function parameters.
* @param sourceFile The source file containing the node.
* @returns An array of function parameter objects.
*/
extractFunctionParameters(node: ts.FunctionDeclaration | ts.MethodDeclaration | ts.ArrowFunction, sourceFile: ts.SourceFile): Result<IProperty[]>;
extractArrowFunctionParameters(node: ts.ArrowFunction, sourceFile: ts.SourceFile): Result<IProperty[]>;
/**
* Extracts and returns function details from a given function declaration or method declaration node.
*
* @param node The function declaration or method declaration node to extract details from.
* @param sourceFile The source file containing the node.
* @returns An object containing function details, or null if the node has no name.
*/
getFunctionDetails(node: ts.FunctionDeclaration | ts.MethodDeclaration | ts.ArrowFunction, sourceFile: ts.SourceFile): Result<IFunctionInfo> | null;
/**
* Maps a function declaration or method declaration to a details object,
* extracting relevant information such as name, content, parameters, return type, and comments.
*
* @param name The name of the function.
* @param content The content of the function.
* @param parameters An array of property definitions for the function parameters.
* @param node The TypeScript function or method declaration node.
* @returns An object containing the function details.
*/
private functionDetailsMapper;
/**
* Retrieves the type of a given function or method declaration.
*
* @param node A function or method declaration node.
* @returns A string representation of the function or method type, or undefined if type checking is unavailable.
*/
getTypeAtLocation(node: DeclarationOrFunctionNode): Result<string | undefined>;
/**
* Retrieves and concatenates JSDoc comments associated with a given TypeScript node.
*
* @param {TNode} node - The TypeScript node to extract comments from.
* @returns {string} Concatenated JSDoc comments.
*/
getComment(node: TNode): string;
/**
* Generates a string representation of a given function or method declaration node.
* This method leverages the TypeScript printer to produce a source code string,
* removing any comments and using line feed as the new line character.
*
* @param node The function or method declaration node to be printed.
* @param sourceFile The source file that contains the node to be printed.
* @returns A string representation of the given node.
*/
getFunctionNodeText(node: ts.FunctionDeclaration | ts.MethodDeclaration | ts.ArrowFunction, sourceFile: ts.SourceFile): string;
/**
* Finds the root directory of a project by searching for a 'package.json' file
* starting from the given current directory and traversing up the directory tree.
*
* @param {string} [currentDir=process.cwd()] - The directory to start searching from.
* @returns {string} The root directory of the project, or the current working directory if no 'package.json' file is found.
*/
findProjectRoot(currentDir?: string): string;
/**
* Retrieves a list of TypeScript files, excluding test and mock files.
* @returns A promise that resolves with a list of TypeScript files.
*/
getTsFiles(): Promise<string[]>;
/**
* Extracts module information from a TypeScript source file.
*
* @param sourceFile The TypeScript source file.
* @param relativePath The relative path of the module.
* @returns The module information.
*/
private extractModuleInfo;
/**
* Retrieves a source file from the TypeScript program by its filename.
*
* @param fileName - The path to the source file to retrieve
* @returns The SourceFile object if found, undefined otherwise
*/
getSourceFile(fileName: string): ts.SourceFile | undefined;
/**
* Gets an array of all root file names in the TypeScript program.
* Root files are the entry points specified in the tsconfig.json or passed to the compiler.
*
* @returns A readonly array of file paths, or undefined if the program is not initialized
*/
getRootFileNames(): readonly string[] | undefined;
/**
* Returns the current TypeScript program instance.
* The program object represents the entire TypeScript project and provides
* access to the compiler's internal state.
*
* @returns The TypeScript Program object, or undefined if not initialized
*/
getProgram(): ts.Program | undefined;
/**
* Retrieves the TypeChecker instance from the current program.
* The TypeChecker is responsible for type analysis and provides
* APIs for querying type information.
*
* @returns The TypeScript TypeChecker object, or undefined if the program is not initialized
* @remarks This method creates a new type checker instance each time it's called,
* consider caching the result if multiple calls are needed
*/
getTypeChecker(): ts.TypeChecker | undefined;
/**
* Builds a hierarchical map of the codebase by traversing TypeScript files
* and extracting module and class information.
*/
buildCodebaseMap(): Promise<Result<ICodebaseMap>>;
extractInterfaceInfo(node: ts.InterfaceDeclaration, sourceFile: ts.SourceFile): Result<IInterfaceInfo>;
extractEnumInfo(node: ts.EnumDeclaration, sourceFile: ts.SourceFile): Result<IEnumInfo>;
buildDependencyGraph(sourceFile: ts.SourceFile): string[];
}