#!/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): stripped = line.strip() if stripped.startswith('import ') or (stripped.startswith('{') and 'from' in stripped): last_import_idx = i elif stripped.startswith('} from') or stripped.endswith(';'): # End of multi-line import last_import_idx = i 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 # Find the component function and its return statement # Look for "export function ComponentName()" or "function ComponentName()" func_pattern = r'(export\s+)?function\s+' + re.escape(component_name) + r'\([^)]*\)\s*\{' func_match = re.search(func_pattern, content) if not func_match: print(f" - WARNING: Could not find function {component_name}") return False # Find the return statement after the function start func_start = func_match.end() content_after_func = content[func_start:] # Find the main return (not inside an if/else) # Look for "return (" at the start of a line (with possible whitespace) return_pattern = r'(\n\s*)return\s*\(\s*\n' return_match = re.search(return_pattern, content_after_func) if not return_match: print(f" - WARNING: Could not find return pattern") return False # Get the indentation before return indent = return_match.group(1) # Insert after the return ( # The return_match gives us the position relative to content_after_func # We need to insert in the full content insert_pos = func_start + return_match.end() # Back up to the newline before the JSX starts # The return_match ends with \n, so the next line is the start of JSX # We need to add before that JSX # Find the first non-empty line after return ( remaining = content[insert_pos:] lines_after = remaining.split('\n') first_line = None for i, line in enumerate(lines_after): if line.strip(): first_line = line first_line_idx = i break if first_line is None: print(f" - WARNING: Could not find JSX after return") return False # Get the indentation of the first JSX line jsx_indent = '' for char in first_line: if char == ' ': jsx_indent += ' ' else: break # Insert before the first JSX line # We need to insert at: func_start + return_match.end() + position of first JSX line # Actually, let's just insert the DebugName tag right after return ( # and before the first " # Find where to insert (after the newline following return () # The return_match ends with \n, so we insert at insert_pos before_insert = content[:insert_pos] after_insert = content[insert_pos:] # Add the opening DebugName tag modified = before_insert + debug_name_open + '\n' + after_insert # Now find the closing ); for the return statement # We need to find the matching ) for the return ( # Find the position of the return ( in modified return_pos = modified.find('return (', func_start) if return_pos == -1: print(f" - WARNING: Could not find 'return ('") return False # Find matching closing paren paren_count = 0 in_string = False string_char = None i = return_pos + len('return (') while i < len(modified): char = modified[i] if in_string: if char == string_char and (i == 0 or modified[i-1] != '\\'): in_string = False i += 1 continue if char == '"' or char == "'": in_string = True string_char = char elif char == '(': paren_count += 1 elif char == ')': if paren_count == 0: # This is the matching closing paren # Check if followed by ; if i + 1 < len(modified) and modified[i+1] == ';': # Insert before this ) before_close = modified[:i] after_close = modified[i:] # Get indentation lines_before = before_close.split('\n') last_line = lines_before[-1] close_indent = '' for char in last_line: if char == ' ': close_indent += ' ' else: break closing_tag = f"\n{close_indent}" modified = before_close + closing_tag + after_close print(f" - Successfully wrapped {component_name} with DebugName") break else: # Just a normal paren pass else: paren_count -= 1 i += 1 # Write back with open(file_path, 'w') as f: f.write(modified) 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!")