Skip to content

Commit 66e1ab0

Browse files
committed
.
1 parent 1410c59 commit 66e1ab0

File tree

8 files changed

+266
-176
lines changed

8 files changed

+266
-176
lines changed

run_format_example.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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)

sqlalchemy_1.6_to_2.0/input_repo/database.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
from sqlalchemy.ext.declarative import declarative_base
44
from sqlalchemy.orm import sessionmaker
55

6-
SQLALCHEMY_DATABASE_URL = "postgresql://user:password@localhost/dbname" # Change to your database URL
6+
SQLALCHEMY_DATABASE_URL = (
7+
"postgresql://user:password@localhost/dbname" # Change to your database URL
8+
)
79

810
engine = create_engine(SQLALCHEMY_DATABASE_URL)
911
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)

sqlalchemy_1.6_to_2.0/input_repo/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from sqlalchemy.orm import relationship, backref
33
from database import Base
44

5+
56
class Publisher(Base):
67
__tablename__ = "publishers"
78

sqlalchemy_1.6_to_2.0/output_repo/database.py

Lines changed: 0 additions & 11 deletions
This file was deleted.

sqlalchemy_1.6_to_2.0/output_repo/main.py

Lines changed: 0 additions & 101 deletions
This file was deleted.

sqlalchemy_1.6_to_2.0/output_repo/models.py

Lines changed: 0 additions & 32 deletions
This file was deleted.

sqlalchemy_1.6_to_2.0/output_repo/schemas.py

Lines changed: 0 additions & 31 deletions
This file was deleted.

0 commit comments

Comments
 (0)