#!/usr/bin/env python3 import os import re from pathlib import Path def find_files_with_duplicate_classnames(): """Find all files with duplicate className attributes""" files = [] for root, dirs, filenames in os.walk('.'): # Skip node_modules and other irrelevant directories dirs[:] = [d for d in dirs if not d.startswith('.') and d != 'node_modules'] for filename in filenames: if filename.endswith(('.tsx', '.ts')) and not filename.endswith('.d.ts'): filepath = os.path.join(root, filename) try: with open(filepath, 'r', encoding='utf-8') as f: content = f.read() # Look for duplicate className attributes if re.search(r'className="[^"]*"\s+className="', content) or re.search(r"className='[^']*'\s+className='", content): files.append(filepath) except: pass return files def fix_duplicate_classnames(content): """Fix duplicate className attributes by merging them""" # Pattern to match duplicate className attributes # pattern = r'className="([^"]*)"\s+className="([^"]*)"' def merge_classnames(match): first_class = match.group(1).strip() second_class = match.group(2).strip() # If they're the same, just keep one if first_class == second_class: return f'className="{first_class}"' # Split class names and merge them first_classes = set(first_class.split()) second_classes = set(second_class.split()) # Combine classes (second takes precedence for conflicting classes) merged_classes = first_classes | second_classes return f'className="{" ".join(sorted(merged_classes))}"' # Also handle cases with single quotes pattern_single = r"className='([^']*)'\s+className='([^']*)'" def merge_classnames_single(match): first_class = match.group(1).strip() second_class = match.group(2).strip() # If they're the same, just keep one if first_class == second_class: return f"className='{first_class}'" # Split class names and merge them first_classes = set(first_class.split()) second_classes = set(second_class.split()) # Combine classes (second takes precedence for conflicting classes) merged_classes = first_classes | second_classes return f"className=\"{' '.join(sorted(merged_classes))}\"" content = re.sub(pattern, merge_classnames, content) content = re.sub(pattern_single, merge_classnames_single, content) return content def fix_file(filepath): """Fix duplicate className attributes in a single file""" with open(filepath, 'r', encoding='utf-8') as f: content = f.read() original_content = content fixed_content = fix_duplicate_classnames(content) if fixed_content != original_content: with open(filepath, 'w', encoding='utf-8') as f: f.write(fixed_content) print(f'Fixed duplicate className attributes in: {filepath}') return True return False def main(): print('=== Finding and fixing duplicate className attributes ===') files = find_files_with_duplicate_classnames() if not files: print('No files with duplicate className attributes found.') return print(f'Found {len(files)} files with duplicate className attributes:') fixed_count = 0 for filepath in files: print(f' - {filepath}') if fix_file(filepath): fixed_count += 1 print(f'\n=== SUMMARY ===') print(f'Fixed duplicate className attributes in {fixed_count} out of {len(files)} files') if __name__ == '__main__': main()