|
| 1 | +import codegen |
| 2 | +from codegen import Codebase |
| 3 | +from codegen.sdk.core.detached_symbols.function_call import FunctionCall |
| 4 | + |
| 5 | + |
| 6 | +@codegen.function("useSuspenseQuery-to-useSuspenseQueries") |
| 7 | +def run(codebase: Codebase): |
| 8 | + """Convert useSuspenseQuery calls to useSuspenseQueries in a React codebase. |
| 9 | +
|
| 10 | + This codemod: |
| 11 | + 1. Finds all files containing useSuspenseQuery |
| 12 | + 2. Adds the necessary import statement |
| 13 | + 3. Converts multiple useSuspenseQuery calls to a single useSuspenseQueries call |
| 14 | + """ |
| 15 | + # Import statement for useSuspenseQueries |
| 16 | + import_str = "import { useQuery, useSuspenseQueries } from '@tanstack/react-query'" |
| 17 | + |
| 18 | + # Track statistics |
| 19 | + files_modified = 0 |
| 20 | + functions_modified = 0 |
| 21 | + |
| 22 | + # Iterate through all files in the codebase |
| 23 | + for file in codebase.files: |
| 24 | + if "useSuspenseQuery" not in file.source: |
| 25 | + continue |
| 26 | + |
| 27 | + print(f"Processing {file.filepath}") |
| 28 | + # Add the import statement |
| 29 | + file.add_import_from_import_string(import_str) |
| 30 | + file_modified = False |
| 31 | + |
| 32 | + # Iterate through all functions in the file |
| 33 | + for function in file.functions: |
| 34 | + if "useSuspenseQuery" not in function.source: |
| 35 | + continue |
| 36 | + |
| 37 | + results = [] # Store left-hand side of assignments |
| 38 | + queries = [] # Store query arguments |
| 39 | + old_statements = [] # Track statements to replace |
| 40 | + |
| 41 | + # Find useSuspenseQuery assignments |
| 42 | + for stmt in function.code_block.assignment_statements: |
| 43 | + if not isinstance(stmt.right, FunctionCall): |
| 44 | + continue |
| 45 | + |
| 46 | + fcall = stmt.right |
| 47 | + if fcall.name != "useSuspenseQuery": |
| 48 | + continue |
| 49 | + |
| 50 | + old_statements.append(stmt) |
| 51 | + results.append(stmt.left.source) |
| 52 | + queries.append(fcall.args[0].value.source) |
| 53 | + |
| 54 | + # Convert to useSuspenseQueries if needed |
| 55 | + if old_statements: |
| 56 | + new_query = f"const [{', '.join(results)}] = useSuspenseQueries({{queries: [{', '.join(queries)}]}})" |
| 57 | + print( |
| 58 | + f"Converting useSuspenseQuery to useSuspenseQueries in {function.name}" |
| 59 | + ) |
| 60 | + |
| 61 | + # Print the diff |
| 62 | + print("\nOriginal code:") |
| 63 | + for stmt in old_statements: |
| 64 | + print(f"- {stmt.source}") |
| 65 | + print("\nNew code:") |
| 66 | + print(f"+ {new_query}") |
| 67 | + print("-" * 50) |
| 68 | + |
| 69 | + # Replace old statements with new query |
| 70 | + for stmt in old_statements: |
| 71 | + stmt.edit(new_query) |
| 72 | + |
| 73 | + functions_modified += 1 |
| 74 | + file_modified = True |
| 75 | + |
| 76 | + if file_modified: |
| 77 | + files_modified += 1 |
| 78 | + |
| 79 | + print("\nModification complete:") |
| 80 | + print(f"Files modified: {files_modified}") |
| 81 | + print(f"Functions modified: {functions_modified}") |
| 82 | + |
| 83 | + |
| 84 | +if __name__ == "__main__": |
| 85 | + print("Initializing codebase...") |
| 86 | + codebase = Codebase.from_repo("deepfence/ThreatMapper") |
| 87 | + |
| 88 | + print("Running codemod...") |
| 89 | + run(codebase) |
0 commit comments