|
1 | | -# Analyzers Package |
| 1 | +# Codegen Analyzers |
2 | 2 |
|
3 | | -This package provides tools for analyzing and modifying code during analysis. |
| 3 | +This directory contains the code analysis modules for the Codegen project. These analyzers provide comprehensive static code analysis, quality checking, dependency analysis, and PR validation capabilities. |
| 4 | + |
| 5 | +## Modules |
| 6 | + |
| 7 | +### Core Analyzers |
| 8 | + |
| 9 | +- **analyzer.py**: Modern analyzer architecture with plugin system |
| 10 | +- **base_analyzer.py**: Base class for all code analyzers |
| 11 | +- **codebase_analyzer.py**: Comprehensive codebase analysis |
| 12 | +- **code_quality.py**: Code quality analysis |
| 13 | +- **dependencies.py**: Dependency analysis |
| 14 | +- **error_analyzer.py**: Error detection and analysis |
| 15 | +- **parser.py**: Code parsing and AST generation for multiple languages |
| 16 | +- **transaction_manager.py**: Transaction manager for handling code modifications |
| 17 | + |
| 18 | +### Support Modules |
| 19 | + |
| 20 | +- **api.py**: API interface for analyzers |
| 21 | +- **analyzer_manager.py**: Manages analyzer plugins |
| 22 | +- **codebase_context.py**: Provides context for codebase analysis |
| 23 | +- **codebase_visualizer.py**: Visualization tools for codebases |
| 24 | +- **issue_analyzer.py**: Issue detection and analysis |
| 25 | +- **issue_types.py**: Definitions for issue types |
| 26 | +- **issues.py**: Issue tracking system |
| 27 | + |
| 28 | +## Parser Module |
| 29 | + |
| 30 | +The `parser.py` module provides specialized parsing functionality for code analysis, including abstract syntax tree (AST) generation and traversal for multiple programming languages. It serves as a foundation for various code analyzers in the system. |
| 31 | + |
| 32 | +### Key Features |
| 33 | + |
| 34 | +- Abstract syntax tree (AST) generation and traversal |
| 35 | +- Support for multiple programming languages (Python, JavaScript, TypeScript) |
| 36 | +- Symbol extraction (functions, classes, variables) |
| 37 | +- Dependency analysis (imports, requires) |
| 38 | +- Error handling and reporting |
| 39 | + |
| 40 | +### Usage Examples |
| 41 | + |
| 42 | +#### Basic Parsing |
| 43 | + |
| 44 | +```python |
| 45 | +from codegen_on_oss.analyzers.parser import parse_file, parse_code |
| 46 | + |
| 47 | +# Parse a file |
| 48 | +ast = parse_file("path/to/file.py") |
| 49 | + |
| 50 | +# Parse code directly |
| 51 | +code = "def hello(): print('Hello, World!')" |
| 52 | +ast = parse_code(code, "python") |
| 53 | +``` |
| 54 | + |
| 55 | +#### Language-Specific Parsing |
| 56 | + |
| 57 | +```python |
| 58 | +from codegen_on_oss.analyzers.parser import PythonParser, JavaScriptParser, TypeScriptParser |
| 59 | + |
| 60 | +# Python parsing |
| 61 | +python_parser = PythonParser() |
| 62 | +python_ast = python_parser.parse_file("script.py") |
| 63 | + |
| 64 | +# JavaScript parsing |
| 65 | +js_parser = JavaScriptParser() |
| 66 | +js_ast = js_parser.parse_file("app.js") |
| 67 | + |
| 68 | +# TypeScript parsing |
| 69 | +ts_parser = TypeScriptParser() |
| 70 | +ts_ast = ts_parser.parse_file("component.ts") |
| 71 | +``` |
| 72 | + |
| 73 | +#### Symbol and Dependency Extraction |
| 74 | + |
| 75 | +```python |
| 76 | +from codegen_on_oss.analyzers.parser import parse_file, create_parser |
| 77 | + |
| 78 | +# Parse a file |
| 79 | +ast = parse_file("path/to/file.py") |
| 80 | + |
| 81 | +# Create a parser for the language |
| 82 | +parser = create_parser("python") |
| 83 | + |
| 84 | +# Extract symbols (functions, classes, variables) |
| 85 | +symbols = parser.get_symbols(ast) |
| 86 | +for symbol in symbols: |
| 87 | + print(f"{symbol['type']}: {symbol['name']}") |
| 88 | + |
| 89 | +# Extract dependencies (imports, requires) |
| 90 | +dependencies = parser.get_dependencies(ast) |
| 91 | +for dep in dependencies: |
| 92 | + if dep["type"] == "import": |
| 93 | + print(f"import {dep['module']}") |
| 94 | + elif dep["type"] == "from_import": |
| 95 | + print(f"from {dep['module']} import {dep['name']}") |
| 96 | +``` |
4 | 97 |
|
5 | 98 | ## Transaction Manager |
6 | 99 |
|
@@ -70,222 +163,3 @@ The transaction manager can raise the following exceptions: |
70 | 163 | ### Integration with Analyzers |
71 | 164 |
|
72 | 165 | The transaction manager is designed to be used with the analyzers package to provide a consistent way to modify code during analysis. It can be integrated with other components of the analyzers package to provide a complete code analysis and modification solution. |
73 | | - |
74 | | -## Core Components |
75 | | - |
76 | | -### 1. API Interface (`api.py`) |
77 | | - |
78 | | -The main entry point for frontend applications. Provides REST-like endpoints for: |
79 | | - |
80 | | -- Codebase analysis |
81 | | -- PR analysis |
82 | | -- Dependency visualization |
83 | | -- Issue reporting |
84 | | -- Code quality assessment |
85 | | - |
86 | | -### 2. Analyzer System (`analyzer.py`) |
87 | | - |
88 | | -Plugin-based system that coordinates different types of analysis: |
89 | | - |
90 | | -- Code quality analysis (complexity, maintainability) |
91 | | -- Dependency analysis (imports, cycles, coupling) |
92 | | -- PR impact analysis |
93 | | -- Type checking and error detection |
94 | | - |
95 | | -### 3. Issue Tracking (`issues.py`) |
96 | | - |
97 | | -Comprehensive issue model with: |
98 | | - |
99 | | -- Severity levels (critical, error, warning, info) |
100 | | -- Categories (dead code, complexity, dependency, etc.) |
101 | | -- Location information and suggestions |
102 | | -- Filtering and grouping capabilities |
103 | | - |
104 | | -### 4. Dependency Analysis (`dependencies.py`) |
105 | | - |
106 | | -Analysis of codebase dependencies: |
107 | | - |
108 | | -- Import dependencies between modules |
109 | | -- Circular dependency detection |
110 | | -- Module coupling analysis |
111 | | -- External dependencies tracking |
112 | | -- Call graphs and class hierarchies |
113 | | - |
114 | | -### 5. Code Quality Analysis (`code_quality.py`) |
115 | | - |
116 | | -Analysis of code quality aspects: |
117 | | - |
118 | | -- Dead code detection (unused functions, variables) |
119 | | -- Complexity metrics (cyclomatic, cognitive) |
120 | | -- Parameter checking (types, usage) |
121 | | -- Style issues and maintainability |
122 | | - |
123 | | -## Using the API |
124 | | - |
125 | | -### Setup |
126 | | - |
127 | | -```python |
128 | | -from codegen_on_oss.analyzers.api import CodegenAnalyzerAPI |
129 | | - |
130 | | -# Create API instance with repository |
131 | | -api = CodegenAnalyzerAPI(repo_path="/path/to/repo") |
132 | | -# OR |
133 | | -api = CodegenAnalyzerAPI(repo_url="https://github.com/owner/repo") |
134 | | -``` |
135 | | - |
136 | | -### Analyzing a Codebase |
137 | | - |
138 | | -```python |
139 | | -# Run comprehensive analysis |
140 | | -results = api.analyze_codebase() |
141 | | - |
142 | | -# Run specific analysis types |
143 | | -results = api.analyze_codebase(analysis_types=["code_quality", "dependency"]) |
144 | | - |
145 | | -# Force refresh of cached analysis |
146 | | -results = api.analyze_codebase(force_refresh=True) |
147 | | -``` |
148 | | - |
149 | | -### Analyzing a PR |
150 | | - |
151 | | -```python |
152 | | -# Analyze a specific PR |
153 | | -pr_results = api.analyze_pr(pr_number=123) |
154 | | - |
155 | | -# Get PR impact visualization |
156 | | -impact_viz = api.get_pr_impact(pr_number=123, format="json") |
157 | | -``` |
158 | | - |
159 | | -### Getting Issues |
160 | | - |
161 | | -```python |
162 | | -# Get all issues |
163 | | -all_issues = api.get_issues() |
164 | | - |
165 | | -# Get issues by severity |
166 | | -critical_issues = api.get_issues(severity="critical") |
167 | | -error_issues = api.get_issues(severity="error") |
168 | | - |
169 | | -# Get issues by category |
170 | | -dependency_issues = api.get_issues(category="dependency_cycle") |
171 | | -``` |
172 | | - |
173 | | -### Getting Visualizations |
174 | | - |
175 | | -```python |
176 | | -# Get module dependency graph |
177 | | -module_deps = api.get_module_dependencies(format="json") |
178 | | - |
179 | | -# Get function call graph |
180 | | -call_graph = api.get_function_call_graph(function_name="main", depth=3, format="json") |
181 | | - |
182 | | -# Export visualization to file |
183 | | -api.export_visualization(call_graph, format="html", filename="call_graph.html") |
184 | | -``` |
185 | | - |
186 | | -### Common Analysis Patterns |
187 | | - |
188 | | -```python |
189 | | -# Find dead code |
190 | | -api.analyze_codebase(analysis_types=["code_quality"]) |
191 | | -dead_code = api.get_issues(category="dead_code") |
192 | | - |
193 | | -# Find circular dependencies |
194 | | -api.analyze_codebase(analysis_types=["dependency"]) |
195 | | -circular_deps = api.get_circular_dependencies() |
196 | | - |
197 | | -# Find parameter issues |
198 | | -api.analyze_codebase(analysis_types=["code_quality"]) |
199 | | -param_issues = api.get_parameter_issues() |
200 | | -``` |
201 | | - |
202 | | -## REST API Endpoints |
203 | | - |
204 | | -The analyzer can be exposed as REST API endpoints for integration with frontend applications: |
205 | | - |
206 | | -### Codebase Analysis |
207 | | - |
208 | | -``` |
209 | | -POST /api/analyze/codebase |
210 | | -{ |
211 | | - "repo_path": "/path/to/repo", |
212 | | - "analysis_types": ["code_quality", "dependency"] |
213 | | -} |
214 | | -``` |
215 | | - |
216 | | -### PR Analysis |
217 | | - |
218 | | -``` |
219 | | -POST /api/analyze/pr |
220 | | -{ |
221 | | - "repo_path": "/path/to/repo", |
222 | | - "pr_number": 123 |
223 | | -} |
224 | | -``` |
225 | | - |
226 | | -### Visualization |
227 | | - |
228 | | -``` |
229 | | -POST /api/visualize |
230 | | -{ |
231 | | - "repo_path": "/path/to/repo", |
232 | | - "viz_type": "module_dependencies", |
233 | | - "params": { |
234 | | - "layout": "hierarchical", |
235 | | - "format": "json" |
236 | | - } |
237 | | -} |
238 | | -``` |
239 | | - |
240 | | -### Issues |
241 | | - |
242 | | -``` |
243 | | -GET /api/issues?severity=error&category=dependency_cycle |
244 | | -``` |
245 | | - |
246 | | -## Implementation Example |
247 | | - |
248 | | -For a web application exposing these endpoints with Flask: |
249 | | - |
250 | | -```python |
251 | | -from flask import Flask, request, jsonify |
252 | | -from codegen_on_oss.analyzers.api import api_analyze_codebase, api_analyze_pr, api_get_visualization, api_get_static_errors |
253 | | - |
254 | | -app = Flask(__name__) |
255 | | - |
256 | | - |
257 | | -@app.route("/api/analyze/codebase", methods=["POST"]) |
258 | | -def analyze_codebase(): |
259 | | - data = request.json |
260 | | - result = api_analyze_codebase(repo_path=data.get("repo_path"), analysis_types=data.get("analysis_types")) |
261 | | - return jsonify(result) |
262 | | - |
263 | | - |
264 | | -@app.route("/api/analyze/pr", methods=["POST"]) |
265 | | -def analyze_pr(): |
266 | | - data = request.json |
267 | | - result = api_analyze_pr(repo_path=data.get("repo_path"), pr_number=data.get("pr_number")) |
268 | | - return jsonify(result) |
269 | | - |
270 | | - |
271 | | -@app.route("/api/visualize", methods=["POST"]) |
272 | | -def visualize(): |
273 | | - data = request.json |
274 | | - result = api_get_visualization(repo_path=data.get("repo_path"), viz_type=data.get("viz_type"), params=data.get("params", {})) |
275 | | - return jsonify(result) |
276 | | - |
277 | | - |
278 | | -@app.route("/api/issues", methods=["GET"]) |
279 | | -def get_issues(): |
280 | | - repo_path = request.args.get("repo_path") |
281 | | - severity = request.args.get("severity") |
282 | | - category = request.args.get("category") |
283 | | - |
284 | | - api = create_api(repo_path=repo_path) |
285 | | - return jsonify(api.get_issues(severity=severity, category=category)) |
286 | | - |
287 | | - |
288 | | -if __name__ == "__main__": |
289 | | - app.run(debug=True) |
290 | | -``` |
291 | | - |
0 commit comments