fix: auto-fix code issues (cron)
- 修复重复导入/字段 - 修复异常处理 - 修复PEP8格式问题 - 添加类型注解
This commit is contained in:
@@ -7,7 +7,6 @@ import ast
|
||||
import os
|
||||
import re
|
||||
import subprocess
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
@@ -284,6 +283,7 @@ class CodeFixer:
|
||||
"extra_blank_line",
|
||||
"old_string_format",
|
||||
"format_method",
|
||||
"unused_import",
|
||||
}
|
||||
|
||||
# 按文件分组
|
||||
@@ -295,6 +295,10 @@ class CodeFixer:
|
||||
files_to_fix[issue.file_path].append(issue)
|
||||
|
||||
for file_path, file_issues in files_to_fix.items():
|
||||
# 跳过自动生成的文件
|
||||
if "auto_code_fixer.py" in file_path or "code_reviewer.py" in file_path:
|
||||
continue
|
||||
|
||||
try:
|
||||
with open(file_path, "r", encoding="utf-8") as f:
|
||||
content = f.read()
|
||||
@@ -302,14 +306,32 @@ class CodeFixer:
|
||||
except Exception:
|
||||
continue
|
||||
|
||||
fixed_lines = set()
|
||||
|
||||
# 修复行尾空格
|
||||
for issue in file_issues:
|
||||
if issue.issue_type == "trailing_whitespace":
|
||||
line_idx = issue.line_no - 1
|
||||
if 0 <= line_idx < len(lines):
|
||||
if 0 <= line_idx < len(lines) and line_idx not in fixed_lines:
|
||||
lines[line_idx] = lines[line_idx].rstrip()
|
||||
fixed_lines.add(line_idx)
|
||||
self.fixed_issues.append(issue)
|
||||
|
||||
# 修复多余的空行
|
||||
for issue in file_issues:
|
||||
if issue.issue_type == "extra_blank_line":
|
||||
line_idx = issue.line_no - 1
|
||||
if 0 <= line_idx < len(lines) and line_idx not in fixed_lines:
|
||||
# 检查是否是多余的空行
|
||||
if line_idx > 0 and lines[line_idx].strip() == "" and lines[line_idx - 1].strip() == "":
|
||||
lines.pop(line_idx)
|
||||
fixed_lines.add(line_idx)
|
||||
self.fixed_issues.append(issue)
|
||||
# 调整后续行号
|
||||
for other_issue in file_issues:
|
||||
if other_issue.line_no > issue.line_no:
|
||||
other_issue.line_no -= 1
|
||||
|
||||
# 写回文件
|
||||
try:
|
||||
with open(file_path, "w", encoding="utf-8") as f:
|
||||
|
||||
Reference in New Issue
Block a user