From e3830a7ce723b114917b3f3e5959cf2da7db38db Mon Sep 17 00:00:00 2001 From: "codegen-sh[bot]" <131295404+codegen-sh[bot]@users.noreply.github.com> Date: Wed, 2 Apr 2025 22:09:34 +0000 Subject: [PATCH 1/2] Add codemod script to remove empty usePresenter parameters --- scripts/codemods/README.md | 50 +++++++++++++++ .../codemods/remove_empty_presenter_params.py | 64 +++++++++++++++++++ 2 files changed, 114 insertions(+) create mode 100644 scripts/codemods/README.md create mode 100644 scripts/codemods/remove_empty_presenter_params.py diff --git a/scripts/codemods/README.md b/scripts/codemods/README.md new file mode 100644 index 000000000..722186da9 --- /dev/null +++ b/scripts/codemods/README.md @@ -0,0 +1,50 @@ +# 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 +2. Identifies calls where the second argument is an empty object (`{}`) +3. Removes the empty object parameter +4. 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") +``` \ No newline at end of file diff --git a/scripts/codemods/remove_empty_presenter_params.py b/scripts/codemods/remove_empty_presenter_params.py new file mode 100644 index 000000000..52047be90 --- /dev/null +++ b/scripts/codemods/remove_empty_presenter_params.py @@ -0,0 +1,64 @@ +import codegen +from codegen import Codebase +import re + + +@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: + 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!") \ No newline at end of file From e2c33d0d876fe43550b9d65bad69da9a6d5d4009 Mon Sep 17 00:00:00 2001 From: "codegen-sh[bot]" <131295404+codegen-sh[bot]@users.noreply.github.com> Date: Wed, 2 Apr 2025 22:10:25 +0000 Subject: [PATCH 2/2] Automated pre-commit update --- scripts/codemods/README.md | 11 ++++--- .../codemods/remove_empty_presenter_params.py | 32 +++++++++---------- 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/scripts/codemods/README.md b/scripts/codemods/README.md index 722186da9..64ced71bf 100644 --- a/scripts/codemods/README.md +++ b/scripts/codemods/README.md @@ -23,19 +23,22 @@ python scripts/codemods/remove_empty_presenter_params.py #### What it does The script: + 1. Searches for all function calls to `usePresenter` in your codebase -2. Identifies calls where the second argument is an empty object (`{}`) -3. Removes the empty object parameter -4. Commits the changes to your files +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); ``` @@ -47,4 +50,4 @@ By default, the script is configured for TypeScript/JavaScript codebases. If you ```python # Change "typescript" to your project's language codebase = Codebase("./", language="typescript") -``` \ No newline at end of file +``` diff --git a/scripts/codemods/remove_empty_presenter_params.py b/scripts/codemods/remove_empty_presenter_params.py index 52047be90..866872418 100644 --- a/scripts/codemods/remove_empty_presenter_params.py +++ b/scripts/codemods/remove_empty_presenter_params.py @@ -1,30 +1,30 @@ +import re + import codegen from codegen import Codebase -import re @codegen.function("remove_empty_presenter_params") def run(codebase: Codebase): - """ - Find all usages of 'usePresenter' where the second argument is '{}' and remove that parameter. - + """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*$') - + empty_obj_pattern = re.compile(r"^\s*{\s*}\s*$") + # Iterate through all files in the codebase for file in codebase.files: file_modified = False - + # Look for function calls to usePresenter for call in file.function_calls: if call.name == "usePresenter" and len(call.arguments) >= 2: @@ -33,18 +33,18 @@ def run(codebase: Codebase): 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() @@ -55,10 +55,10 @@ def run(codebase: Codebase): 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!") \ No newline at end of file + print("✅ Done! All empty usePresenter parameters have been removed!")