99 lines
3.5 KiB
Python
99 lines
3.5 KiB
Python
|
|
#!/usr/bin/env python3
|
||
|
|
# -*- coding: utf-8 -*-
|
||
|
|
import re
|
||
|
|
|
||
|
|
def fix_multiline_strings(content):
|
||
|
|
"""修复JavaScript文件中的多行字符串问题"""
|
||
|
|
|
||
|
|
# 查找所有的 "overview":, "process":, "keyPoints": 字段
|
||
|
|
lines = content.split('\n')
|
||
|
|
fixed_lines = []
|
||
|
|
in_string = False
|
||
|
|
current_field = None
|
||
|
|
string_content = []
|
||
|
|
|
||
|
|
for i, line in enumerate(lines):
|
||
|
|
# 检查是否是字段开始
|
||
|
|
if not in_string:
|
||
|
|
if '"overview":' in line or '"process":' in line or '"keyPoints":' in line:
|
||
|
|
# 提取字段名和开始的内容
|
||
|
|
match = re.match(r'(\s*"(overview|process|keyPoints)":\s*")(.*)', line)
|
||
|
|
if match:
|
||
|
|
prefix = match.group(1)
|
||
|
|
field = match.group(2)
|
||
|
|
content = match.group(3)
|
||
|
|
|
||
|
|
# 检查是否在同一行结束
|
||
|
|
if content.endswith('",') or content.endswith('"'):
|
||
|
|
# 单行字符串,直接添加
|
||
|
|
fixed_lines.append(line)
|
||
|
|
else:
|
||
|
|
# 多行字符串开始
|
||
|
|
in_string = True
|
||
|
|
current_field = field
|
||
|
|
string_content = [content]
|
||
|
|
# 保存前缀,稍后使用
|
||
|
|
current_prefix = prefix
|
||
|
|
else:
|
||
|
|
fixed_lines.append(line)
|
||
|
|
else:
|
||
|
|
fixed_lines.append(line)
|
||
|
|
else:
|
||
|
|
# 在字符串内部
|
||
|
|
# 检查是否是字符串结束
|
||
|
|
if line.strip().endswith('",') or line.strip().endswith('"'):
|
||
|
|
# 字符串结束
|
||
|
|
# 提取结束部分的内容
|
||
|
|
end_content = line.strip()
|
||
|
|
if end_content.endswith('",'):
|
||
|
|
string_content.append(end_content[:-2])
|
||
|
|
ending = '",'
|
||
|
|
else:
|
||
|
|
string_content.append(end_content[:-1])
|
||
|
|
ending = '"'
|
||
|
|
|
||
|
|
# 合并所有内容并转义换行符
|
||
|
|
full_content = '\\n'.join(string_content)
|
||
|
|
# 转义其他特殊字符
|
||
|
|
full_content = full_content.replace('\\', '\\\\')
|
||
|
|
full_content = full_content.replace('"', '\\"')
|
||
|
|
|
||
|
|
# 创建修复后的行
|
||
|
|
fixed_line = current_prefix + full_content + ending
|
||
|
|
fixed_lines.append(fixed_line)
|
||
|
|
|
||
|
|
# 重置状态
|
||
|
|
in_string = False
|
||
|
|
current_field = None
|
||
|
|
string_content = []
|
||
|
|
else:
|
||
|
|
# 继续收集字符串内容
|
||
|
|
string_content.append(line)
|
||
|
|
|
||
|
|
return '\n'.join(fixed_lines)
|
||
|
|
|
||
|
|
def main():
|
||
|
|
# 读取文件
|
||
|
|
with open('src/mocks/projectLibraryMock.js', 'r', encoding='utf-8') as f:
|
||
|
|
content = f.read()
|
||
|
|
|
||
|
|
# 修复多行字符串
|
||
|
|
fixed_content = fix_multiline_strings(content)
|
||
|
|
|
||
|
|
# 保存修复后的文件
|
||
|
|
with open('src/mocks/projectLibraryMock.js', 'w', encoding='utf-8') as f:
|
||
|
|
f.write(fixed_content)
|
||
|
|
|
||
|
|
print("✅ 多行字符串已修复")
|
||
|
|
|
||
|
|
# 验证语法
|
||
|
|
import subprocess
|
||
|
|
result = subprocess.run(['node', '-c', 'src/mocks/projectLibraryMock.js'],
|
||
|
|
capture_output=True, text=True)
|
||
|
|
if result.returncode == 0:
|
||
|
|
print("✅ JavaScript语法检查通过")
|
||
|
|
else:
|
||
|
|
print(f"❌ JavaScript语法错误: {result.stderr}")
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
main()
|