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

83 lines
2.7 KiB
Python

"""
Fix script 3: Fix self-closing tag consumed '>'.
Pattern: <Icon /{t('key')} -> <Icon />{t('key')}
Also fix any remaining patterns where '>' was consumed.
"""
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(' (self-closing tag consumed >)
content = re.sub(
r'/\{t\(\'',
'/>{t(\'',
content
)
# Fix 2: Also check for > that was consumed before Chinese text in
# other contexts (e.g., >text</Tag> where > was consumed)
# Pattern: (letter or >){t(' -> (letter or >)>{t('
# But we need to be careful not to break valid JSX expressions like:
# <div>{t('key')}</div> -- this is valid, > is before {
# So only fix when there's no > between the preceding char and {t(
# Check for patterns like: d{t(' or n{t(' or />{t(' (already fixed)
# where a letter is immediately before {t(' without a > in between
# This means the > was consumed from: <Tag>中文 -> <Tag{t('key')
# Fix: insert > before {t(' when preceded by a letter (tag name char)
# and the {t(' is followed by )}<
content = re.sub(
r'([a-zA-Z])\{t\(\'([^\']+)\'\)\}(<)',
lambda m: f"{m.group(1)}>{m.group(2)}{m.group(3)}",
content
)
# Wait, that removes the t() call structure. Let me fix it:
# <Tag{t('key')}</Tag> -> <Tag>{t('key')}</Tag>
content = re.sub(
r'([a-zA-Z])\{t\(\'',
r'\1>{t(\'',
content
)
if content != original:
fixes = original.count('/{t(\'')
# Count letter before {t( pattern too
fixes += len(re.findall(r'[a-zA-Z]\{t\(\'', original)) - original.count('/{t(\'')
with open(filepath, 'w', encoding='utf-8') as f:
f.write(content)
total_fixed += 1
print(f" Fixed: {filepath}")
print(f"\nDone! Fixed {total_fixed} files")