|
1 | 1 | # TypeScript Code Extractor and Analyzer |
2 | 2 |
|
3 | | -This project provides a robust toolkit for extracting, analyzing, and mapping TypeScript code structures. |
| 3 | +This project provides an advanced toolkit for parsing TypeScript code using the TypeScript Abstract Syntax Tree (AST) to extract, analyze, and map code structures. |
4 | 4 |
|
5 | | -The TypeScript Code Extractor and Analyzer is a comprehensive solution designed to parse and analyze TypeScript codebases. It offers a set of tools to extract detailed information about classes, interfaces, functions, and other TypeScript constructs, enabling developers to gain deep insights into their codebase structure and dependencies. |
| 5 | +TypeScript Code Extractor and Analyzer is a robust system that utilizes a TypeScript parser to navigate through the codebase's AST, extracting structured metadata about various components such as modules, classes, functions, interfaces, properties, and enums. |
6 | 6 |
|
7 | | -Key features include: |
| 7 | +Key features: |
8 | 8 |
|
9 | | -- Extraction of class metadata, including methods, properties, interfaces, and enums |
10 | | -- Function and method analysis, including parameter extraction and return type inference |
11 | | -- Interface and enum information extraction |
12 | | -- Dependency graph generation for TypeScript files |
| 9 | +- AST-based Class Metadata Extraction: Utilizes TypeScript's AST to gather comprehensive metadata on class methods, properties, interfaces, and enums. |
| 10 | +- Function and Method Signature Analysis: Parses function signatures from the AST for details on parameters, return types, and inferred type information. |
| 11 | +- Interface and Enum Parsing: Extracts information from AST nodes representing interfaces and enums in TypeScript. |
| 12 | +- Dependency Graph Construction: Builds a graph of file dependencies by analyzing import declarations within the AST. |
| 13 | + |
| 14 | +### Installation |
| 15 | +To integrate this tool into your project, install it via npm: |
| 16 | +``` |
| 17 | +npm i @traversets/code-extractor |
| 18 | +``` |
13 | 19 |
|
14 | 20 | ### Code Analysis |
15 | 21 |
|
16 | | -To analyze a TypeScript codebase: |
| 22 | +Below is an example of how to use the AST parser for code analysis: |
17 | 23 |
|
18 | 24 | ```typescript |
19 | | -import { TypeScriptCodeMapper } from "./src/services/typescript-code-mapper.service"; |
20 | 25 |
|
21 | | -const codeMapper = new TypeScriptCodeMapper(); |
| 26 | +const codeMapper: TypeScriptCodeMapper = new TypeScriptCodeMapper(); |
22 | 27 |
|
23 | | -// Build a codebase map |
24 | | -const codebaseMap = await codeMapper.buildCodebaseMap(); |
| 28 | +// Get Root files |
| 29 | +const rootFiles: readonly string[] = codeMapper.getRootFileNames(); |
25 | 30 |
|
26 | | -// Extract class metadata |
27 | | -const classInfo = codeMapper.extractClassMetaData(classNode, sourceFile); |
| 31 | +// Convert a rootFile into a sourceFile |
| 32 | +const sourceFile: ts.SourceFile = codeMapper.getSourceFile(rootFiles[5]); |
28 | 33 |
|
29 | 34 | // Build a dependency graph |
30 | | -const dependencies = codeMapper.buildDependencyGraph(sourceFile); |
| 35 | +const getSourceFileDepencies: string[] = codeMapper.buildDependencyGraph(sourceFile); |
| 36 | + |
| 37 | +// Build a codebase map |
| 38 | +const codebaseMap = await codeMapper.buildCodebaseMap().getValue(); |
31 | 39 | ``` |
| 40 | + |
| 41 | +### Sample Response Structure |
| 42 | +The resulting JSON structure reflects the TypeScript AST's hierarchical representation: |
| 43 | +``` |
| 44 | +{ |
| 45 | + "MyProject": { |
| 46 | + "modules": { |
| 47 | + "src/utils/logger.ts": { |
| 48 | + "classes": [ |
| 49 | + { |
| 50 | + "name": "Logger", |
| 51 | + "functions": [ |
| 52 | + { |
| 53 | + "name": "log", |
| 54 | + "parameters": [{ "name": "message", "type": "string" }], |
| 55 | + "returnType": "void", |
| 56 | + "content": "", |
| 57 | + "comment": "Logs application Error" |
| 58 | + } |
| 59 | + ], |
| 60 | + "properties": [ |
| 61 | + { "name": "logLevel", "type": "LogLevel" } |
| 62 | + ] |
| 63 | + } |
| 64 | + ], |
| 65 | + "functions": [], |
| 66 | + "interfaces": [], |
| 67 | + "enums": [], |
| 68 | + "dependencies": ["import { LogLevel } from './types';"] |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | +
|
| 74 | +``` |
| 75 | + |
| 76 | +### Usage for Agentic RAG Systems |
| 77 | +This tool enhances Retrieval-Augmented Generation (RAG) systems by: |
| 78 | + |
| 79 | +- Parsing the TypeScript AST into embeddings for semantic code search and similarity matching |
| 80 | +- Leveraging AST metadata for advanced code analysis, query resolution, or to aid in code generation, thereby improving the understanding and manipulation of TypeScript codebases within AI systems. |
| 81 | + |
0 commit comments