-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast_parser.py
27 lines (22 loc) · 965 Bytes
/
ast_parser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import ast
class ASTAnalyzer(ast.NodeVisitor):
def __init__(self):
self.findings = []
self.function_complexity = {}
def visit_FunctionDef(self, node):
# 计算圈复杂度
complexity = len(node.body)
self.function_complexity[node.name] = complexity
self.generic_visit(node)
def visit_Name(self, node):
# 检测未使用变量
if isinstance(node.ctx, ast.Store):
if not any(ref.id == node.id for ref in self.references):
self.findings.append(f"未使用变量: {node.id}")
def visit_Assign(self, node):
# 检测未使用变量
if isinstance(node.targets[0].ctx, ast.Store):
if not any(ref.id == node.targets[0].id for ref in self.references):
self.findings.append(f"未使用变量: {node.targets[0].id}")
def visit_Assert(self, node: ast.Assert) -> ast.Any:
return super().visit_Assert(node)