#!/usr/bin/env python3 import re def fix_tab_file(file_path, component_name): """Add DebugName import and wrap the main return with DebugName""" print(f"Processing {file_path} for {component_name}...") with open(file_path, 'r') as f: content = f.read() # Check if DebugName is already imported if "from '@/lib/game/debug-context'" in content: print(f" - DebugName already imported") else: # Find the last import line and add the DebugName import after it lines = content.split('\n') last_import_idx = -1 for i, line in enumerate(lines): if line.startswith('import ') or line.strip().startswith('import {'): last_import_idx = i if last_import_idx >= 0: # Check if next line is also part of import if i + 1 < len(lines) and (lines[i+1].strip().startswith('} from') or lines[i+1].strip() == '}'): # Multi-line import, find the closing for j in range(i+1, len(lines)): if '} from' in lines[j]: last_import_idx = j break if last_import_idx >= 0: lines.insert(last_import_idx + 1, "import { DebugName } from '@/lib/game/debug-context';") content = '\n'.join(lines) print(f" - Added DebugName import") else: print(f" - WARNING: No import found") return False # Now find the main return statement (not early returns) # Look for "return (" followed by newline and then some JSX pattern = r'(export function \w+\(\)\s*\{.*?return\s*\(\s*\n)(\s*)(<)' match = re.search(pattern, content, re.DOTALL) if not match: print(f" - WARNING: Could not find main return pattern") return False # Get the indentation indent = match.group(2) # Add opening DebugName tag after the return ( before_return = content[:match.end(1)] after_return = content[match.end(1):] # Add after the return ( modified = before_return + f'{indent}\n{indent}{after_return[0]}' # Now find the closing ); for the main return # Find "displayName" to locate the end of the component display_pattern = re.escape(component_name) + r'\.displayName\s*=\s*[\'"].*?[\'"];\s*\n' display_match = re.search(display_pattern, modified) if not display_match: print(f" - WARNING: Could not find displayName") return False # Find the ); before displayName before_display = modified[:display_match.start()] after_display = modified[display_match.start():] # Find the last ); in before_display that's at the start of a line lines_before = before_display.split('\n') close_paren_line = -1 close_paren_indent = '' for i in range(len(lines_before) - 1, -1, -1): line = lines_before[i] if line.strip() == ');': close_paren_line = i close_paren_indent = line[:len(line) - len(line.lstrip())] break if close_paren_line == -1: print(f" - WARNING: Could not find closing );\") return False # Insert before ); lines_before.insert(close_paren_line, f"{close_paren_indent}") before_display_fixed = '\n'.join(lines_before) modified = before_display_fixed + after_display # Write back with open(file_path, 'w') as f: f.write(modified) print(f" - Successfully wrapped {component_name} with DebugName") return True # Fix the remaining files fix_tab_file('src/components/game/tabs/GolemancyTab.tsx', 'GolemancyTab') fix_tab_file('src/components/game/tabs/SpellsTab.tsx', 'SpellsTab') print("\nDone!")