Skip to content

Add codemod script to remove empty usePresenter parameters #1005

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions scripts/codemods/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Codegen Codemods

This directory contains codemod scripts that can be used to automate code transformations using the Codegen SDK.

## Available Codemods

### Remove Empty usePresenter Parameters

**Script**: `remove_empty_presenter_params.py`

This codemod finds all usages of `usePresenter` where the second argument is an empty object (`{}`) and removes that parameter.

#### Usage

```bash
# Navigate to your project root
cd your-project-root

# Run the script
python scripts/codemods/remove_empty_presenter_params.py
```

#### What it does

The script:

1. Searches for all function calls to `usePresenter` in your codebase
1. Identifies calls where the second argument is an empty object (`{}`)
1. Removes the empty object parameter
1. Commits the changes to your files

#### Example Transformation

Before:

```javascript
const presenter = usePresenter(someValue, {});
```

After:

```javascript
const presenter = usePresenter(someValue);
```

#### Configuration

By default, the script is configured for TypeScript/JavaScript codebases. If your project uses a different language, you can modify the language parameter in the script:

```python
# Change "typescript" to your project's language
codebase = Codebase("./", language="typescript")
```
64 changes: 64 additions & 0 deletions scripts/codemods/remove_empty_presenter_params.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import re

import codegen
from codegen import Codebase


@codegen.function("remove_empty_presenter_params")
def run(codebase: Codebase):
"""Find all usages of 'usePresenter' where the second argument is '{}' and remove that parameter.

This script handles various forms of empty objects:
- {}
- { }
- Object with whitespace
"""
print("🔍 Searching for usePresenter calls with empty object as second parameter...")

modified_files = 0
modified_calls = 0

# Regular expression to match empty objects with possible whitespace
empty_obj_pattern = re.compile(r"^\s*{\s*}\s*$")

# Iterate through all files in the codebase
for file in codebase.files:

Check failure on line 25 in scripts/codemods/remove_empty_presenter_params.py

View workflow job for this annotation

GitHub Actions / mypy

error: overloaded function has no attribute "__iter__" (not iterable) [attr-defined]
file_modified = False

# Look for function calls to usePresenter
for call in file.function_calls:
if call.name == "usePresenter" and len(call.arguments) >= 2:
# Check if the second argument is an empty object {}
second_arg = call.arguments[1]
if empty_obj_pattern.match(second_arg.source_code):
print(f"🔧 Found usePresenter with empty object in {file.filepath}")
print(f" Original: {call.source_code}")

# Remove the second argument
call.remove_argument(1)
file_modified = True
modified_calls += 1

print(f" Modified: {call.source_code}")

# Commit changes if the file was modified
if file_modified:
modified_files += 1

# Commit all changes
if modified_files > 0:
codebase.commit()
print(f"✅ Modified {modified_calls} usePresenter calls across {modified_files} files")
else:
print("ℹ️ No usePresenter calls with empty object parameters found")


if __name__ == "__main__":
print("🚀 Starting script to remove empty usePresenter parameters...")

# Initialize codebase with the appropriate language
# You can change this to match your project's primary language
codebase = Codebase("./", language="typescript")

run(codebase)
print("✅ Done! All empty usePresenter parameters have been removed!")
Loading