vcode-analysis 0.13.0__py3-none-any.whl → 0.16.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- analyzers/architecture.py +45 -330
- analyzers/architecture_detect.py +235 -0
- analyzers/architecture_models.py +84 -0
- analyzers/code_review.py +129 -196
- analyzers/context_builder.py +13 -249
- analyzers/context_deps.py +191 -0
- analyzers/context_models.py +68 -0
- analyzers/directory.py +23 -538
- analyzers/directory_models.py +82 -0
- analyzers/directory_report.py +100 -0
- analyzers/directory_rules.py +108 -0
- analyzers/directory_tech.py +266 -0
- analyzers/dup_detect.py +390 -0
- analyzers/dup_models.py +178 -0
- analyzers/dup_suggest.py +124 -0
- analyzers/duplication.py +270 -875
- analyzers/kg_extract.py +351 -0
- analyzers/kg_helpers.py +340 -0
- analyzers/kg_models.py +241 -0
- analyzers/kg_render.py +275 -0
- analyzers/knowledge_graph.py +10 -1160
- analyzers/security.py +88 -679
- analyzers/security_context.py +163 -0
- analyzers/security_rules.py +37 -0
- analyzers/security_rules_dynamic.py +283 -0
- analyzers/security_rules_native.py +178 -0
- cli.py +15 -2062
- cli_analysis.py +391 -0
- cli_dup.py +232 -0
- cli_repos.py +358 -0
- cli_review.py +368 -0
- cli_review_commit.py +146 -0
- cli_security.py +341 -0
- cli_utils.py +320 -0
- core/batch_analyzer.py +141 -153
- core/git_batch.py +326 -0
- core/git_handler.py +24 -385
- core/git_models.py +71 -0
- core/prompts/batch_review.md +3 -1
- core/prompts/code_review.md +2 -0
- core/prompts/code_review_v2.md +2 -0
- core/report_generator.py +103 -506
- core/report_html_template.py +330 -0
- core/report_stats.py +107 -0
- parsers/c/regex_func_parser.py +189 -0
- parsers/c/regex_parser.py +12 -424
- parsers/c/regex_type_parser.py +240 -0
- parsers/javascript_models.py +93 -0
- parsers/javascript_parser.py +21 -237
- parsers/javascript_regex_fallback.py +164 -0
- parsers/kotlin/ast_class_parser.py +197 -0
- parsers/kotlin/ast_member_parser.py +239 -0
- parsers/kotlin/ast_parser.py +11 -435
- parsers/kotlin/regex_class_parser.py +111 -0
- parsers/kotlin/regex_helpers.py +97 -0
- parsers/kotlin/regex_member_parser.py +331 -0
- parsers/kotlin/regex_parser.py +15 -798
- parsers/kotlin/regex_type_parser.py +279 -0
- {vcode_analysis-0.13.0.dist-info → vcode_analysis-0.16.2.dist-info}/METADATA +25 -1
- vcode_analysis-0.16.2.dist-info/RECORD +111 -0
- vcode_analysis-0.16.2.dist-info/top_level.txt +12 -0
- vcode_mcp/server.py +1 -1
- vcode_mcp/tool_handler.py +2 -2
- vcode_analysis-0.13.0.dist-info/RECORD +0 -71
- vcode_analysis-0.13.0.dist-info/top_level.txt +0 -5
- {vcode_analysis-0.13.0.dist-info → vcode_analysis-0.16.2.dist-info}/WHEEL +0 -0
- {vcode_analysis-0.13.0.dist-info → vcode_analysis-0.16.2.dist-info}/entry_points.txt +0 -0
- {vcode_analysis-0.13.0.dist-info → vcode_analysis-0.16.2.dist-info}/licenses/LICENSE +0 -0
analyzers/architecture.py
CHANGED
|
@@ -1,146 +1,35 @@
|
|
|
1
1
|
"""架构分析器
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
分析项目结构、依赖关系、模块耦合度。
|
|
4
|
+
|
|
5
|
+
模块划分:
|
|
6
|
+
- architecture_models : 数据结构与导入语句正则
|
|
7
|
+
- architecture_detect : 导入检测与代码结构统计(AST 优先,正则回退)
|
|
4
8
|
"""
|
|
5
9
|
|
|
6
|
-
from dataclasses import dataclass, field
|
|
7
|
-
from typing import Optional
|
|
8
10
|
from collections import defaultdict
|
|
9
|
-
import re
|
|
10
|
-
from pathlib import Path
|
|
11
11
|
|
|
12
|
-
from core.analyzer import Analyzer, FileInfo
|
|
12
|
+
from core.analyzer import Analyzer, FileInfo
|
|
13
13
|
from core.prompt_manager import get_prompt_manager
|
|
14
14
|
from core.json_utils import remove_trailing_commas
|
|
15
15
|
|
|
16
|
-
#
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
TS_AST_AVAILABLE = True
|
|
34
|
-
except ImportError:
|
|
35
|
-
TS_AST_AVAILABLE = False
|
|
36
|
-
|
|
37
|
-
# Java AST 解析器(可选依赖)
|
|
38
|
-
try:
|
|
39
|
-
from parsers.java_parser import JavaASTParser
|
|
40
|
-
JAVA_AST_AVAILABLE = True
|
|
41
|
-
except ImportError:
|
|
42
|
-
JAVA_AST_AVAILABLE = False
|
|
43
|
-
|
|
44
|
-
# Kotlin AST 解析器(可选依赖)
|
|
45
|
-
try:
|
|
46
|
-
from parsers.kotlin_parser import KotlinASTParser
|
|
47
|
-
KOTLIN_AST_AVAILABLE = True
|
|
48
|
-
except ImportError:
|
|
49
|
-
KOTLIN_AST_AVAILABLE = False
|
|
50
|
-
|
|
51
|
-
# C AST 解析器(可选依赖)
|
|
52
|
-
try:
|
|
53
|
-
from parsers.c_parser import CASTParser
|
|
54
|
-
C_AST_AVAILABLE = True
|
|
55
|
-
except ImportError:
|
|
56
|
-
C_AST_AVAILABLE = False
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
@dataclass
|
|
60
|
-
class DependencyInfo:
|
|
61
|
-
"""依赖信息"""
|
|
62
|
-
source: str
|
|
63
|
-
target: str
|
|
64
|
-
import_type: str # import, from_import, require, include
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
@dataclass
|
|
68
|
-
class ModuleInfo:
|
|
69
|
-
"""模块信息"""
|
|
70
|
-
path: str
|
|
71
|
-
language: str
|
|
72
|
-
imports: list[str]
|
|
73
|
-
exports: list[str]
|
|
74
|
-
lines: int
|
|
75
|
-
classes: int
|
|
76
|
-
functions: int
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
@dataclass
|
|
80
|
-
class ArchitectureResult:
|
|
81
|
-
"""架构分析结果"""
|
|
82
|
-
total_files: int
|
|
83
|
-
total_lines: int
|
|
84
|
-
languages: dict[str, int]
|
|
85
|
-
modules: list[ModuleInfo]
|
|
86
|
-
dependencies: list[DependencyInfo]
|
|
87
|
-
circular_dependencies: list[tuple[str, str]]
|
|
88
|
-
coupling_score: float # 0-100, 越低越好
|
|
89
|
-
cohesion_score: float # 0-100, 越高越好
|
|
90
|
-
summary: str
|
|
91
|
-
suggestions: list[str]
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
# 架构分析提示词已迁移到 prompts/architecture.md
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
# 导入语句正则表达式
|
|
98
|
-
IMPORT_PATTERNS = {
|
|
99
|
-
"Python": [
|
|
100
|
-
r'^import\s+(\S+)',
|
|
101
|
-
r'^from\s+(\S+)\s+import',
|
|
102
|
-
],
|
|
103
|
-
"JavaScript": [
|
|
104
|
-
r'^import\s+.*from\s+[\'"]([^\'"]+)[\'"]',
|
|
105
|
-
r'^require\([\'"]([^\'"]+)[\'"]\)',
|
|
106
|
-
],
|
|
107
|
-
"TypeScript": [
|
|
108
|
-
r'^import\s+.*from\s+[\'"]([^\'"]+)[\'"]',
|
|
109
|
-
r'^import\s+[\'"]([^\'"]+)[\'"]',
|
|
110
|
-
],
|
|
111
|
-
"Java": [
|
|
112
|
-
r'^import\s+([\w\.]+);',
|
|
113
|
-
],
|
|
114
|
-
"Kotlin": [
|
|
115
|
-
r'^import\s+([\w\.]+)',
|
|
116
|
-
],
|
|
117
|
-
"C": [
|
|
118
|
-
r'^#include\s*<([^>]+)>',
|
|
119
|
-
r'^#include\s*"([^"]+)"',
|
|
120
|
-
],
|
|
121
|
-
"C++": [
|
|
122
|
-
r'^#include\s*<([^>]+)>',
|
|
123
|
-
r'^#include\s*"([^"]+)"',
|
|
124
|
-
],
|
|
125
|
-
"Go": [
|
|
126
|
-
r'^import\s+"([^"]+)"',
|
|
127
|
-
r'^import\s+\([^)]*\)',
|
|
128
|
-
],
|
|
129
|
-
"Rust": [
|
|
130
|
-
r'^use\s+([\w:]+)',
|
|
131
|
-
],
|
|
132
|
-
"Ruby": [
|
|
133
|
-
r'^require\s+[\'"]([^\'"]+)[\'"]',
|
|
134
|
-
r'^require_relative\s+[\'"]([^\'"]+)[\'"]',
|
|
135
|
-
],
|
|
136
|
-
"PHP": [
|
|
137
|
-
r'^use\s+([\w\\]+);',
|
|
138
|
-
r'^require(?:_once)?\s+[\'"]([^\'"]+)[\'"]',
|
|
139
|
-
],
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
class ArchitectureAnalyzer:
|
|
16
|
+
# 公开符号再导出(保持原有导入路径兼容)
|
|
17
|
+
from analyzers.architecture_models import ( # noqa: F401
|
|
18
|
+
ArchitectureResult,
|
|
19
|
+
DependencyInfo,
|
|
20
|
+
ModuleInfo,
|
|
21
|
+
)
|
|
22
|
+
from analyzers.architecture_detect import ArchitectureDetectMixin
|
|
23
|
+
|
|
24
|
+
__all__ = [
|
|
25
|
+
"ArchitectureAnalyzer",
|
|
26
|
+
"ArchitectureResult",
|
|
27
|
+
"ModuleInfo",
|
|
28
|
+
"DependencyInfo",
|
|
29
|
+
]
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class ArchitectureAnalyzer(ArchitectureDetectMixin):
|
|
144
33
|
"""架构分析器"""
|
|
145
34
|
|
|
146
35
|
def __init__(self, analyzer: Analyzer):
|
|
@@ -148,187 +37,6 @@ class ArchitectureAnalyzer:
|
|
|
148
37
|
self.modules: list[ModuleInfo] = []
|
|
149
38
|
self.dependencies: list[DependencyInfo] = []
|
|
150
39
|
|
|
151
|
-
def _count_lines(self, content: str) -> int:
|
|
152
|
-
"""准确计算代码行数"""
|
|
153
|
-
if not content:
|
|
154
|
-
return 0
|
|
155
|
-
# 如果以换行符结尾,换行符数量就是行数
|
|
156
|
-
# 否则,行数 = 换行符数量 + 1
|
|
157
|
-
if content.endswith('\n'):
|
|
158
|
-
return content.count('\n')
|
|
159
|
-
return content.count('\n') + 1
|
|
160
|
-
|
|
161
|
-
def _detect_imports(self, content: str, language: str, file_path: str = "") -> list[str]:
|
|
162
|
-
"""检测文件中的导入语句"""
|
|
163
|
-
imports = []
|
|
164
|
-
|
|
165
|
-
# Python 文件优先使用 AST 解析器
|
|
166
|
-
if language == "Python" and PYTHON_AST_AVAILABLE and file_path:
|
|
167
|
-
try:
|
|
168
|
-
parser = PythonASTParser()
|
|
169
|
-
result = parser.parse_code(content, file_path)
|
|
170
|
-
if result.success:
|
|
171
|
-
for imp in result.imports:
|
|
172
|
-
if imp.is_from and imp.module:
|
|
173
|
-
imports.append(imp.module)
|
|
174
|
-
else:
|
|
175
|
-
imports.extend(imp.names)
|
|
176
|
-
return imports
|
|
177
|
-
except Exception:
|
|
178
|
-
pass # 回退到正则表达式
|
|
179
|
-
|
|
180
|
-
# JavaScript 文件使用 AST 解析器
|
|
181
|
-
if language == "JavaScript" and JS_AST_AVAILABLE and file_path:
|
|
182
|
-
try:
|
|
183
|
-
parser = JavaScriptASTParser()
|
|
184
|
-
result = parser.parse_code(content, file_path)
|
|
185
|
-
if result.success:
|
|
186
|
-
for imp in result.imports:
|
|
187
|
-
imports.append(imp.source)
|
|
188
|
-
return imports
|
|
189
|
-
except Exception:
|
|
190
|
-
pass # 回退到正则表达式
|
|
191
|
-
|
|
192
|
-
# TypeScript 文件使用 AST 解析器
|
|
193
|
-
if language == "TypeScript" and TS_AST_AVAILABLE and file_path:
|
|
194
|
-
try:
|
|
195
|
-
parser = TypeScriptASTParser()
|
|
196
|
-
result = parser.parse_code(content, file_path)
|
|
197
|
-
if result.success:
|
|
198
|
-
for imp in result.imports:
|
|
199
|
-
imports.append(imp.source)
|
|
200
|
-
return imports
|
|
201
|
-
except Exception:
|
|
202
|
-
pass # 回退到正则表达式
|
|
203
|
-
|
|
204
|
-
# Java 文件使用 AST 解析器
|
|
205
|
-
if language == "Java" and JAVA_AST_AVAILABLE and file_path:
|
|
206
|
-
try:
|
|
207
|
-
parser = JavaASTParser()
|
|
208
|
-
result = parser.parse_code(content, file_path)
|
|
209
|
-
if result.success:
|
|
210
|
-
for imp in result.imports:
|
|
211
|
-
imports.append(imp.path)
|
|
212
|
-
return imports
|
|
213
|
-
except Exception:
|
|
214
|
-
pass # 回退到正则表达式
|
|
215
|
-
|
|
216
|
-
# 其他语言或 AST 解析失败时使用正则表达式
|
|
217
|
-
patterns = IMPORT_PATTERNS.get(language, [])
|
|
218
|
-
for line in content.split('\n'):
|
|
219
|
-
line = line.strip()
|
|
220
|
-
for pattern in patterns:
|
|
221
|
-
match = re.match(pattern, line)
|
|
222
|
-
if match:
|
|
223
|
-
imports.append(match.group(1))
|
|
224
|
-
|
|
225
|
-
return imports
|
|
226
|
-
|
|
227
|
-
def _count_structures(self, content: str, language: str, file_path: str = "") -> dict:
|
|
228
|
-
"""统计代码结构"""
|
|
229
|
-
result = {"classes": 0, "functions": 0, "docstring_coverage": 0.0, "type_hint_coverage": 0.0}
|
|
230
|
-
|
|
231
|
-
# Python 文件优先使用 AST 解析器
|
|
232
|
-
if language == "Python" and PYTHON_AST_AVAILABLE and file_path:
|
|
233
|
-
try:
|
|
234
|
-
parser = PythonASTParser()
|
|
235
|
-
ast_result = parser.parse_code(content, file_path)
|
|
236
|
-
if ast_result.success:
|
|
237
|
-
result["classes"] = len(ast_result.classes)
|
|
238
|
-
result["functions"] = len(ast_result.functions)
|
|
239
|
-
# 统计类中的方法
|
|
240
|
-
for cls in ast_result.classes:
|
|
241
|
-
result["functions"] += len(cls.methods)
|
|
242
|
-
result["docstring_coverage"] = ast_result.docstring_coverage
|
|
243
|
-
result["type_hint_coverage"] = ast_result.type_hint_coverage
|
|
244
|
-
return result
|
|
245
|
-
except Exception:
|
|
246
|
-
pass # 回退到正则表达式
|
|
247
|
-
|
|
248
|
-
# JavaScript 文件使用 AST 解析器
|
|
249
|
-
if language == "JavaScript" and JS_AST_AVAILABLE and file_path:
|
|
250
|
-
try:
|
|
251
|
-
parser = JavaScriptASTParser()
|
|
252
|
-
ast_result = parser.parse_code(content, file_path)
|
|
253
|
-
if ast_result.success:
|
|
254
|
-
result["classes"] = len(ast_result.classes)
|
|
255
|
-
result["functions"] = len(ast_result.functions)
|
|
256
|
-
# 统计类中的方法
|
|
257
|
-
for cls in ast_result.classes:
|
|
258
|
-
result["functions"] += len(cls.methods)
|
|
259
|
-
return result
|
|
260
|
-
except Exception:
|
|
261
|
-
pass # 回退到正则表达式
|
|
262
|
-
|
|
263
|
-
# TypeScript 文件使用 AST 解析器
|
|
264
|
-
if language == "TypeScript" and TS_AST_AVAILABLE and file_path:
|
|
265
|
-
try:
|
|
266
|
-
parser = TypeScriptASTParser()
|
|
267
|
-
ast_result = parser.parse_code(content, file_path)
|
|
268
|
-
if ast_result.success:
|
|
269
|
-
result["classes"] = len(ast_result.classes)
|
|
270
|
-
result["functions"] = len(ast_result.functions)
|
|
271
|
-
# 统计类中的方法
|
|
272
|
-
for cls in ast_result.classes:
|
|
273
|
-
result["functions"] += len(cls.methods)
|
|
274
|
-
# 统计 TypeScript 特有结构
|
|
275
|
-
result["interfaces"] = len(ast_result.interfaces)
|
|
276
|
-
result["types"] = len(ast_result.types)
|
|
277
|
-
result["enums"] = len(ast_result.enums)
|
|
278
|
-
return result
|
|
279
|
-
except Exception:
|
|
280
|
-
pass # 回退到正则表达式
|
|
281
|
-
|
|
282
|
-
# Java 文件使用 AST 解析器
|
|
283
|
-
if language == "Java" and JAVA_AST_AVAILABLE and file_path:
|
|
284
|
-
try:
|
|
285
|
-
parser = JavaASTParser()
|
|
286
|
-
ast_result = parser.parse_code(content, file_path)
|
|
287
|
-
if ast_result.success:
|
|
288
|
-
result["classes"] = len(ast_result.classes) + len(ast_result.interfaces) + len(ast_result.enums)
|
|
289
|
-
result["functions"] = 0
|
|
290
|
-
# 统计类中的方法
|
|
291
|
-
for cls in ast_result.classes:
|
|
292
|
-
result["functions"] += len(cls.methods)
|
|
293
|
-
for iface in ast_result.interfaces:
|
|
294
|
-
result["functions"] += len(iface.methods)
|
|
295
|
-
for enum in ast_result.enums:
|
|
296
|
-
result["functions"] += len(enum.methods)
|
|
297
|
-
return result
|
|
298
|
-
except Exception:
|
|
299
|
-
pass # 回退到正则表达式
|
|
300
|
-
|
|
301
|
-
# Kotlin 文件使用 AST 解析器
|
|
302
|
-
if language == "Kotlin" and KOTLIN_AST_AVAILABLE and file_path:
|
|
303
|
-
try:
|
|
304
|
-
parser = KotlinASTParser()
|
|
305
|
-
ast_result = parser.parse_code(content, file_path)
|
|
306
|
-
if ast_result.success:
|
|
307
|
-
result["classes"] = len(ast_result.classes)
|
|
308
|
-
result["functions"] = len(ast_result.functions)
|
|
309
|
-
# 统计类中的方法
|
|
310
|
-
for cls in ast_result.classes:
|
|
311
|
-
result["functions"] += len(cls.functions)
|
|
312
|
-
return result
|
|
313
|
-
except Exception:
|
|
314
|
-
pass # 回退到正则表达式
|
|
315
|
-
|
|
316
|
-
# 其他语言或 AST 解析失败时使用正则表达式
|
|
317
|
-
if language == "Python":
|
|
318
|
-
result["classes"] = len(re.findall(r'^class\s+\w+', content, re.MULTILINE))
|
|
319
|
-
result["functions"] = len(re.findall(r'^def\s+\w+', content, re.MULTILINE))
|
|
320
|
-
elif language in ["JavaScript", "TypeScript"]:
|
|
321
|
-
result["classes"] = len(re.findall(r'\bclass\s+\w+', content))
|
|
322
|
-
result["functions"] = len(re.findall(r'\bfunction\s+\w+', content))
|
|
323
|
-
elif language == "Java":
|
|
324
|
-
result["classes"] = len(re.findall(r'\bclass\s+\w+', content))
|
|
325
|
-
result["methods"] = len(re.findall(r'\b(public|private|protected)\s+\w+\s+\w+\s*\(', content))
|
|
326
|
-
elif language == "Kotlin":
|
|
327
|
-
result["classes"] = len(re.findall(r'\bclass\s+\w+', content))
|
|
328
|
-
result["functions"] = len(re.findall(r'\bfun\s+\w+', content))
|
|
329
|
-
|
|
330
|
-
return result
|
|
331
|
-
|
|
332
40
|
def analyze_file(self, file_info: FileInfo) -> ModuleInfo:
|
|
333
41
|
"""分析单个文件"""
|
|
334
42
|
try:
|
|
@@ -472,6 +180,23 @@ class ArchitectureAnalyzer:
|
|
|
472
180
|
modules_summary=modules_summary,
|
|
473
181
|
)
|
|
474
182
|
|
|
183
|
+
summary, suggestions = self._llm_summary(prompt, total_lines)
|
|
184
|
+
|
|
185
|
+
return ArchitectureResult(
|
|
186
|
+
total_files=len(self.modules),
|
|
187
|
+
total_lines=total_lines,
|
|
188
|
+
languages=dict(languages),
|
|
189
|
+
modules=self.modules,
|
|
190
|
+
dependencies=self.dependencies,
|
|
191
|
+
circular_dependencies=circular_deps,
|
|
192
|
+
coupling_score=coupling_score,
|
|
193
|
+
cohesion_score=cohesion_score,
|
|
194
|
+
summary=summary,
|
|
195
|
+
suggestions=suggestions,
|
|
196
|
+
)
|
|
197
|
+
|
|
198
|
+
def _llm_summary(self, prompt: str, total_lines: int) -> tuple[str, list]:
|
|
199
|
+
"""调用 LLM 生成架构总结与建议,失败时回退默认值。"""
|
|
475
200
|
try:
|
|
476
201
|
response = self.analyzer.llm.simple_chat(prompt)
|
|
477
202
|
import json
|
|
@@ -485,19 +210,9 @@ class ArchitectureAnalyzer:
|
|
|
485
210
|
data = json.loads(remove_trailing_commas(content.strip()))
|
|
486
211
|
summary = data.get("summary", "")
|
|
487
212
|
suggestions = data.get("suggestions", [])
|
|
213
|
+
return summary, suggestions
|
|
488
214
|
except Exception:
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
total_files=len(self.modules),
|
|
494
|
-
total_lines=total_lines,
|
|
495
|
-
languages=dict(languages),
|
|
496
|
-
modules=self.modules,
|
|
497
|
-
dependencies=self.dependencies,
|
|
498
|
-
circular_dependencies=circular_deps,
|
|
499
|
-
coupling_score=coupling_score,
|
|
500
|
-
cohesion_score=cohesion_score,
|
|
501
|
-
summary=summary,
|
|
502
|
-
suggestions=suggestions,
|
|
503
|
-
)
|
|
215
|
+
return (
|
|
216
|
+
f"项目包含 {len(self.modules)} 个文件,{total_lines} 行代码",
|
|
217
|
+
["建议添加更多文档", "考虑模块化重构"],
|
|
218
|
+
)
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
"""架构分析器:导入检测与代码结构统计 Mixin
|
|
2
|
+
|
|
3
|
+
各语言优先使用 AST 解析器,失败或不可用时回退正则表达式。
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
import re
|
|
7
|
+
|
|
8
|
+
from analyzers.architecture_models import IMPORT_PATTERNS
|
|
9
|
+
|
|
10
|
+
# Python AST 解析器(可选依赖)
|
|
11
|
+
try:
|
|
12
|
+
from parsers.python_parser import PythonASTParser
|
|
13
|
+
PYTHON_AST_AVAILABLE = True
|
|
14
|
+
except ImportError:
|
|
15
|
+
PYTHON_AST_AVAILABLE = False
|
|
16
|
+
|
|
17
|
+
# JavaScript AST 解析器(可选依赖)
|
|
18
|
+
try:
|
|
19
|
+
from parsers.javascript_parser import JavaScriptASTParser
|
|
20
|
+
JS_AST_AVAILABLE = True
|
|
21
|
+
except ImportError:
|
|
22
|
+
JS_AST_AVAILABLE = False
|
|
23
|
+
|
|
24
|
+
# TypeScript AST 解析器(可选依赖)
|
|
25
|
+
try:
|
|
26
|
+
from parsers.typescript_parser import TypeScriptASTParser
|
|
27
|
+
TS_AST_AVAILABLE = True
|
|
28
|
+
except ImportError:
|
|
29
|
+
TS_AST_AVAILABLE = False
|
|
30
|
+
|
|
31
|
+
# Java AST 解析器(可选依赖)
|
|
32
|
+
try:
|
|
33
|
+
from parsers.java_parser import JavaASTParser
|
|
34
|
+
JAVA_AST_AVAILABLE = True
|
|
35
|
+
except ImportError:
|
|
36
|
+
JAVA_AST_AVAILABLE = False
|
|
37
|
+
|
|
38
|
+
# Kotlin AST 解析器(可选依赖)
|
|
39
|
+
try:
|
|
40
|
+
from parsers.kotlin_parser import KotlinASTParser
|
|
41
|
+
KOTLIN_AST_AVAILABLE = True
|
|
42
|
+
except ImportError:
|
|
43
|
+
KOTLIN_AST_AVAILABLE = False
|
|
44
|
+
|
|
45
|
+
# C AST 解析器(可选依赖)
|
|
46
|
+
try:
|
|
47
|
+
from parsers.c_parser import CASTParser
|
|
48
|
+
C_AST_AVAILABLE = True
|
|
49
|
+
except ImportError:
|
|
50
|
+
C_AST_AVAILABLE = False
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
class ArchitectureDetectMixin:
|
|
54
|
+
"""导入检测与代码结构统计"""
|
|
55
|
+
|
|
56
|
+
def _count_lines(self, content: str) -> int:
|
|
57
|
+
"""准确计算代码行数"""
|
|
58
|
+
if not content:
|
|
59
|
+
return 0
|
|
60
|
+
# 如果以换行符结尾,换行符数量就是行数
|
|
61
|
+
# 否则,行数 = 换行符数量 + 1
|
|
62
|
+
if content.endswith('\n'):
|
|
63
|
+
return content.count('\n')
|
|
64
|
+
return content.count('\n') + 1
|
|
65
|
+
|
|
66
|
+
def _detect_imports(self, content: str, language: str, file_path: str = "") -> list[str]:
|
|
67
|
+
"""检测文件中的导入语句"""
|
|
68
|
+
imports = []
|
|
69
|
+
|
|
70
|
+
# Python 文件优先使用 AST 解析器
|
|
71
|
+
if language == "Python" and PYTHON_AST_AVAILABLE and file_path:
|
|
72
|
+
try:
|
|
73
|
+
parser = PythonASTParser()
|
|
74
|
+
result = parser.parse_code(content, file_path)
|
|
75
|
+
if result.success:
|
|
76
|
+
for imp in result.imports:
|
|
77
|
+
if imp.is_from and imp.module:
|
|
78
|
+
imports.append(imp.module)
|
|
79
|
+
else:
|
|
80
|
+
imports.extend(imp.names)
|
|
81
|
+
return imports
|
|
82
|
+
except Exception:
|
|
83
|
+
pass # 回退到正则表达式
|
|
84
|
+
|
|
85
|
+
# JavaScript 文件使用 AST 解析器
|
|
86
|
+
if language == "JavaScript" and JS_AST_AVAILABLE and file_path:
|
|
87
|
+
try:
|
|
88
|
+
parser = JavaScriptASTParser()
|
|
89
|
+
result = parser.parse_code(content, file_path)
|
|
90
|
+
if result.success:
|
|
91
|
+
for imp in result.imports:
|
|
92
|
+
imports.append(imp.source)
|
|
93
|
+
return imports
|
|
94
|
+
except Exception:
|
|
95
|
+
pass # 回退到正则表达式
|
|
96
|
+
|
|
97
|
+
# TypeScript 文件使用 AST 解析器
|
|
98
|
+
if language == "TypeScript" and TS_AST_AVAILABLE and file_path:
|
|
99
|
+
try:
|
|
100
|
+
parser = TypeScriptASTParser()
|
|
101
|
+
result = parser.parse_code(content, file_path)
|
|
102
|
+
if result.success:
|
|
103
|
+
for imp in result.imports:
|
|
104
|
+
imports.append(imp.source)
|
|
105
|
+
return imports
|
|
106
|
+
except Exception:
|
|
107
|
+
pass # 回退到正则表达式
|
|
108
|
+
|
|
109
|
+
# Java 文件使用 AST 解析器
|
|
110
|
+
if language == "Java" and JAVA_AST_AVAILABLE and file_path:
|
|
111
|
+
try:
|
|
112
|
+
parser = JavaASTParser()
|
|
113
|
+
result = parser.parse_code(content, file_path)
|
|
114
|
+
if result.success:
|
|
115
|
+
for imp in result.imports:
|
|
116
|
+
imports.append(imp.path)
|
|
117
|
+
return imports
|
|
118
|
+
except Exception:
|
|
119
|
+
pass # 回退到正则表达式
|
|
120
|
+
|
|
121
|
+
# 其他语言或 AST 解析失败时使用正则表达式
|
|
122
|
+
patterns = IMPORT_PATTERNS.get(language, [])
|
|
123
|
+
for line in content.split('\n'):
|
|
124
|
+
line = line.strip()
|
|
125
|
+
for pattern in patterns:
|
|
126
|
+
match = re.match(pattern, line)
|
|
127
|
+
if match:
|
|
128
|
+
imports.append(match.group(1))
|
|
129
|
+
|
|
130
|
+
return imports
|
|
131
|
+
|
|
132
|
+
def _count_structures(self, content: str, language: str, file_path: str = "") -> dict:
|
|
133
|
+
"""统计代码结构"""
|
|
134
|
+
result = {"classes": 0, "functions": 0, "docstring_coverage": 0.0, "type_hint_coverage": 0.0}
|
|
135
|
+
|
|
136
|
+
# Python 文件优先使用 AST 解析器
|
|
137
|
+
if language == "Python" and PYTHON_AST_AVAILABLE and file_path:
|
|
138
|
+
try:
|
|
139
|
+
parser = PythonASTParser()
|
|
140
|
+
ast_result = parser.parse_code(content, file_path)
|
|
141
|
+
if ast_result.success:
|
|
142
|
+
result["classes"] = len(ast_result.classes)
|
|
143
|
+
result["functions"] = len(ast_result.functions)
|
|
144
|
+
# 统计类中的方法
|
|
145
|
+
for cls in ast_result.classes:
|
|
146
|
+
result["functions"] += len(cls.methods)
|
|
147
|
+
result["docstring_coverage"] = ast_result.docstring_coverage
|
|
148
|
+
result["type_hint_coverage"] = ast_result.type_hint_coverage
|
|
149
|
+
return result
|
|
150
|
+
except Exception:
|
|
151
|
+
pass # 回退到正则表达式
|
|
152
|
+
|
|
153
|
+
# JavaScript 文件使用 AST 解析器
|
|
154
|
+
if language == "JavaScript" and JS_AST_AVAILABLE and file_path:
|
|
155
|
+
try:
|
|
156
|
+
parser = JavaScriptASTParser()
|
|
157
|
+
ast_result = parser.parse_code(content, file_path)
|
|
158
|
+
if ast_result.success:
|
|
159
|
+
result["classes"] = len(ast_result.classes)
|
|
160
|
+
result["functions"] = len(ast_result.functions)
|
|
161
|
+
# 统计类中的方法
|
|
162
|
+
for cls in ast_result.classes:
|
|
163
|
+
result["functions"] += len(cls.methods)
|
|
164
|
+
return result
|
|
165
|
+
except Exception:
|
|
166
|
+
pass # 回退到正则表达式
|
|
167
|
+
|
|
168
|
+
# TypeScript 文件使用 AST 解析器
|
|
169
|
+
if language == "TypeScript" and TS_AST_AVAILABLE and file_path:
|
|
170
|
+
try:
|
|
171
|
+
parser = TypeScriptASTParser()
|
|
172
|
+
ast_result = parser.parse_code(content, file_path)
|
|
173
|
+
if ast_result.success:
|
|
174
|
+
result["classes"] = len(ast_result.classes)
|
|
175
|
+
result["functions"] = len(ast_result.functions)
|
|
176
|
+
# 统计类中的方法
|
|
177
|
+
for cls in ast_result.classes:
|
|
178
|
+
result["functions"] += len(cls.methods)
|
|
179
|
+
# 统计 TypeScript 特有结构
|
|
180
|
+
result["interfaces"] = len(ast_result.interfaces)
|
|
181
|
+
result["types"] = len(ast_result.types)
|
|
182
|
+
result["enums"] = len(ast_result.enums)
|
|
183
|
+
return result
|
|
184
|
+
except Exception:
|
|
185
|
+
pass # 回退到正则表达式
|
|
186
|
+
|
|
187
|
+
# Java 文件使用 AST 解析器
|
|
188
|
+
if language == "Java" and JAVA_AST_AVAILABLE and file_path:
|
|
189
|
+
try:
|
|
190
|
+
parser = JavaASTParser()
|
|
191
|
+
ast_result = parser.parse_code(content, file_path)
|
|
192
|
+
if ast_result.success:
|
|
193
|
+
result["classes"] = len(ast_result.classes) + len(ast_result.interfaces) + len(ast_result.enums)
|
|
194
|
+
result["functions"] = 0
|
|
195
|
+
# 统计类中的方法
|
|
196
|
+
for cls in ast_result.classes:
|
|
197
|
+
result["functions"] += len(cls.methods)
|
|
198
|
+
for iface in ast_result.interfaces:
|
|
199
|
+
result["functions"] += len(iface.methods)
|
|
200
|
+
for enum in ast_result.enums:
|
|
201
|
+
result["functions"] += len(enum.methods)
|
|
202
|
+
return result
|
|
203
|
+
except Exception:
|
|
204
|
+
pass # 回退到正则表达式
|
|
205
|
+
|
|
206
|
+
# Kotlin 文件使用 AST 解析器
|
|
207
|
+
if language == "Kotlin" and KOTLIN_AST_AVAILABLE and file_path:
|
|
208
|
+
try:
|
|
209
|
+
parser = KotlinASTParser()
|
|
210
|
+
ast_result = parser.parse_code(content, file_path)
|
|
211
|
+
if ast_result.success:
|
|
212
|
+
result["classes"] = len(ast_result.classes)
|
|
213
|
+
result["functions"] = len(ast_result.functions)
|
|
214
|
+
# 统计类中的方法
|
|
215
|
+
for cls in ast_result.classes:
|
|
216
|
+
result["functions"] += len(cls.functions)
|
|
217
|
+
return result
|
|
218
|
+
except Exception:
|
|
219
|
+
pass # 回退到正则表达式
|
|
220
|
+
|
|
221
|
+
# 其他语言或 AST 解析失败时使用正则表达式
|
|
222
|
+
if language == "Python":
|
|
223
|
+
result["classes"] = len(re.findall(r'^class\s+\w+', content, re.MULTILINE))
|
|
224
|
+
result["functions"] = len(re.findall(r'^def\s+\w+', content, re.MULTILINE))
|
|
225
|
+
elif language in ["JavaScript", "TypeScript"]:
|
|
226
|
+
result["classes"] = len(re.findall(r'\bclass\s+\w+', content))
|
|
227
|
+
result["functions"] = len(re.findall(r'\bfunction\s+\w+', content))
|
|
228
|
+
elif language == "Java":
|
|
229
|
+
result["classes"] = len(re.findall(r'\bclass\s+\w+', content))
|
|
230
|
+
result["methods"] = len(re.findall(r'\b(public|private|protected)\s+\w+\s+\w+\s*\(', content))
|
|
231
|
+
elif language == "Kotlin":
|
|
232
|
+
result["classes"] = len(re.findall(r'\bclass\s+\w+', content))
|
|
233
|
+
result["functions"] = len(re.findall(r'\bfun\s+\w+', content))
|
|
234
|
+
|
|
235
|
+
return result
|