Files
ALL-teach_sys/frontend_食品/fix_mockdata_syntax.py

87 lines
3.4 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import re
def fix_mockdata_syntax():
"""修复mockData.js的语法错误"""
file_path = 'src/data/mockData.js'
try:
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
print("开始修复语法错误...")
# 找到第一个profileOverview定义的结束位置
# 查找模式第一个profileOverview后面跟着的};
first_profile_end = re.search(r'mockData\.profileOverview = \{[\s\S]*?\};', content)
if first_profile_end:
print(f"找到第一个profileOverview定义结束位置: {first_profile_end.end()}")
# 检查第一个profileOverview后面是否紧跟第二个profileOverview
after_first = content[first_profile_end.end():]
second_profile_start = re.search(r'// 添加Profile个人档案页面数据\s*mockData\.profileOverview', after_first)
if second_profile_start:
print("发现重复的profileOverview定义")
# 删除第一个profileOverview定义
content_before_first = content[:first_profile_end.start()]
content_after_first = content[first_profile_end.end():]
# 重新组合内容去掉第一个profileOverview
fixed_content = content_before_first + content_after_first
print("已删除重复的profileOverview定义")
else:
print("未发现重复定义,检查其他语法问题...")
fixed_content = content
else:
print("未找到profileOverview定义")
fixed_content = content
# 修复可能的多余逗号和括号问题
# 查找并修复 },\n },\n }; 这样的模式
fixed_content = re.sub(r'},\s*},\s*};', '};\n\n', fixed_content)
# 查找并修复 },\n },\n };\n 这样的模式
fixed_content = re.sub(r'},\s*}\s*};\s*\n\s*// 添加Profile', '};\n\n// 添加Profile', fixed_content)
# 保存修复后的文件
with open(file_path, 'w', encoding='utf-8') as f:
f.write(fixed_content)
print("语法错误修复完成!")
# 验证修复结果
try:
import subprocess
result = subprocess.run(['node', '-c', file_path], capture_output=True, text=True)
if result.returncode == 0:
print("✅ JavaScript语法检查通过!")
else:
print(f"❌ JavaScript语法仍有错误: {result.stderr}")
# 尝试更精确的修复
print("尝试更精确的修复...")
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
# 找到问题行附近的内容
lines = content.split('\n')
for i, line in enumerate(lines):
if i >= 1200 and i <= 1210:
print(f"{i+1}行: {line}")
except Exception as e:
print(f"语法检查时出错: {e}")
except Exception as e:
print(f"修复过程中出错: {e}")
if __name__ == "__main__":
fix_mockdata_syntax()