import ast import logging from pathlib import Path from dataclasses import dataclass, field from typing import Optional logger = logging.getLogger(__name__) @dataclass class ClassInfo: name: str file_path: str line_start: int line_end: int methods: list["MethodInfo"] = field(default_factory=list) base_classes: list[str] = field(default_factory=list) decorators: list[str] = field(default_factory=list) docstring: str = "" is_abstract: bool = False is_dataclass: bool = False @dataclass class MethodInfo: name: str file_path: str class_name: str = "" line_start: int = 0 line_end: int = 0 args: list[str] = field(default_factory=list) return_type: str = "" decorators: list[str] = field(default_factory=list) docstring: str = "" is_async: bool = False is_static: bool = False is_classmethod: bool = False is_property: bool = False @dataclass class FunctionInfo: name: str file_path: str line_start: int = 0 line_end: int = 0 args: list[str] = field(default_factory=list) return_type: str = "" decorators: list[str] = field(default_factory=list) docstring: str = "" is_async: bool = False @dataclass class ImportInfo: module: str names: list[str] = field(default_factory=list) is_from_import: bool = False alias: str = "" @dataclass class FileAnalysis: path: str language: str classes: list[ClassInfo] = field(default_factory=list) functions: list[FunctionInfo] = field(default_factory=list) imports: list[ImportInfo] = field(default_factory=list) docstring: str = "" line_count: int = 0 class CodeAnalyzer: def __init__(self, repo_path: Path): self.repo_path = Path(repo_path) def analyze_file(self, file_path: str) -> Optional[FileAnalysis]: full_path = self.repo_path / file_path if not full_path.exists(): logger.warning(f"文件不存在: {full_path}") return None ext = full_path.suffix if ext == ".py": return self._analyze_python(full_path, file_path) elif ext in (".ts", ".tsx"): return self._analyze_typescript(full_path, file_path) else: return self._analyze_generic(full_path, file_path) def analyze_all(self, files: list[str]) -> list[FileAnalysis]: results = [] for f in files: analysis = self.analyze_file(f) if analysis: results.append(analysis) return results def _analyze_python(self, full_path: Path, rel_path: str) -> FileAnalysis: analysis = FileAnalysis(path=rel_path, language="python") try: source = full_path.read_text(encoding="utf-8") tree = ast.parse(source) analysis.line_count = len(source.splitlines()) analysis.docstring = ast.get_docstring(tree) or "" for node in ast.walk(tree): if isinstance(node, ast.Import): for alias in node.names: analysis.imports.append(ImportInfo( module=alias.name, names=[alias.name], alias=alias.asname or "", )) elif isinstance(node, ast.ImportFrom): analysis.imports.append(ImportInfo( module=node.module or "", names=[a.name for a in node.names], is_from_import=True, )) for node in ast.iter_child_nodes(tree): if isinstance(node, ast.ClassDef): class_info = self._extract_class_info(node, rel_path, source) analysis.classes.append(class_info) elif isinstance(node, ast.FunctionDef): func_info = self._extract_function_info(node, rel_path, source) analysis.functions.append(func_info) elif isinstance(node, ast.AsyncFunctionDef): func_info = self._extract_function_info(node, rel_path, source, is_async=True) analysis.functions.append(func_info) except SyntaxError as e: logger.warning(f"Python 语法错误 {rel_path}: {e}") except Exception as e: logger.warning(f"分析文件失败 {rel_path}: {e}") return analysis def _extract_class_info(self, node: ast.ClassDef, rel_path: str, source: str) -> ClassInfo: bases = [] for base in node.bases: if isinstance(base, ast.Name): bases.append(base.id) elif isinstance(base, ast.Attribute): bases.append(ast.unparse(base)) decorators = [] is_abstract = False is_dataclass = False for dec in node.decorator_list: dname = ast.unparse(dec) decorators.append(dname) if "abstractmethod" in dname or "ABCMeta" in dname: is_abstract = True if "dataclass" in dname: is_dataclass = True class_info = ClassInfo( name=node.name, file_path=rel_path, line_start=node.lineno, line_end=node.end_lineno or node.lineno, base_classes=bases, decorators=decorators, docstring=ast.get_docstring(node) or "", is_abstract=is_abstract, is_dataclass=is_dataclass, ) for child in node.body: if isinstance(child, ast.FunctionDef): method = self._extract_function_info(child, rel_path, source) method.class_name = node.name class_info.methods.append(method) elif isinstance(child, ast.AsyncFunctionDef): method = self._extract_function_info(child, rel_path, source, is_async=True) method.class_name = node.name class_info.methods.append(method) return class_info def _extract_function_info(self, node, rel_path: str, source: str, is_async: bool = False) -> MethodInfo: args = [] for arg in node.args.args: arg_str = arg.arg if arg.annotation: arg_str += f": {ast.unparse(arg.annotation)}" args.append(arg_str) return_type = "" if node.returns: return_type = ast.unparse(node.returns) decorators = [] is_static = False is_classmethod = False is_property = False for dec in node.decorator_list: dname = ast.unparse(dec) decorators.append(dname) if "staticmethod" in dname: is_static = True if "classmethod" in dname: is_classmethod = True if "property" in dname: is_property = True return MethodInfo( name=node.name, file_path=rel_path, line_start=node.lineno, line_end=node.end_lineno or node.lineno, args=args, return_type=return_type, decorators=decorators, docstring=ast.get_docstring(node) or "", is_async=is_async, is_static=is_static, is_classmethod=is_classmethod, is_property=is_property, ) def _analyze_typescript(self, full_path: Path, rel_path: str) -> FileAnalysis: analysis = FileAnalysis(path=rel_path, language="typescript") try: source = full_path.read_text(encoding="utf-8") analysis.line_count = len(source.splitlines()) analysis.classes = self._extract_ts_classes(source, rel_path) analysis.functions = self._extract_ts_functions(source, rel_path) analysis.imports = self._extract_ts_imports(source) except Exception as e: logger.warning(f"分析 TypeScript 文件失败 {rel_path}: {e}") return analysis def _extract_ts_classes(self, source: str, rel_path: str) -> list[ClassInfo]: import re classes = [] pattern = re.compile( r'(?:export\s+(?:abstract\s+)?)?class\s+(\w+)(?:\s+extends\s+(\w+))?\s*\{', re.MULTILINE, ) for match in pattern.finditer(source): classes.append(ClassInfo( name=match.group(1), file_path=rel_path, line_start=source[:match.start()].count("\n") + 1, line_end=0, base_classes=[match.group(2)] if match.group(2) else [], )) return classes def _extract_ts_functions(self, source: str, rel_path: str) -> list[FunctionInfo]: import re functions = [] pattern = re.compile( r'(?:export\s+)?(?:async\s+)?function\s+(\w+)\s*\(([^)]*)\)', re.MULTILINE, ) for match in pattern.finditer(source): functions.append(FunctionInfo( name=match.group(1), file_path=rel_path, line_start=source[:match.start()].count("\n") + 1, args=[a.strip() for a in match.group(2).split(",") if a.strip()], )) return functions def _extract_ts_imports(self, source: str) -> list[ImportInfo]: import re imports = [] pattern = re.compile( r'import\s+(?:{([^}]+)}|(\w+))\s+from\s+[\'"]([^\'"]+)[\'"]', re.MULTILINE, ) for match in pattern.finditer(source): named = match.group(1) default = match.group(2) mod = match.group(3) if named: names = [n.strip().split(" as ")[0].strip() for n in named.split(",")] else: names = [default] if default else [] imports.append(ImportInfo(module=mod, names=names)) return imports def _analyze_generic(self, full_path: Path, rel_path: str) -> FileAnalysis: return FileAnalysis( path=rel_path, language=full_path.suffix.lstrip("."), line_count=len(full_path.read_text(encoding="utf-8", errors="ignore").splitlines()), ) def build_call_graph(self, analyses: list[FileAnalysis]) -> dict[str, list[str]]: graph = {} for analysis in analyses: for func in analysis.functions: key = f"{analysis.path}::{func.name}" graph[key] = [] for imp in analysis.imports: for name in imp.names: if name in func.docstring: graph[key].append(f"{imp.module}::{name}") return graph