Files
web-Iot/.workbuddy/fix_jsx_gt.py
2026-08-12 11:29:44 +08:00

64 lines
1.8 KiB
Python

"""
Fix script 2: Fix consumed '>' in JSX tags.
The batch script pattern `>Chinese<` -> `{t('key')}<` consumed the '>' from opening JSX tags.
"""
import re
import os
target_dirs = ['src/pages', 'src/components']
skip_files = [
'src/components/LanguageSwitcher.tsx',
'src/components/Layout.tsx',
'src/i18n/index.ts',
'src/locales/zh/index.ts',
'src/locales/en/index.ts',
]
def get_target_files():
files = []
for target_dir in target_dirs:
for root, dirs, filenames in os.walk(target_dir):
for fname in filenames:
if fname.endswith(('.tsx', '.ts', '.jsx', '.js')):
filepath = os.path.join(root, fname).replace('\\', '/')
if filepath not in skip_files:
files.append(filepath)
return files
target_files = get_target_files()
total_fixed = 0
for filepath in target_files:
try:
with open(filepath, 'r', encoding='utf-8') as f:
content = f.read()
except:
continue
original = content
# Fix 1: }{t(' -> }>{t(' (consumed > after JSX expression attribute)
# Example: <Option value={1}{t('key')}</Option> -> <Option value={1}>{t('key')}</Option>
content = re.sub(
r'\}\{t\(\'',
'}>{t(\'',
content
)
# Fix 2: "{t(' -> ">{t(' (consumed > after JSX string attribute)
# Example: <Option value="X"{t('key')}</Option> -> <Option value="X">{t('key')}</Option>
content = re.sub(
r'"\{t\(\'',
'">{t(\'',
content
)
if content != original:
fixes = original.count('}{t(\'') + original.count('"{t(\'')
with open(filepath, 'w', encoding='utf-8') as f:
f.write(content)
total_fixed += 1
print(f" Fixed: {filepath} ({fixes} issues)")
print(f"\nDone! Fixed {total_fixed} files")