From 414d9fbde4e206ad22bb9ee4d70e4d4b4dbcce37 Mon Sep 17 00:00:00 2001 From: KopekC Date: Thu, 30 Jan 2025 23:47:15 -0500 Subject: [PATCH 01/20] . --- .../analize_reexports/input_repo/package.json | 15 ++ .../input_repo/tsconfig.json | 9 ++ examples/analize_reexports/run.py | 136 ++++++++++++++++++ 3 files changed, 160 insertions(+) create mode 100644 examples/analize_reexports/input_repo/package.json create mode 100644 examples/analize_reexports/input_repo/tsconfig.json create mode 100644 examples/analize_reexports/run.py diff --git a/examples/analize_reexports/input_repo/package.json b/examples/analize_reexports/input_repo/package.json new file mode 100644 index 0000000..ce1ed32 --- /dev/null +++ b/examples/analize_reexports/input_repo/package.json @@ -0,0 +1,15 @@ +{ + "name": "default-exports-test", + "version": "1.0.0", + "description": "Test codebase for converting default exports", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC", + "devDependencies": { + "typescript": "^5.0.0" + } +} diff --git a/examples/analize_reexports/input_repo/tsconfig.json b/examples/analize_reexports/input_repo/tsconfig.json new file mode 100644 index 0000000..531309f --- /dev/null +++ b/examples/analize_reexports/input_repo/tsconfig.json @@ -0,0 +1,9 @@ +{ + "compilerOptions": { + "baseUrl": "./", + "paths": { + "*": ["src/*"] + } + }, + "include": ["src/**/*"] +} \ No newline at end of file diff --git a/examples/analize_reexports/run.py b/examples/analize_reexports/run.py new file mode 100644 index 0000000..795f5d0 --- /dev/null +++ b/examples/analize_reexports/run.py @@ -0,0 +1,136 @@ +from codegen import Codebase +from codegen.sdk.typescript.file import TSFile, TSImport +from codegen.sdk.enums import ProgrammingLanguage + +processed_imports = set() + +def run(codebase: Codebase): + print(len(codebase.files)) + for file in codebase.files: + + # Only process files under /src/shared + if "examples/analize_reexports" not in file.filepath: + continue + if '/src/shared' not in file.filepath: + print("not in shared") + print(file.filepath) + continue + + # Gather all reexports that are not external exports + all_reexports = [] + for export_stmt in file.export_statements: + for export in export_stmt.exports: + if export.is_reexport() and not export.is_external_export: + all_reexports.append(export) + + # Skip if there are none + if not all_reexports: + continue + + for export in all_reexports: + has_wildcard = False + + # Replace "src/" with "src/shared/" + resolved_public_file = export.resolved_symbol.filepath.replace("src/", "src/shared/") + + # Get relative path from the "public" file back to the original file + relative_path = codebase.get_relative_path( + from_file=resolved_public_file, + to_file=export.resolved_symbol.filepath + ) + + # Ensure the "public" file exists + if not codebase.has_file(resolved_public_file): + target_file = codebase.create_file(resolved_public_file, sync=True) + else: + target_file = codebase.get_file(resolved_public_file) + + # If target file already has a wildcard export for this relative path, skip + if target_file.has_export_statement_for_path(relative_path, "WILDCARD"): + has_wildcard = True + continue + + # Compare "public" path to the local file's export.filepath + if codebase._remove_extension(resolved_public_file) != codebase._remove_extension(export.filepath): + + # A) Wildcard export, e.g. `export * from "..."` + if export.is_wildcard_export(): + target_file.insert_before(f'export * from "{relative_path}"') + + # B) Type export, e.g. `export type { Foo, Bar } from "..."` + elif export.is_type_export(): + # Does this file already have a type export statement for the path? + statement = file.get_export_statement_for_path(relative_path, "TYPE") + if statement: + # Insert into existing statement + if export.is_aliased(): + statement.insert(0, f"{export.resolved_symbol.name} as {export.name}") + else: + statement.insert(0, f"{export.name}") + else: + # Insert a new type export statement + if export.is_aliased(): + target_file.insert_before( + f'export type {{ {export.resolved_symbol.name} as {export.name} }} ' + f'from "{relative_path}"' + ) + else: + target_file.insert_before( + f'export type {{ {export.name} }} from "{relative_path}"' + ) + + # C) Normal export, e.g. `export { Foo, Bar } from "..."` + else: + statement = file.get_export_statement_for_path(relative_path, "EXPORT") + if statement: + # Insert into existing statement + if export.is_aliased(): + statement.insert(0, f"{export.resolved_symbol.name} as {export.name}") + else: + statement.insert(0, f"{export.name}") + else: + # Insert a brand-new normal export statement + if export.is_aliased(): + target_file.insert_before( + f'export {{ {export.resolved_symbol.name} as {export.name} }} ' + f'from "{relative_path}"' + ) + else: + target_file.insert_before( + f'export {{ {export.name} }} from "{relative_path}"' + ) + + # Now update all import usages that refer to this export + for usage in export.symbol_usages(): + if isinstance(usage, TSImport) and usage not in processed_imports: + processed_imports.add(usage) + + # Translate the resolved_public_file to the usage file's TS config import path + new_path = usage.file.ts_config.translate_import_path(resolved_public_file) + + if has_wildcard and export.name != export.resolved_symbol.name: + name = f"{export.resolved_symbol.name} as {export.name}" + else: + name = usage.name + + if usage.is_type_import(): + new_import = f'import type {{ {name} }} from "{new_path}"' + else: + new_import = f'import {{ {name} }} from "{new_path}"' + + usage.file.insert_before(new_import) + usage.remove() + + # Remove the old export from the original file + export.remove() + + # If the file ends up with no exports, remove it entirely + if not file.export_statements and len(file.symbols) == 0: + file.remove() + + +if __name__ == "__main__": + print("Starting...") + codebase = Codebase("./", programming_language=ProgrammingLanguage.TYPESCRIPT) + run(codebase) + print("Done!") From 51d109ff55e0d84a46dae1291b53c45ab713bf18 Mon Sep 17 00:00:00 2001 From: KopekC Date: Thu, 30 Jan 2025 23:47:58 -0500 Subject: [PATCH 02/20] . --- examples/analize_reexports/input_repo/src/moduleA/foo.ts | 5 +++++ .../analize_reexports/input_repo/src/moduleA/shared/index.ts | 1 + examples/analize_reexports/input_repo/src/moduleA/utils.ts | 3 +++ examples/analize_reexports/input_repo/src/moduleB/bar.ts | 3 +++ .../analize_reexports/input_repo/src/moduleB/shared/index.ts | 2 ++ 5 files changed, 14 insertions(+) create mode 100644 examples/analize_reexports/input_repo/src/moduleA/foo.ts create mode 100644 examples/analize_reexports/input_repo/src/moduleA/shared/index.ts create mode 100644 examples/analize_reexports/input_repo/src/moduleA/utils.ts create mode 100644 examples/analize_reexports/input_repo/src/moduleB/bar.ts create mode 100644 examples/analize_reexports/input_repo/src/moduleB/shared/index.ts diff --git a/examples/analize_reexports/input_repo/src/moduleA/foo.ts b/examples/analize_reexports/input_repo/src/moduleA/foo.ts new file mode 100644 index 0000000..6174ec3 --- /dev/null +++ b/examples/analize_reexports/input_repo/src/moduleA/foo.ts @@ -0,0 +1,5 @@ +export class Foo { + constructor() { + console.log('Foo class from Module A'); + } +} \ No newline at end of file diff --git a/examples/analize_reexports/input_repo/src/moduleA/shared/index.ts b/examples/analize_reexports/input_repo/src/moduleA/shared/index.ts new file mode 100644 index 0000000..1d0353f --- /dev/null +++ b/examples/analize_reexports/input_repo/src/moduleA/shared/index.ts @@ -0,0 +1 @@ +export { barFunction } from '../../moduleB/bar'; \ No newline at end of file diff --git a/examples/analize_reexports/input_repo/src/moduleA/utils.ts b/examples/analize_reexports/input_repo/src/moduleA/utils.ts new file mode 100644 index 0000000..1f85a39 --- /dev/null +++ b/examples/analize_reexports/input_repo/src/moduleA/utils.ts @@ -0,0 +1,3 @@ +export const utilFunctionA = () => { + console.log('Utility function A'); +}; \ No newline at end of file diff --git a/examples/analize_reexports/input_repo/src/moduleB/bar.ts b/examples/analize_reexports/input_repo/src/moduleB/bar.ts new file mode 100644 index 0000000..ce52446 --- /dev/null +++ b/examples/analize_reexports/input_repo/src/moduleB/bar.ts @@ -0,0 +1,3 @@ +export const barFunction = () => { + console.log('Bar function from Module B'); +}; \ No newline at end of file diff --git a/examples/analize_reexports/input_repo/src/moduleB/shared/index.ts b/examples/analize_reexports/input_repo/src/moduleB/shared/index.ts new file mode 100644 index 0000000..47b1d8e --- /dev/null +++ b/examples/analize_reexports/input_repo/src/moduleB/shared/index.ts @@ -0,0 +1,2 @@ +export { Foo } from '../../moduleA/foo'; +export { utilFunctionA } from '../../moduleA/utils'; \ No newline at end of file From dddde6233f48adc9f22617f798ec11295711ce0a Mon Sep 17 00:00:00 2001 From: KopekC Date: Thu, 30 Jan 2025 23:49:45 -0500 Subject: [PATCH 03/20] . --- examples/analize_reexports/input_repo/src/moduleA/reexports.ts | 3 +++ examples/analize_reexports/input_repo/src/moduleB/reexports.ts | 3 +++ 2 files changed, 6 insertions(+) create mode 100644 examples/analize_reexports/input_repo/src/moduleA/reexports.ts create mode 100644 examples/analize_reexports/input_repo/src/moduleB/reexports.ts diff --git a/examples/analize_reexports/input_repo/src/moduleA/reexports.ts b/examples/analize_reexports/input_repo/src/moduleA/reexports.ts new file mode 100644 index 0000000..f9b9824 --- /dev/null +++ b/examples/analize_reexports/input_repo/src/moduleA/reexports.ts @@ -0,0 +1,3 @@ +import { barFunction } from './shared'; + +export { barFunction }; \ No newline at end of file diff --git a/examples/analize_reexports/input_repo/src/moduleB/reexports.ts b/examples/analize_reexports/input_repo/src/moduleB/reexports.ts new file mode 100644 index 0000000..f4dff0b --- /dev/null +++ b/examples/analize_reexports/input_repo/src/moduleB/reexports.ts @@ -0,0 +1,3 @@ +import { Foo, utilFunctionA } from './shared'; + +export { Foo, utilFunctionA }; \ No newline at end of file From 3ce687317a3451cf42c1f7fe5a52f6932f0a33b8 Mon Sep 17 00:00:00 2001 From: KopekC Date: Thu, 30 Jan 2025 23:52:03 -0500 Subject: [PATCH 04/20] . --- .../analize_reexports/input_repo/src/moduleA/reexports.ts | 2 +- .../input_repo/src/moduleA/shared/index.ts | 1 - .../input_repo/src/moduleA/src/shared/index.ts | 1 + .../analize_reexports/input_repo/src/moduleB/reexports.ts | 2 +- .../input_repo/src/moduleB/shared/index.ts | 2 -- .../input_repo/src/moduleB/src/shared/index.ts | 2 ++ examples/analize_reexports/run.py | 7 +++---- 7 files changed, 8 insertions(+), 9 deletions(-) delete mode 100644 examples/analize_reexports/input_repo/src/moduleA/shared/index.ts create mode 100644 examples/analize_reexports/input_repo/src/moduleA/src/shared/index.ts delete mode 100644 examples/analize_reexports/input_repo/src/moduleB/shared/index.ts create mode 100644 examples/analize_reexports/input_repo/src/moduleB/src/shared/index.ts diff --git a/examples/analize_reexports/input_repo/src/moduleA/reexports.ts b/examples/analize_reexports/input_repo/src/moduleA/reexports.ts index f9b9824..c4c2555 100644 --- a/examples/analize_reexports/input_repo/src/moduleA/reexports.ts +++ b/examples/analize_reexports/input_repo/src/moduleA/reexports.ts @@ -1,3 +1,3 @@ -import { barFunction } from './shared'; +import { barFunction } from './src/shared'; export { barFunction }; \ No newline at end of file diff --git a/examples/analize_reexports/input_repo/src/moduleA/shared/index.ts b/examples/analize_reexports/input_repo/src/moduleA/shared/index.ts deleted file mode 100644 index 1d0353f..0000000 --- a/examples/analize_reexports/input_repo/src/moduleA/shared/index.ts +++ /dev/null @@ -1 +0,0 @@ -export { barFunction } from '../../moduleB/bar'; \ No newline at end of file diff --git a/examples/analize_reexports/input_repo/src/moduleA/src/shared/index.ts b/examples/analize_reexports/input_repo/src/moduleA/src/shared/index.ts new file mode 100644 index 0000000..a809d69 --- /dev/null +++ b/examples/analize_reexports/input_repo/src/moduleA/src/shared/index.ts @@ -0,0 +1 @@ +export { barFunction } from '../../../moduleB/bar'; \ No newline at end of file diff --git a/examples/analize_reexports/input_repo/src/moduleB/reexports.ts b/examples/analize_reexports/input_repo/src/moduleB/reexports.ts index f4dff0b..ed9615c 100644 --- a/examples/analize_reexports/input_repo/src/moduleB/reexports.ts +++ b/examples/analize_reexports/input_repo/src/moduleB/reexports.ts @@ -1,3 +1,3 @@ -import { Foo, utilFunctionA } from './shared'; +import { Foo, utilFunctionA } from './src/shared'; export { Foo, utilFunctionA }; \ No newline at end of file diff --git a/examples/analize_reexports/input_repo/src/moduleB/shared/index.ts b/examples/analize_reexports/input_repo/src/moduleB/shared/index.ts deleted file mode 100644 index 47b1d8e..0000000 --- a/examples/analize_reexports/input_repo/src/moduleB/shared/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -export { Foo } from '../../moduleA/foo'; -export { utilFunctionA } from '../../moduleA/utils'; \ No newline at end of file diff --git a/examples/analize_reexports/input_repo/src/moduleB/src/shared/index.ts b/examples/analize_reexports/input_repo/src/moduleB/src/shared/index.ts new file mode 100644 index 0000000..ae3a362 --- /dev/null +++ b/examples/analize_reexports/input_repo/src/moduleB/src/shared/index.ts @@ -0,0 +1,2 @@ +export { Foo } from '../../../moduleA/foo'; +export { utilFunctionA } from '../../../moduleA/utils'; \ No newline at end of file diff --git a/examples/analize_reexports/run.py b/examples/analize_reexports/run.py index 795f5d0..95d5641 100644 --- a/examples/analize_reexports/run.py +++ b/examples/analize_reexports/run.py @@ -5,17 +5,14 @@ processed_imports = set() def run(codebase: Codebase): - print(len(codebase.files)) for file in codebase.files: # Only process files under /src/shared if "examples/analize_reexports" not in file.filepath: continue if '/src/shared' not in file.filepath: - print("not in shared") - print(file.filepath) continue - + # Gather all reexports that are not external exports all_reexports = [] for export_stmt in file.export_statements: @@ -23,6 +20,8 @@ def run(codebase: Codebase): if export.is_reexport() and not export.is_external_export: all_reexports.append(export) + print(f"all_reexports {len(all_reexports)}") + # Skip if there are none if not all_reexports: continue From 73f1b31a21e6e4aee8af16ae7c46a584ef578591 Mon Sep 17 00:00:00 2001 From: KopekC Date: Thu, 30 Jan 2025 23:53:33 -0500 Subject: [PATCH 05/20] . --- examples/analize_reexports/run.py | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/examples/analize_reexports/run.py b/examples/analize_reexports/run.py index 95d5641..40967ef 100644 --- a/examples/analize_reexports/run.py +++ b/examples/analize_reexports/run.py @@ -5,12 +5,16 @@ processed_imports = set() def run(codebase: Codebase): + print("Processing files in the codebase...") for file in codebase.files: + print(f"Checking file: {file.filepath}") # Only process files under /src/shared if "examples/analize_reexports" not in file.filepath: + print("Skipping file not in the target directory.") continue if '/src/shared' not in file.filepath: + print("Skipping file not in /src/shared.") continue # Gather all reexports that are not external exports @@ -20,10 +24,11 @@ def run(codebase: Codebase): if export.is_reexport() and not export.is_external_export: all_reexports.append(export) - print(f"all_reexports {len(all_reexports)}") + print(f"Found {len(all_reexports)} reexports in {file.filepath}") # Skip if there are none if not all_reexports: + print("No reexports found, moving to the next file.") continue for export in all_reexports: @@ -31,43 +36,48 @@ def run(codebase: Codebase): # Replace "src/" with "src/shared/" resolved_public_file = export.resolved_symbol.filepath.replace("src/", "src/shared/") + print(f"Resolved public file path: {resolved_public_file}") # Get relative path from the "public" file back to the original file relative_path = codebase.get_relative_path( from_file=resolved_public_file, to_file=export.resolved_symbol.filepath ) + print(f"Relative path: {relative_path}") # Ensure the "public" file exists if not codebase.has_file(resolved_public_file): + print(f"Creating new file: {resolved_public_file}") target_file = codebase.create_file(resolved_public_file, sync=True) else: + print(f"File already exists: {resolved_public_file}") target_file = codebase.get_file(resolved_public_file) # If target file already has a wildcard export for this relative path, skip if target_file.has_export_statement_for_path(relative_path, "WILDCARD"): has_wildcard = True + print("Wildcard export already exists, skipping.") continue # Compare "public" path to the local file's export.filepath if codebase._remove_extension(resolved_public_file) != codebase._remove_extension(export.filepath): + print("Processing export...") # A) Wildcard export, e.g. `export * from "..."` if export.is_wildcard_export(): target_file.insert_before(f'export * from "{relative_path}"') + print(f"Inserted wildcard export for {relative_path}") # B) Type export, e.g. `export type { Foo, Bar } from "..."` elif export.is_type_export(): - # Does this file already have a type export statement for the path? statement = file.get_export_statement_for_path(relative_path, "TYPE") if statement: - # Insert into existing statement if export.is_aliased(): statement.insert(0, f"{export.resolved_symbol.name} as {export.name}") else: statement.insert(0, f"{export.name}") + print(f"Inserted into existing type export statement for {relative_path}") else: - # Insert a new type export statement if export.is_aliased(): target_file.insert_before( f'export type {{ {export.resolved_symbol.name} as {export.name} }} ' @@ -77,18 +87,18 @@ def run(codebase: Codebase): target_file.insert_before( f'export type {{ {export.name} }} from "{relative_path}"' ) + print(f"Inserted new type export statement for {relative_path}") # C) Normal export, e.g. `export { Foo, Bar } from "..."` else: statement = file.get_export_statement_for_path(relative_path, "EXPORT") if statement: - # Insert into existing statement if export.is_aliased(): statement.insert(0, f"{export.resolved_symbol.name} as {export.name}") else: statement.insert(0, f"{export.name}") + print(f"Inserted into existing export statement for {relative_path}") else: - # Insert a brand-new normal export statement if export.is_aliased(): target_file.insert_before( f'export {{ {export.resolved_symbol.name} as {export.name} }} ' @@ -98,6 +108,7 @@ def run(codebase: Codebase): target_file.insert_before( f'export {{ {export.name} }} from "{relative_path}"' ) + print(f"Inserted new export statement for {relative_path}") # Now update all import usages that refer to this export for usage in export.symbol_usages(): @@ -119,13 +130,16 @@ def run(codebase: Codebase): usage.file.insert_before(new_import) usage.remove() + print(f"Updated import in {usage.file.filepath}") # Remove the old export from the original file export.remove() + print(f"Removed old export from {export.filepath}") # If the file ends up with no exports, remove it entirely if not file.export_statements and len(file.symbols) == 0: file.remove() + print(f"Removed empty file: {file.filepath}") if __name__ == "__main__": From 89dc903ae6d10b8be5efad3e710cb9619e3fc916 Mon Sep 17 00:00:00 2001 From: KopekC Date: Thu, 30 Jan 2025 23:54:53 -0500 Subject: [PATCH 06/20] . --- examples/analize_reexports/run.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/analize_reexports/run.py b/examples/analize_reexports/run.py index 40967ef..f828af7 100644 --- a/examples/analize_reexports/run.py +++ b/examples/analize_reexports/run.py @@ -140,7 +140,7 @@ def run(codebase: Codebase): if not file.export_statements and len(file.symbols) == 0: file.remove() print(f"Removed empty file: {file.filepath}") - + codebase.commit() if __name__ == "__main__": print("Starting...") From 14718b39a9b44c4d678202c115d4967e8303e52d Mon Sep 17 00:00:00 2001 From: KopekC Date: Thu, 30 Jan 2025 23:56:19 -0500 Subject: [PATCH 07/20] . --- examples/analize_reexports/run.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/analize_reexports/run.py b/examples/analize_reexports/run.py index f828af7..bece14a 100644 --- a/examples/analize_reexports/run.py +++ b/examples/analize_reexports/run.py @@ -140,7 +140,7 @@ def run(codebase: Codebase): if not file.export_statements and len(file.symbols) == 0: file.remove() print(f"Removed empty file: {file.filepath}") - codebase.commit() + codebase.commit() if __name__ == "__main__": print("Starting...") From 08a37f43abb406cf07b1ea97a269e130112fc4fd Mon Sep 17 00:00:00 2001 From: KopekC Date: Fri, 31 Jan 2025 00:00:53 -0500 Subject: [PATCH 08/20] . --- .../analize_reexports/input_repo/src/moduleA/reexports.ts | 4 +++- .../input_repo/src/moduleA/src/shared/index.ts | 2 +- examples/analize_reexports/input_repo/src/moduleA/usage.ts | 6 ++++++ .../analize_reexports/input_repo/src/moduleB/reexports.ts | 3 ++- .../input_repo/src/moduleB/src/shared/index.ts | 3 +-- examples/analize_reexports/input_repo/src/moduleB/usage.ts | 7 +++++++ .../input_repo/src/shared/moduleA/utils.ts | 1 + 7 files changed, 21 insertions(+), 5 deletions(-) create mode 100644 examples/analize_reexports/input_repo/src/moduleA/usage.ts create mode 100644 examples/analize_reexports/input_repo/src/moduleB/usage.ts create mode 100644 examples/analize_reexports/input_repo/src/shared/moduleA/utils.ts diff --git a/examples/analize_reexports/input_repo/src/moduleA/reexports.ts b/examples/analize_reexports/input_repo/src/moduleA/reexports.ts index c4c2555..a49320b 100644 --- a/examples/analize_reexports/input_repo/src/moduleA/reexports.ts +++ b/examples/analize_reexports/input_repo/src/moduleA/reexports.ts @@ -1,3 +1,5 @@ import { barFunction } from './src/shared'; +import { Foo } from './foo'; +import { utilFunctionA } from './utils'; -export { barFunction }; \ No newline at end of file +export { barFunction, Foo, utilFunctionA }; \ No newline at end of file diff --git a/examples/analize_reexports/input_repo/src/moduleA/src/shared/index.ts b/examples/analize_reexports/input_repo/src/moduleA/src/shared/index.ts index a809d69..0f6db73 100644 --- a/examples/analize_reexports/input_repo/src/moduleA/src/shared/index.ts +++ b/examples/analize_reexports/input_repo/src/moduleA/src/shared/index.ts @@ -1 +1 @@ -export { barFunction } from '../../../moduleB/bar'; \ No newline at end of file +export { barFunction } from '../../../moduleB/reexports'; \ No newline at end of file diff --git a/examples/analize_reexports/input_repo/src/moduleA/usage.ts b/examples/analize_reexports/input_repo/src/moduleA/usage.ts new file mode 100644 index 0000000..75a3efd --- /dev/null +++ b/examples/analize_reexports/input_repo/src/moduleA/usage.ts @@ -0,0 +1,6 @@ +import { barFunction } from './reexports'; + +export const useBarFunction = () => { + console.log('Using barFunction in Module A'); + barFunction(); +}; \ No newline at end of file diff --git a/examples/analize_reexports/input_repo/src/moduleB/reexports.ts b/examples/analize_reexports/input_repo/src/moduleB/reexports.ts index ed9615c..36b9487 100644 --- a/examples/analize_reexports/input_repo/src/moduleB/reexports.ts +++ b/examples/analize_reexports/input_repo/src/moduleB/reexports.ts @@ -1,3 +1,4 @@ import { Foo, utilFunctionA } from './src/shared'; +import { barFunction } from './bar'; -export { Foo, utilFunctionA }; \ No newline at end of file +export { Foo, utilFunctionA, barFunction }; \ No newline at end of file diff --git a/examples/analize_reexports/input_repo/src/moduleB/src/shared/index.ts b/examples/analize_reexports/input_repo/src/moduleB/src/shared/index.ts index ae3a362..b35b676 100644 --- a/examples/analize_reexports/input_repo/src/moduleB/src/shared/index.ts +++ b/examples/analize_reexports/input_repo/src/moduleB/src/shared/index.ts @@ -1,2 +1 @@ -export { Foo } from '../../../moduleA/foo'; -export { utilFunctionA } from '../../../moduleA/utils'; \ No newline at end of file +export { Foo, utilFunctionA } from '../../../moduleA/reexports'; \ No newline at end of file diff --git a/examples/analize_reexports/input_repo/src/moduleB/usage.ts b/examples/analize_reexports/input_repo/src/moduleB/usage.ts new file mode 100644 index 0000000..d70d4de --- /dev/null +++ b/examples/analize_reexports/input_repo/src/moduleB/usage.ts @@ -0,0 +1,7 @@ +import { Foo, utilFunctionA } from './reexports'; + +export const useFooAndUtil = () => { + console.log('Using Foo and utilFunctionA in Module B'); + const fooInstance = new Foo(); + utilFunctionA(); +}; \ No newline at end of file diff --git a/examples/analize_reexports/input_repo/src/shared/moduleA/utils.ts b/examples/analize_reexports/input_repo/src/shared/moduleA/utils.ts new file mode 100644 index 0000000..5a1ba0b --- /dev/null +++ b/examples/analize_reexports/input_repo/src/shared/moduleA/utils.ts @@ -0,0 +1 @@ +export { utilFunctionA } from "../../moduleA/reexports" From 13e850ee579433e6447bcae30d4e5257098fd324 Mon Sep 17 00:00:00 2001 From: KopekC Date: Fri, 31 Jan 2025 00:03:38 -0500 Subject: [PATCH 09/20] . --- examples/analize_reexports/input_repo/src/moduleA/foo.ts | 5 ----- .../analize_reexports/input_repo/src/moduleA/reexports.ts | 5 ----- .../input_repo/src/moduleA/src/shared/index.ts | 1 - examples/analize_reexports/input_repo/src/moduleA/usage.ts | 6 ------ examples/analize_reexports/input_repo/src/moduleA/utils.ts | 3 --- examples/analize_reexports/input_repo/src/moduleB/bar.ts | 3 --- .../analize_reexports/input_repo/src/moduleB/reexports.ts | 4 ---- .../input_repo/src/moduleB/src/shared/index.ts | 1 - examples/analize_reexports/input_repo/src/moduleB/usage.ts | 7 ------- .../input_repo/src/shared/moduleA/utils.ts | 1 - 10 files changed, 36 deletions(-) delete mode 100644 examples/analize_reexports/input_repo/src/moduleA/foo.ts delete mode 100644 examples/analize_reexports/input_repo/src/moduleA/reexports.ts delete mode 100644 examples/analize_reexports/input_repo/src/moduleA/src/shared/index.ts delete mode 100644 examples/analize_reexports/input_repo/src/moduleA/usage.ts delete mode 100644 examples/analize_reexports/input_repo/src/moduleA/utils.ts delete mode 100644 examples/analize_reexports/input_repo/src/moduleB/bar.ts delete mode 100644 examples/analize_reexports/input_repo/src/moduleB/reexports.ts delete mode 100644 examples/analize_reexports/input_repo/src/moduleB/src/shared/index.ts delete mode 100644 examples/analize_reexports/input_repo/src/moduleB/usage.ts delete mode 100644 examples/analize_reexports/input_repo/src/shared/moduleA/utils.ts diff --git a/examples/analize_reexports/input_repo/src/moduleA/foo.ts b/examples/analize_reexports/input_repo/src/moduleA/foo.ts deleted file mode 100644 index 6174ec3..0000000 --- a/examples/analize_reexports/input_repo/src/moduleA/foo.ts +++ /dev/null @@ -1,5 +0,0 @@ -export class Foo { - constructor() { - console.log('Foo class from Module A'); - } -} \ No newline at end of file diff --git a/examples/analize_reexports/input_repo/src/moduleA/reexports.ts b/examples/analize_reexports/input_repo/src/moduleA/reexports.ts deleted file mode 100644 index a49320b..0000000 --- a/examples/analize_reexports/input_repo/src/moduleA/reexports.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { barFunction } from './src/shared'; -import { Foo } from './foo'; -import { utilFunctionA } from './utils'; - -export { barFunction, Foo, utilFunctionA }; \ No newline at end of file diff --git a/examples/analize_reexports/input_repo/src/moduleA/src/shared/index.ts b/examples/analize_reexports/input_repo/src/moduleA/src/shared/index.ts deleted file mode 100644 index 0f6db73..0000000 --- a/examples/analize_reexports/input_repo/src/moduleA/src/shared/index.ts +++ /dev/null @@ -1 +0,0 @@ -export { barFunction } from '../../../moduleB/reexports'; \ No newline at end of file diff --git a/examples/analize_reexports/input_repo/src/moduleA/usage.ts b/examples/analize_reexports/input_repo/src/moduleA/usage.ts deleted file mode 100644 index 75a3efd..0000000 --- a/examples/analize_reexports/input_repo/src/moduleA/usage.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { barFunction } from './reexports'; - -export const useBarFunction = () => { - console.log('Using barFunction in Module A'); - barFunction(); -}; \ No newline at end of file diff --git a/examples/analize_reexports/input_repo/src/moduleA/utils.ts b/examples/analize_reexports/input_repo/src/moduleA/utils.ts deleted file mode 100644 index 1f85a39..0000000 --- a/examples/analize_reexports/input_repo/src/moduleA/utils.ts +++ /dev/null @@ -1,3 +0,0 @@ -export const utilFunctionA = () => { - console.log('Utility function A'); -}; \ No newline at end of file diff --git a/examples/analize_reexports/input_repo/src/moduleB/bar.ts b/examples/analize_reexports/input_repo/src/moduleB/bar.ts deleted file mode 100644 index ce52446..0000000 --- a/examples/analize_reexports/input_repo/src/moduleB/bar.ts +++ /dev/null @@ -1,3 +0,0 @@ -export const barFunction = () => { - console.log('Bar function from Module B'); -}; \ No newline at end of file diff --git a/examples/analize_reexports/input_repo/src/moduleB/reexports.ts b/examples/analize_reexports/input_repo/src/moduleB/reexports.ts deleted file mode 100644 index 36b9487..0000000 --- a/examples/analize_reexports/input_repo/src/moduleB/reexports.ts +++ /dev/null @@ -1,4 +0,0 @@ -import { Foo, utilFunctionA } from './src/shared'; -import { barFunction } from './bar'; - -export { Foo, utilFunctionA, barFunction }; \ No newline at end of file diff --git a/examples/analize_reexports/input_repo/src/moduleB/src/shared/index.ts b/examples/analize_reexports/input_repo/src/moduleB/src/shared/index.ts deleted file mode 100644 index b35b676..0000000 --- a/examples/analize_reexports/input_repo/src/moduleB/src/shared/index.ts +++ /dev/null @@ -1 +0,0 @@ -export { Foo, utilFunctionA } from '../../../moduleA/reexports'; \ No newline at end of file diff --git a/examples/analize_reexports/input_repo/src/moduleB/usage.ts b/examples/analize_reexports/input_repo/src/moduleB/usage.ts deleted file mode 100644 index d70d4de..0000000 --- a/examples/analize_reexports/input_repo/src/moduleB/usage.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { Foo, utilFunctionA } from './reexports'; - -export const useFooAndUtil = () => { - console.log('Using Foo and utilFunctionA in Module B'); - const fooInstance = new Foo(); - utilFunctionA(); -}; \ No newline at end of file diff --git a/examples/analize_reexports/input_repo/src/shared/moduleA/utils.ts b/examples/analize_reexports/input_repo/src/shared/moduleA/utils.ts deleted file mode 100644 index 5a1ba0b..0000000 --- a/examples/analize_reexports/input_repo/src/shared/moduleA/utils.ts +++ /dev/null @@ -1 +0,0 @@ -export { utilFunctionA } from "../../moduleA/reexports" From 88cf7eaecddaf68deb7dcb67ee7328878d2a73e5 Mon Sep 17 00:00:00 2001 From: KopekC Date: Fri, 31 Jan 2025 00:11:14 -0500 Subject: [PATCH 10/20] . --- .../input_repo/src/module_a/functions.ts | 20 ++++++++++ .../input_repo/src/module_b/functions.ts | 23 +++++++++++ .../input_repo/src/module_b/imports.ts | 2 + .../input_repo/src/module_c/functions.ts | 40 +++++++++++++++++++ .../input_repo/src/module_c/imports.ts | 2 + 5 files changed, 87 insertions(+) create mode 100644 examples/analize_reexports/input_repo/src/module_a/functions.ts create mode 100644 examples/analize_reexports/input_repo/src/module_b/functions.ts create mode 100644 examples/analize_reexports/input_repo/src/module_b/imports.ts create mode 100644 examples/analize_reexports/input_repo/src/module_c/functions.ts create mode 100644 examples/analize_reexports/input_repo/src/module_c/imports.ts diff --git a/examples/analize_reexports/input_repo/src/module_a/functions.ts b/examples/analize_reexports/input_repo/src/module_a/functions.ts new file mode 100644 index 0000000..7e21f38 --- /dev/null +++ b/examples/analize_reexports/input_repo/src/module_a/functions.ts @@ -0,0 +1,20 @@ +export const calculateSum = (a: number, b: number): number => { + return a + b; +}; + +export const formatName = (firstName: string, lastName: string): string => { + return `${firstName} ${lastName}`; +}; + +export const generateId = (): string => { + return Math.random().toString(36).substring(7); +}; + +export const validateEmail = (email: string): boolean => { + const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; + return emailRegex.test(email); +}; + +export const capitalize = (str: string): string => { + return str.charAt(0).toUpperCase() + str.slice(1).toLowerCase(); +}; diff --git a/examples/analize_reexports/input_repo/src/module_b/functions.ts b/examples/analize_reexports/input_repo/src/module_b/functions.ts new file mode 100644 index 0000000..bc36cf4 --- /dev/null +++ b/examples/analize_reexports/input_repo/src/module_b/functions.ts @@ -0,0 +1,23 @@ +import { calculateSum, formatName, capitalize } from '../module_a'; + +export const calculateAverage = (numbers: number[]): number => { + const sum = numbers.reduce((acc, curr) => calculateSum(acc, curr), 0); + return sum / numbers.length; +}; + +export const createUserProfile = (firstName: string, lastName: string): string => { + const formattedName = formatName(firstName, lastName); + return `Profile: ${formattedName}`; +}; + +export const formatText = (text: string): string => { + return text.split(' ').map(capitalize).join(' '); +}; + +export const multiply = (a: number, b: number): number => { + return a * b; +}; + +export const generateGreeting = (name: string): string => { + return `Hello, ${capitalize(name)}!`; +}; diff --git a/examples/analize_reexports/input_repo/src/module_b/imports.ts b/examples/analize_reexports/input_repo/src/module_b/imports.ts new file mode 100644 index 0000000..d518a17 --- /dev/null +++ b/examples/analize_reexports/input_repo/src/module_b/imports.ts @@ -0,0 +1,2 @@ +import { calculateSum, formatName, capitalize } from '../module_a/functions'; + diff --git a/examples/analize_reexports/input_repo/src/module_c/functions.ts b/examples/analize_reexports/input_repo/src/module_c/functions.ts new file mode 100644 index 0000000..105289a --- /dev/null +++ b/examples/analize_reexports/input_repo/src/module_c/functions.ts @@ -0,0 +1,40 @@ +import { validateEmail, generateId } from '../module_a'; +import { calculateAverage, multiply, createUserProfile } from 'index'; + +export const createUser = (email: string, firstName: string, lastName: string) => { + if (!validateEmail(email)) { + throw new Error('Invalid email'); + } + + return { + id: generateId(), + profile: createUserProfile(firstName, lastName), + email + }; +}; + +export const calculateMetrics = (values: number[]): { average: number; scaled: number[] } => { + const avg = calculateAverage(values); + const scaled = values.map(v => multiply(v, 2)); + return { average: avg, scaled }; +}; + +export const validateAndFormatUser = (email: string, firstName: string, lastName: string) => { + if (!validateEmail(email)) { + return { success: false, message: 'Invalid email' }; + } + + const profile = createUserProfile(firstName, lastName); + return { success: true, profile }; +}; + +export const processNumbers = (numbers: number[]): number => { + const { average } = calculateMetrics(numbers); + return multiply(average, 100); +}; + +export const generateReport = (userData: { email: string; name: string }): string => { + const isValidEmail = validateEmail(userData.email); + const id = generateId(); + return `Report ${id}: Email ${isValidEmail ? 'valid' : 'invalid'} - ${userData.name}`; +}; diff --git a/examples/analize_reexports/input_repo/src/module_c/imports.ts b/examples/analize_reexports/input_repo/src/module_c/imports.ts new file mode 100644 index 0000000..0921827 --- /dev/null +++ b/examples/analize_reexports/input_repo/src/module_c/imports.ts @@ -0,0 +1,2 @@ +import { validateEmail, generateId } from '../module_a/functions'; +import { calculateAverage, multiply, createUserProfile } from '../module_b/functions'; From f628170df82dfe68776fe852bc96bd84395be44b Mon Sep 17 00:00:00 2001 From: KopekC Date: Fri, 31 Jan 2025 00:15:12 -0500 Subject: [PATCH 11/20] . --- .../input_repo/src/module_b/src/shared/exports.ts | 0 .../analize_reexports/input_repo/src/module_c/functions.ts | 3 +-- .../analize_reexports/input_repo/src/module_c/imports.ts | 5 +++-- .../input_repo/src/module_c/src/shared/symbols/exports.ts | 2 ++ 4 files changed, 6 insertions(+), 4 deletions(-) create mode 100644 examples/analize_reexports/input_repo/src/module_b/src/shared/exports.ts create mode 100644 examples/analize_reexports/input_repo/src/module_c/src/shared/symbols/exports.ts diff --git a/examples/analize_reexports/input_repo/src/module_b/src/shared/exports.ts b/examples/analize_reexports/input_repo/src/module_b/src/shared/exports.ts new file mode 100644 index 0000000..e69de29 diff --git a/examples/analize_reexports/input_repo/src/module_c/functions.ts b/examples/analize_reexports/input_repo/src/module_c/functions.ts index 105289a..426a34e 100644 --- a/examples/analize_reexports/input_repo/src/module_c/functions.ts +++ b/examples/analize_reexports/input_repo/src/module_c/functions.ts @@ -1,5 +1,4 @@ -import { validateEmail, generateId } from '../module_a'; -import { calculateAverage, multiply, createUserProfile } from 'index'; +import { validateEmail, generateId, calculateAverage, multiply, createUserProfile } from './src/shared/symbols/exports'; export const createUser = (email: string, firstName: string, lastName: string) => { if (!validateEmail(email)) { diff --git a/examples/analize_reexports/input_repo/src/module_c/imports.ts b/examples/analize_reexports/input_repo/src/module_c/imports.ts index 0921827..c855c8a 100644 --- a/examples/analize_reexports/input_repo/src/module_c/imports.ts +++ b/examples/analize_reexports/input_repo/src/module_c/imports.ts @@ -1,2 +1,3 @@ -import { validateEmail, generateId } from '../module_a/functions'; -import { calculateAverage, multiply, createUserProfile } from '../module_b/functions'; +export { validateEmail, generateId } from '../module_a/functions'; +export { calculateAverage, multiply, createUserProfile } from '../module_b/functions'; + diff --git a/examples/analize_reexports/input_repo/src/module_c/src/shared/symbols/exports.ts b/examples/analize_reexports/input_repo/src/module_c/src/shared/symbols/exports.ts new file mode 100644 index 0000000..5b24025 --- /dev/null +++ b/examples/analize_reexports/input_repo/src/module_c/src/shared/symbols/exports.ts @@ -0,0 +1,2 @@ +export { validateEmail, generateId } from '../../../imports' +export { calculateAverage, multiply, createUserProfile } from '../../../imports' From 4d78292a11f9c238356067bd949e49c742dd4881 Mon Sep 17 00:00:00 2001 From: KopekC Date: Fri, 31 Jan 2025 00:17:29 -0500 Subject: [PATCH 12/20] . --- examples/analize_reexports/input_repo/src/module_b/functions.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/analize_reexports/input_repo/src/module_b/functions.ts b/examples/analize_reexports/input_repo/src/module_b/functions.ts index bc36cf4..490e320 100644 --- a/examples/analize_reexports/input_repo/src/module_b/functions.ts +++ b/examples/analize_reexports/input_repo/src/module_b/functions.ts @@ -1,4 +1,4 @@ -import { calculateSum, formatName, capitalize } from '../module_a'; +import { calculateSum, formatName, capitalize } from '../module_a/functions'; export const calculateAverage = (numbers: number[]): number => { const sum = numbers.reduce((acc, curr) => calculateSum(acc, curr), 0); From 49317a264e3c2a8dfe114226eedec0b25ad46fac Mon Sep 17 00:00:00 2001 From: KopekC Date: Fri, 31 Jan 2025 00:21:23 -0500 Subject: [PATCH 13/20] . --- .../analize_reexports/input_repo/src/module_b/functions.ts | 3 ++- examples/analize_reexports/input_repo/src/module_b/imports.ts | 3 ++- .../input_repo/src/module_b/src/shared/exports.ts | 2 ++ 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/examples/analize_reexports/input_repo/src/module_b/functions.ts b/examples/analize_reexports/input_repo/src/module_b/functions.ts index 490e320..0a8a4fb 100644 --- a/examples/analize_reexports/input_repo/src/module_b/functions.ts +++ b/examples/analize_reexports/input_repo/src/module_b/functions.ts @@ -1,4 +1,4 @@ -import { calculateSum, formatName, capitalize } from '../module_a/functions'; +import { calculateSum, formatName, capitalize, validateEmail } from './src/shared/exports'; export const calculateAverage = (numbers: number[]): number => { const sum = numbers.reduce((acc, curr) => calculateSum(acc, curr), 0); @@ -19,5 +19,6 @@ export const multiply = (a: number, b: number): number => { }; export const generateGreeting = (name: string): string => { + const email = validateEmail(name); return `Hello, ${capitalize(name)}!`; }; diff --git a/examples/analize_reexports/input_repo/src/module_b/imports.ts b/examples/analize_reexports/input_repo/src/module_b/imports.ts index d518a17..0019593 100644 --- a/examples/analize_reexports/input_repo/src/module_b/imports.ts +++ b/examples/analize_reexports/input_repo/src/module_b/imports.ts @@ -1,2 +1,3 @@ -import { calculateSum, formatName, capitalize } from '../module_a/functions'; +export { calculateSum, formatName, capitalize } from '../module_a/functions'; +export { validateEmail } from '../module_c/src/shared/symbols/exports'; diff --git a/examples/analize_reexports/input_repo/src/module_b/src/shared/exports.ts b/examples/analize_reexports/input_repo/src/module_b/src/shared/exports.ts index e69de29..87c1f3e 100644 --- a/examples/analize_reexports/input_repo/src/module_b/src/shared/exports.ts +++ b/examples/analize_reexports/input_repo/src/module_b/src/shared/exports.ts @@ -0,0 +1,2 @@ +export { calculateSum, formatName, capitalize } from "../../imports"; +export {validateEmail} from "../../imports" \ No newline at end of file From 0d11d6bcd8f6d196e80ff7b7b43e57fa6eec4a18 Mon Sep 17 00:00:00 2001 From: KopekC Date: Fri, 31 Jan 2025 00:48:37 -0500 Subject: [PATCH 14/20] . --- .../{src => modules}/module_a/functions.ts | 0 .../{src => modules}/module_b/functions.ts | 0 .../{src => modules}/module_b/imports.ts | 0 .../module_b/src/shared/exports.ts | 0 .../{src => modules}/module_c/functions.ts | 0 .../{src => modules}/module_c/imports.ts | 0 .../module_c/src/shared/symbols/exports.ts | 0 examples/analize_reexports/run.py | 60 ++++++++----------- 8 files changed, 24 insertions(+), 36 deletions(-) rename examples/analize_reexports/input_repo/{src => modules}/module_a/functions.ts (100%) rename examples/analize_reexports/input_repo/{src => modules}/module_b/functions.ts (100%) rename examples/analize_reexports/input_repo/{src => modules}/module_b/imports.ts (100%) rename examples/analize_reexports/input_repo/{src => modules}/module_b/src/shared/exports.ts (100%) rename examples/analize_reexports/input_repo/{src => modules}/module_c/functions.ts (100%) rename examples/analize_reexports/input_repo/{src => modules}/module_c/imports.ts (100%) rename examples/analize_reexports/input_repo/{src => modules}/module_c/src/shared/symbols/exports.ts (100%) diff --git a/examples/analize_reexports/input_repo/src/module_a/functions.ts b/examples/analize_reexports/input_repo/modules/module_a/functions.ts similarity index 100% rename from examples/analize_reexports/input_repo/src/module_a/functions.ts rename to examples/analize_reexports/input_repo/modules/module_a/functions.ts diff --git a/examples/analize_reexports/input_repo/src/module_b/functions.ts b/examples/analize_reexports/input_repo/modules/module_b/functions.ts similarity index 100% rename from examples/analize_reexports/input_repo/src/module_b/functions.ts rename to examples/analize_reexports/input_repo/modules/module_b/functions.ts diff --git a/examples/analize_reexports/input_repo/src/module_b/imports.ts b/examples/analize_reexports/input_repo/modules/module_b/imports.ts similarity index 100% rename from examples/analize_reexports/input_repo/src/module_b/imports.ts rename to examples/analize_reexports/input_repo/modules/module_b/imports.ts diff --git a/examples/analize_reexports/input_repo/src/module_b/src/shared/exports.ts b/examples/analize_reexports/input_repo/modules/module_b/src/shared/exports.ts similarity index 100% rename from examples/analize_reexports/input_repo/src/module_b/src/shared/exports.ts rename to examples/analize_reexports/input_repo/modules/module_b/src/shared/exports.ts diff --git a/examples/analize_reexports/input_repo/src/module_c/functions.ts b/examples/analize_reexports/input_repo/modules/module_c/functions.ts similarity index 100% rename from examples/analize_reexports/input_repo/src/module_c/functions.ts rename to examples/analize_reexports/input_repo/modules/module_c/functions.ts diff --git a/examples/analize_reexports/input_repo/src/module_c/imports.ts b/examples/analize_reexports/input_repo/modules/module_c/imports.ts similarity index 100% rename from examples/analize_reexports/input_repo/src/module_c/imports.ts rename to examples/analize_reexports/input_repo/modules/module_c/imports.ts diff --git a/examples/analize_reexports/input_repo/src/module_c/src/shared/symbols/exports.ts b/examples/analize_reexports/input_repo/modules/module_c/src/shared/symbols/exports.ts similarity index 100% rename from examples/analize_reexports/input_repo/src/module_c/src/shared/symbols/exports.ts rename to examples/analize_reexports/input_repo/modules/module_c/src/shared/symbols/exports.ts diff --git a/examples/analize_reexports/run.py b/examples/analize_reexports/run.py index bece14a..654eff8 100644 --- a/examples/analize_reexports/run.py +++ b/examples/analize_reexports/run.py @@ -5,18 +5,14 @@ processed_imports = set() def run(codebase: Codebase): - print("Processing files in the codebase...") + print("๐Ÿš€ Starting reexport analysis...") for file in codebase.files: - print(f"Checking file: {file.filepath}") - # Only process files under /src/shared - if "examples/analize_reexports" not in file.filepath: - print("Skipping file not in the target directory.") - continue - if '/src/shared' not in file.filepath: - print("Skipping file not in /src/shared.") + if "examples/analize_reexports" not in file.filepath or '/src/shared' not in file.filepath: continue + print(f"๐Ÿ“ Analyzing: {file.filepath}") + # Gather all reexports that are not external exports all_reexports = [] for export_stmt in file.export_statements: @@ -24,51 +20,44 @@ def run(codebase: Codebase): if export.is_reexport() and not export.is_external_export: all_reexports.append(export) - print(f"Found {len(all_reexports)} reexports in {file.filepath}") - - # Skip if there are none if not all_reexports: - print("No reexports found, moving to the next file.") continue + print(f"๐Ÿ“ฆ Found {len(all_reexports)} reexports to process") + for export in all_reexports: has_wildcard = False # Replace "src/" with "src/shared/" resolved_public_file = export.resolved_symbol.filepath.replace("src/", "src/shared/") - print(f"Resolved public file path: {resolved_public_file}") + print(f"๐Ÿ”„ Processing: {export.name} -> {resolved_public_file}") # Get relative path from the "public" file back to the original file relative_path = codebase.get_relative_path( from_file=resolved_public_file, to_file=export.resolved_symbol.filepath ) - print(f"Relative path: {relative_path}") # Ensure the "public" file exists if not codebase.has_file(resolved_public_file): - print(f"Creating new file: {resolved_public_file}") + print(f"โœจ Creating new public file: {resolved_public_file}") target_file = codebase.create_file(resolved_public_file, sync=True) else: - print(f"File already exists: {resolved_public_file}") target_file = codebase.get_file(resolved_public_file) # If target file already has a wildcard export for this relative path, skip if target_file.has_export_statement_for_path(relative_path, "WILDCARD"): has_wildcard = True - print("Wildcard export already exists, skipping.") continue # Compare "public" path to the local file's export.filepath if codebase._remove_extension(resolved_public_file) != codebase._remove_extension(export.filepath): - print("Processing export...") - - # A) Wildcard export, e.g. `export * from "..."` + # A) Wildcard export if export.is_wildcard_export(): target_file.insert_before(f'export * from "{relative_path}"') - print(f"Inserted wildcard export for {relative_path}") + print(f"โญ Added wildcard export for {relative_path}") - # B) Type export, e.g. `export type { Foo, Bar } from "..."` + # B) Type export elif export.is_type_export(): statement = file.get_export_statement_for_path(relative_path, "TYPE") if statement: @@ -76,7 +65,7 @@ def run(codebase: Codebase): statement.insert(0, f"{export.resolved_symbol.name} as {export.name}") else: statement.insert(0, f"{export.name}") - print(f"Inserted into existing type export statement for {relative_path}") + print(f"๐Ÿ“ Updated existing type export for {export.name}") else: if export.is_aliased(): target_file.insert_before( @@ -87,9 +76,9 @@ def run(codebase: Codebase): target_file.insert_before( f'export type {{ {export.name} }} from "{relative_path}"' ) - print(f"Inserted new type export statement for {relative_path}") + print(f"โœจ Added new type export for {export.name}") - # C) Normal export, e.g. `export { Foo, Bar } from "..."` + # C) Normal export else: statement = file.get_export_statement_for_path(relative_path, "EXPORT") if statement: @@ -97,7 +86,7 @@ def run(codebase: Codebase): statement.insert(0, f"{export.resolved_symbol.name} as {export.name}") else: statement.insert(0, f"{export.name}") - print(f"Inserted into existing export statement for {relative_path}") + print(f"๐Ÿ“ Updated existing export for {export.name}") else: if export.is_aliased(): target_file.insert_before( @@ -108,14 +97,13 @@ def run(codebase: Codebase): target_file.insert_before( f'export {{ {export.name} }} from "{relative_path}"' ) - print(f"Inserted new export statement for {relative_path}") + print(f"โœจ Added new export for {export.name}") - # Now update all import usages that refer to this export + # Update import usages for usage in export.symbol_usages(): if isinstance(usage, TSImport) and usage not in processed_imports: processed_imports.add(usage) - # Translate the resolved_public_file to the usage file's TS config import path new_path = usage.file.ts_config.translate_import_path(resolved_public_file) if has_wildcard and export.name != export.resolved_symbol.name: @@ -130,20 +118,20 @@ def run(codebase: Codebase): usage.file.insert_before(new_import) usage.remove() - print(f"Updated import in {usage.file.filepath}") + print(f"๐Ÿ”„ Updated import in {usage.file.filepath}") - # Remove the old export from the original file + # Remove old export export.remove() - print(f"Removed old export from {export.filepath}") + print(f"๐Ÿ—‘๏ธ Removed old export from {export.filepath}") - # If the file ends up with no exports, remove it entirely + # Clean up empty files if not file.export_statements and len(file.symbols) == 0: file.remove() - print(f"Removed empty file: {file.filepath}") + print(f"๐Ÿงน Removed empty file: {file.filepath}") codebase.commit() if __name__ == "__main__": - print("Starting...") + print("๐ŸŽฏ Starting reexport organization...") codebase = Codebase("./", programming_language=ProgrammingLanguage.TYPESCRIPT) run(codebase) - print("Done!") + print("โœ… Done! All reexports organized successfully!") From e9125e45859e4facef237119f300d6b9e2cb31ee Mon Sep 17 00:00:00 2001 From: KopekC Date: Fri, 31 Jan 2025 00:51:06 -0500 Subject: [PATCH 15/20] . --- examples/analize_reexports/input_repo/tsconfig.json | 4 ++-- examples/analize_reexports/run.py | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/examples/analize_reexports/input_repo/tsconfig.json b/examples/analize_reexports/input_repo/tsconfig.json index 531309f..6c9c6ed 100644 --- a/examples/analize_reexports/input_repo/tsconfig.json +++ b/examples/analize_reexports/input_repo/tsconfig.json @@ -2,8 +2,8 @@ "compilerOptions": { "baseUrl": "./", "paths": { - "*": ["src/*"] + "*": ["modules/*"] } }, - "include": ["src/**/*"] + "include": ["modules/**/*"] } \ No newline at end of file diff --git a/examples/analize_reexports/run.py b/examples/analize_reexports/run.py index 654eff8..2c36b0f 100644 --- a/examples/analize_reexports/run.py +++ b/examples/analize_reexports/run.py @@ -39,6 +39,7 @@ def run(codebase: Codebase): ) # Ensure the "public" file exists + print(f" resolved_public_file: {resolved_public_file}") if not codebase.has_file(resolved_public_file): print(f"โœจ Creating new public file: {resolved_public_file}") target_file = codebase.create_file(resolved_public_file, sync=True) From c9f4252b4a8274acde7bd5e05e388c6c6df73913 Mon Sep 17 00:00:00 2001 From: KopekC Date: Fri, 31 Jan 2025 00:53:40 -0500 Subject: [PATCH 16/20] . --- .../input_repo/modules/module_a/{ => src}/functions.ts | 0 .../input_repo/modules/module_a/src/shared/index.ts | 0 .../analize_reexports/input_repo/modules/module_b/imports.ts | 2 +- .../input_repo/modules/module_b/{ => src}/functions.ts | 2 +- .../analize_reexports/input_repo/modules/module_c/imports.ts | 4 ++-- .../input_repo/modules/module_c/{ => src}/functions.ts | 2 +- 6 files changed, 5 insertions(+), 5 deletions(-) rename examples/analize_reexports/input_repo/modules/module_a/{ => src}/functions.ts (100%) create mode 100644 examples/analize_reexports/input_repo/modules/module_a/src/shared/index.ts rename examples/analize_reexports/input_repo/modules/module_b/{ => src}/functions.ts (96%) rename examples/analize_reexports/input_repo/modules/module_c/{ => src}/functions.ts (95%) diff --git a/examples/analize_reexports/input_repo/modules/module_a/functions.ts b/examples/analize_reexports/input_repo/modules/module_a/src/functions.ts similarity index 100% rename from examples/analize_reexports/input_repo/modules/module_a/functions.ts rename to examples/analize_reexports/input_repo/modules/module_a/src/functions.ts diff --git a/examples/analize_reexports/input_repo/modules/module_a/src/shared/index.ts b/examples/analize_reexports/input_repo/modules/module_a/src/shared/index.ts new file mode 100644 index 0000000..e69de29 diff --git a/examples/analize_reexports/input_repo/modules/module_b/imports.ts b/examples/analize_reexports/input_repo/modules/module_b/imports.ts index 0019593..7f4dd6f 100644 --- a/examples/analize_reexports/input_repo/modules/module_b/imports.ts +++ b/examples/analize_reexports/input_repo/modules/module_b/imports.ts @@ -1,3 +1,3 @@ -export { calculateSum, formatName, capitalize } from '../module_a/functions'; +export { calculateSum, formatName, capitalize } from '../module_a/src/functions'; export { validateEmail } from '../module_c/src/shared/symbols/exports'; diff --git a/examples/analize_reexports/input_repo/modules/module_b/functions.ts b/examples/analize_reexports/input_repo/modules/module_b/src/functions.ts similarity index 96% rename from examples/analize_reexports/input_repo/modules/module_b/functions.ts rename to examples/analize_reexports/input_repo/modules/module_b/src/functions.ts index 0a8a4fb..1158610 100644 --- a/examples/analize_reexports/input_repo/modules/module_b/functions.ts +++ b/examples/analize_reexports/input_repo/modules/module_b/src/functions.ts @@ -1,4 +1,4 @@ -import { calculateSum, formatName, capitalize, validateEmail } from './src/shared/exports'; +import { calculateSum, formatName, capitalize, validateEmail } from './shared/exports'; export const calculateAverage = (numbers: number[]): number => { const sum = numbers.reduce((acc, curr) => calculateSum(acc, curr), 0); diff --git a/examples/analize_reexports/input_repo/modules/module_c/imports.ts b/examples/analize_reexports/input_repo/modules/module_c/imports.ts index c855c8a..cf00eb0 100644 --- a/examples/analize_reexports/input_repo/modules/module_c/imports.ts +++ b/examples/analize_reexports/input_repo/modules/module_c/imports.ts @@ -1,3 +1,3 @@ -export { validateEmail, generateId } from '../module_a/functions'; -export { calculateAverage, multiply, createUserProfile } from '../module_b/functions'; +export { validateEmail, generateId } from '../module_a/src/functions'; +export { calculateAverage, multiply, createUserProfile } from '../module_b/src/functions'; diff --git a/examples/analize_reexports/input_repo/modules/module_c/functions.ts b/examples/analize_reexports/input_repo/modules/module_c/src/functions.ts similarity index 95% rename from examples/analize_reexports/input_repo/modules/module_c/functions.ts rename to examples/analize_reexports/input_repo/modules/module_c/src/functions.ts index 426a34e..37a0443 100644 --- a/examples/analize_reexports/input_repo/modules/module_c/functions.ts +++ b/examples/analize_reexports/input_repo/modules/module_c/src/functions.ts @@ -1,4 +1,4 @@ -import { validateEmail, generateId, calculateAverage, multiply, createUserProfile } from './src/shared/symbols/exports'; +import { validateEmail, generateId, calculateAverage, multiply, createUserProfile } from './shared/symbols/exports'; export const createUser = (email: string, firstName: string, lastName: string) => { if (!validateEmail(email)) { From cb7dadde134e376b23be2d80baf6ba7bcf8a6b09 Mon Sep 17 00:00:00 2001 From: KopekC Date: Fri, 31 Jan 2025 01:02:47 -0500 Subject: [PATCH 17/20] . --- examples/analize_reexports/README.md | 109 +++++++++++++++++++++++++++ examples/analize_reexports/run.py | 4 +- 2 files changed, 111 insertions(+), 2 deletions(-) create mode 100644 examples/analize_reexports/README.md diff --git a/examples/analize_reexports/README.md b/examples/analize_reexports/README.md new file mode 100644 index 0000000..6d95b8b --- /dev/null +++ b/examples/analize_reexports/README.md @@ -0,0 +1,109 @@ +# Transform Module Re-exports Organization + +This example demonstrates how to use Codegen to automatically analyze and reorganize TypeScript module re-exports through shared directories. The script makes this process simple by handling all the tedious manual updates automatically. + +> [!NOTE] +> This codemod helps maintain clean module boundaries and improves code organization by centralizing shared exports. + +## How the Migration Script Works + +The script automates the entire reorganization process in a few key steps: + +1. **Export Analysis** + ```python + for export_stmt in file.export_statements: + for export in export_stmt.exports: + if export.is_reexport() and not export.is_external_export: + all_reexports.append(export) + ``` + - Automatically identifies re-exports in shared directories + - Analyzes export patterns and dependencies + - Uses Codegen's intelligent code analysis engine + +2. **Shared File Management** + ```python + resolved_public_file = export.resolved_symbol.filepath.replace("src/", "src/shared/") + if not codebase.has_file(resolved_public_file): + target_file = codebase.create_file(resolved_public_file, sync=True) + ``` + - Creates or updates shared export files + - Maintains proper file structure + - Handles path resolution automatically + +3. **Import Updates** + ```python + # Updates imports to use new shared paths + new_path = usage.file.ts_config.translate_import_path(resolved_public_file) + new_import = f'import {{ {name} }} from "{new_path}"' + ``` + - Updates all import statements to use new paths + - Maintains proper TypeScript path resolution + - Handles different import types (normal, type) + +## Why This Makes Organization Easy + +1. **Zero Manual Updates** + - Codegen SDK handles all file creation and updates + - No tedious export management + +2. **Consistent Structure** + - Ensures all shared exports follow the same pattern + - Maintains clean module boundaries + +3. **Safe Transformations** + - Validates changes before applying them + - Preserves existing functionality + +## Common Re-export Patterns + +### Module to Shared Exports +```typescript +// Before: Direct module import +import { validateEmail } from '../module_a/src/functions'; + +// After: Import through shared +import { validateEmail } from '../module_a/src/shared'; +``` + +### Export Consolidation +```typescript +// Before: Multiple export files +export { foo } from './foo'; +export { bar } from './bar'; + +// After: Consolidated in shared +export * from '../functions'; +``` + +## Key Benefits to Note + +1. **Better Module Boundaries** + - Clear public API for each module + - Centralized shared functionality + +2. **Improved Maintainability** + - Easier to track dependencies + - Simplified import paths + +3. **Code Organization** + - Consistent export structure + - Reduced import complexity + + +The script will: +1. ๐ŸŽฏ Start the reexport organization +2. ๐Ÿ“ Analyze shared directories +3. ๐Ÿ”„ Process and update exports +4. โœจ Create shared export files +5. ๐Ÿงน Clean up redundant exports + +## Learn More + +- [TypeScript Modules](https://www.typescriptlang.org/docs/handbook/modules.html) +- [Export/Import Documentation](https://www.typescriptlang.org/docs/handbook/modules.html#export) +- [Codegen Documentation](https://docs.codegen.com) +- [Tutorial on Analyzing and Organizing Re-exports](https://docs.codegen.com/tutorials/managing-typescript-exports) +- [More on exports ](https://docs.codegen.com/building-with-codegen/exports) +## Contributing + +Feel free to submit issues and enhancement requests! \ No newline at end of file diff --git a/examples/analize_reexports/run.py b/examples/analize_reexports/run.py index 2c36b0f..bac8476 100644 --- a/examples/analize_reexports/run.py +++ b/examples/analize_reexports/run.py @@ -1,9 +1,10 @@ +import codegen from codegen import Codebase from codegen.sdk.typescript.file import TSFile, TSImport from codegen.sdk.enums import ProgrammingLanguage processed_imports = set() - +@codegen.function("reexport_management" def run(codebase: Codebase): print("๐Ÿš€ Starting reexport analysis...") for file in codebase.files: @@ -39,7 +40,6 @@ def run(codebase: Codebase): ) # Ensure the "public" file exists - print(f" resolved_public_file: {resolved_public_file}") if not codebase.has_file(resolved_public_file): print(f"โœจ Creating new public file: {resolved_public_file}") target_file = codebase.create_file(resolved_public_file, sync=True) From 03a1aa502208e4e678170161d373a9db8abc7283 Mon Sep 17 00:00:00 2001 From: KopekC Date: Fri, 31 Jan 2025 01:03:48 -0500 Subject: [PATCH 18/20] . --- examples/{analize_reexports => reexport_management}/README.md | 0 .../input_repo/modules/module_a/src/functions.ts | 0 .../input_repo/modules/module_a/src/shared/index.ts | 0 .../input_repo/modules/module_b/imports.ts | 0 .../input_repo/modules/module_b/src/functions.ts | 0 .../input_repo/modules/module_b/src/shared/exports.ts | 0 .../input_repo/modules/module_c/imports.ts | 0 .../input_repo/modules/module_c/src/functions.ts | 0 .../input_repo/modules/module_c/src/shared/symbols/exports.ts | 0 .../input_repo/package.json | 0 .../input_repo/tsconfig.json | 0 examples/{analize_reexports => reexport_management}/run.py | 2 +- 12 files changed, 1 insertion(+), 1 deletion(-) rename examples/{analize_reexports => reexport_management}/README.md (100%) rename examples/{analize_reexports => reexport_management}/input_repo/modules/module_a/src/functions.ts (100%) rename examples/{analize_reexports => reexport_management}/input_repo/modules/module_a/src/shared/index.ts (100%) rename examples/{analize_reexports => reexport_management}/input_repo/modules/module_b/imports.ts (100%) rename examples/{analize_reexports => reexport_management}/input_repo/modules/module_b/src/functions.ts (100%) rename examples/{analize_reexports => reexport_management}/input_repo/modules/module_b/src/shared/exports.ts (100%) rename examples/{analize_reexports => reexport_management}/input_repo/modules/module_c/imports.ts (100%) rename examples/{analize_reexports => reexport_management}/input_repo/modules/module_c/src/functions.ts (100%) rename examples/{analize_reexports => reexport_management}/input_repo/modules/module_c/src/shared/symbols/exports.ts (100%) rename examples/{analize_reexports => reexport_management}/input_repo/package.json (100%) rename examples/{analize_reexports => reexport_management}/input_repo/tsconfig.json (100%) rename examples/{analize_reexports => reexport_management}/run.py (99%) diff --git a/examples/analize_reexports/README.md b/examples/reexport_management/README.md similarity index 100% rename from examples/analize_reexports/README.md rename to examples/reexport_management/README.md diff --git a/examples/analize_reexports/input_repo/modules/module_a/src/functions.ts b/examples/reexport_management/input_repo/modules/module_a/src/functions.ts similarity index 100% rename from examples/analize_reexports/input_repo/modules/module_a/src/functions.ts rename to examples/reexport_management/input_repo/modules/module_a/src/functions.ts diff --git a/examples/analize_reexports/input_repo/modules/module_a/src/shared/index.ts b/examples/reexport_management/input_repo/modules/module_a/src/shared/index.ts similarity index 100% rename from examples/analize_reexports/input_repo/modules/module_a/src/shared/index.ts rename to examples/reexport_management/input_repo/modules/module_a/src/shared/index.ts diff --git a/examples/analize_reexports/input_repo/modules/module_b/imports.ts b/examples/reexport_management/input_repo/modules/module_b/imports.ts similarity index 100% rename from examples/analize_reexports/input_repo/modules/module_b/imports.ts rename to examples/reexport_management/input_repo/modules/module_b/imports.ts diff --git a/examples/analize_reexports/input_repo/modules/module_b/src/functions.ts b/examples/reexport_management/input_repo/modules/module_b/src/functions.ts similarity index 100% rename from examples/analize_reexports/input_repo/modules/module_b/src/functions.ts rename to examples/reexport_management/input_repo/modules/module_b/src/functions.ts diff --git a/examples/analize_reexports/input_repo/modules/module_b/src/shared/exports.ts b/examples/reexport_management/input_repo/modules/module_b/src/shared/exports.ts similarity index 100% rename from examples/analize_reexports/input_repo/modules/module_b/src/shared/exports.ts rename to examples/reexport_management/input_repo/modules/module_b/src/shared/exports.ts diff --git a/examples/analize_reexports/input_repo/modules/module_c/imports.ts b/examples/reexport_management/input_repo/modules/module_c/imports.ts similarity index 100% rename from examples/analize_reexports/input_repo/modules/module_c/imports.ts rename to examples/reexport_management/input_repo/modules/module_c/imports.ts diff --git a/examples/analize_reexports/input_repo/modules/module_c/src/functions.ts b/examples/reexport_management/input_repo/modules/module_c/src/functions.ts similarity index 100% rename from examples/analize_reexports/input_repo/modules/module_c/src/functions.ts rename to examples/reexport_management/input_repo/modules/module_c/src/functions.ts diff --git a/examples/analize_reexports/input_repo/modules/module_c/src/shared/symbols/exports.ts b/examples/reexport_management/input_repo/modules/module_c/src/shared/symbols/exports.ts similarity index 100% rename from examples/analize_reexports/input_repo/modules/module_c/src/shared/symbols/exports.ts rename to examples/reexport_management/input_repo/modules/module_c/src/shared/symbols/exports.ts diff --git a/examples/analize_reexports/input_repo/package.json b/examples/reexport_management/input_repo/package.json similarity index 100% rename from examples/analize_reexports/input_repo/package.json rename to examples/reexport_management/input_repo/package.json diff --git a/examples/analize_reexports/input_repo/tsconfig.json b/examples/reexport_management/input_repo/tsconfig.json similarity index 100% rename from examples/analize_reexports/input_repo/tsconfig.json rename to examples/reexport_management/input_repo/tsconfig.json diff --git a/examples/analize_reexports/run.py b/examples/reexport_management/run.py similarity index 99% rename from examples/analize_reexports/run.py rename to examples/reexport_management/run.py index bac8476..5296fc8 100644 --- a/examples/analize_reexports/run.py +++ b/examples/reexport_management/run.py @@ -4,7 +4,7 @@ from codegen.sdk.enums import ProgrammingLanguage processed_imports = set() -@codegen.function("reexport_management" +@codegen.function("reexport_management") def run(codebase: Codebase): print("๐Ÿš€ Starting reexport analysis...") for file in codebase.files: From 2f114e1e3e8a368579958538cc22ef0de404d989 Mon Sep 17 00:00:00 2001 From: KopekC Date: Fri, 31 Jan 2025 14:52:14 -0500 Subject: [PATCH 19/20] Adds fragment to shorthand example --- examples/fragment_to_shorthand/README.md | 67 ++++++++++++++++++++++++ examples/fragment_to_shorthand/run.py | 37 +++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 examples/fragment_to_shorthand/README.md create mode 100644 examples/fragment_to_shorthand/run.py diff --git a/examples/fragment_to_shorthand/README.md b/examples/fragment_to_shorthand/README.md new file mode 100644 index 0000000..d124439 --- /dev/null +++ b/examples/fragment_to_shorthand/README.md @@ -0,0 +1,67 @@ +# Transform React Fragment to Shorthand Syntax + +This example demonstrates how to use Codegen to automatically convert React Fragment components to the shorthand syntax (<>). The script makes this process simple by handling all the tedious manual updates automatically. + +> [!NOTE] +> This codemod helps modernize React codebases by using the more concise fragment syntax while maintaining functionality. + +## How the Migration Script Works + +The script automates the entire conversion process in a few key steps: + +1. **Fragment Detection** + ```jsx + // From: + +
Hello
+
World
+
+ + // To: + <> +
Hello
+
World
+ + ``` + +2. **Import Cleanup** + ```typescript + // From: + import React, { Fragment } from 'react'; + + // To: + import React from 'react'; + ``` + +## Why This Makes Migration Easy + +1. **Zero Manual Updates** + - Codegen SDK handles all Fragment replacements + - Automatically cleans up imports + +2. **Consistent Changes** + - Ensures all Fragments are converted + - Maintains code functionality + +3. **Safe Transformations** + - Preserves JSX structure + - Handles nested Fragments correctly + +## Running the Migration + + +The script will: +1. Find all Fragment components +2. Convert them to shorthand syntax +3. Clean up Fragment imports +4. Preserve other React imports + +## Learn More + +- [React Fragments](https://react.dev/reference/react/Fragment) +- [JSX Fragments](https://react.dev/reference/jsx#jsx-fragments) +- [Codegen Documentation](https://docs.codegen.com) +- [More on Codegen SDK jsx elements API](https://docs.codegen.com/api-reference/typescript/JSXElement#jsxelement) +## Contributing + +Feel free to submit issues and enhancement requests! \ No newline at end of file diff --git a/examples/fragment_to_shorthand/run.py b/examples/fragment_to_shorthand/run.py new file mode 100644 index 0000000..d726891 --- /dev/null +++ b/examples/fragment_to_shorthand/run.py @@ -0,0 +1,37 @@ +import codegen +from codegen import Codebase +from codegen.sdk.enums import ProgrammingLanguage + +@codegen.function("fragment_to_shorthand") +def run(codebase: Codebase): + print("๐Ÿ” Starting Fragment syntax conversion...") + + for file in codebase.files: + print(f"๐Ÿ“ Processing: {file.filepath}") + + fragments_found = False + + # Convert Fragment components to shorthand + for element in file.jsx_elements: + if element.name == "Fragment": + print(f"๐Ÿ”„ Converting Fragment in {file.filepath}") + element.set_name("") # Convert to <> syntax + fragments_found = True + + # Clean up Fragment imports if we found and converted any + if fragments_found: + for import_stmt in file.import_statements: + for imp in import_stmt.imports: + if imp.name == "Fragment": + print(f"๐Ÿงน Removing Fragment import from {file.filepath}") + imp.remove() + + if fragments_found: + print(f"โœจ Completed conversion in {file.filepath}") + codebase.commit() + +if __name__ == "__main__": + print("๐ŸŽฏ Starting Fragment to shorthand conversion...") + codebase = Codebase.from_repo("RocketChat/Rocket.Chat",commit="a4f2102af1c2e875c60cafebd0163105bdaca678",programming_language=ProgrammingLanguage.TYPESCRIPT) + run(codebase) + print("โœ… Done! All Fragments converted to shorthand syntax!") \ No newline at end of file From bfb8bf99efc5eb949b10767333e88a4971299d6a Mon Sep 17 00:00:00 2001 From: kopekC <28070492+kopekC@users.noreply.github.com> Date: Fri, 31 Jan 2025 19:53:02 +0000 Subject: [PATCH 20/20] Automated pre-commit update --- examples/fragment_to_shorthand/run.py | 16 ++++++++------ examples/reexport_management/run.py | 32 +++++++++------------------ 2 files changed, 20 insertions(+), 28 deletions(-) diff --git a/examples/fragment_to_shorthand/run.py b/examples/fragment_to_shorthand/run.py index d726891..c140cb1 100644 --- a/examples/fragment_to_shorthand/run.py +++ b/examples/fragment_to_shorthand/run.py @@ -2,22 +2,23 @@ from codegen import Codebase from codegen.sdk.enums import ProgrammingLanguage + @codegen.function("fragment_to_shorthand") def run(codebase: Codebase): print("๐Ÿ” Starting Fragment syntax conversion...") - + for file in codebase.files: print(f"๐Ÿ“ Processing: {file.filepath}") - + fragments_found = False - + # Convert Fragment components to shorthand for element in file.jsx_elements: if element.name == "Fragment": print(f"๐Ÿ”„ Converting Fragment in {file.filepath}") element.set_name("") # Convert to <> syntax fragments_found = True - + # Clean up Fragment imports if we found and converted any if fragments_found: for import_stmt in file.import_statements: @@ -25,13 +26,14 @@ def run(codebase: Codebase): if imp.name == "Fragment": print(f"๐Ÿงน Removing Fragment import from {file.filepath}") imp.remove() - + if fragments_found: print(f"โœจ Completed conversion in {file.filepath}") codebase.commit() + if __name__ == "__main__": print("๐ŸŽฏ Starting Fragment to shorthand conversion...") - codebase = Codebase.from_repo("RocketChat/Rocket.Chat",commit="a4f2102af1c2e875c60cafebd0163105bdaca678",programming_language=ProgrammingLanguage.TYPESCRIPT) + codebase = Codebase.from_repo("RocketChat/Rocket.Chat", commit="a4f2102af1c2e875c60cafebd0163105bdaca678", programming_language=ProgrammingLanguage.TYPESCRIPT) run(codebase) - print("โœ… Done! All Fragments converted to shorthand syntax!") \ No newline at end of file + print("โœ… Done! All Fragments converted to shorthand syntax!") diff --git a/examples/reexport_management/run.py b/examples/reexport_management/run.py index 6c85323..b4b0aaf 100644 --- a/examples/reexport_management/run.py +++ b/examples/reexport_management/run.py @@ -6,16 +6,18 @@ from codegen.sdk.enums import ProgrammingLanguage processed_imports = set() + + @codegen.function("reexport_management") def run(codebase: Codebase): print("๐Ÿš€ Starting reexport analysis...") for file in codebase.files: # Only process files under /src/shared - if "examples/analize_reexports" not in file.filepath or '/src/shared' not in file.filepath: + if "examples/analize_reexports" not in file.filepath or "/src/shared" not in file.filepath: continue - + print(f"๐Ÿ“ Analyzing: {file.filepath}") - + # Gather all reexports that are not external exports all_reexports = [] for export_stmt in file.export_statements: @@ -36,10 +38,7 @@ def run(codebase: Codebase): print(f"๐Ÿ”„ Processing: {export.name} -> {resolved_public_file}") # Get relative path from the "public" file back to the original file - relative_path = codebase.get_relative_path( - from_file=resolved_public_file, - to_file=export.resolved_symbol.filepath - ) + relative_path = codebase.get_relative_path(from_file=resolved_public_file, to_file=export.resolved_symbol.filepath) # Ensure the "public" file exists if not codebase.has_file(resolved_public_file): @@ -71,14 +70,9 @@ def run(codebase: Codebase): print(f"๐Ÿ“ Updated existing type export for {export.name}") else: if export.is_aliased(): - target_file.insert_before( - f'export type {{ {export.resolved_symbol.name} as {export.name} }} ' - f'from "{relative_path}"' - ) + target_file.insert_before(f'export type {{ {export.resolved_symbol.name} as {export.name} }} from "{relative_path}"') else: - target_file.insert_before( - f'export type {{ {export.name} }} from "{relative_path}"' - ) + target_file.insert_before(f'export type {{ {export.name} }} from "{relative_path}"') print(f"โœจ Added new type export for {export.name}") # C) Normal export @@ -92,14 +86,9 @@ def run(codebase: Codebase): print(f"๐Ÿ“ Updated existing export for {export.name}") else: if export.is_aliased(): - target_file.insert_before( - f'export {{ {export.resolved_symbol.name} as {export.name} }} ' - f'from "{relative_path}"' - ) + target_file.insert_before(f'export {{ {export.resolved_symbol.name} as {export.name} }} from "{relative_path}"') else: - target_file.insert_before( - f'export {{ {export.name} }} from "{relative_path}"' - ) + target_file.insert_before(f'export {{ {export.name} }} from "{relative_path}"') print(f"โœจ Added new export for {export.name}") # Update import usages @@ -133,6 +122,7 @@ def run(codebase: Codebase): print(f"๐Ÿงน Removed empty file: {file.filepath}") codebase.commit() + if __name__ == "__main__": print("๐ŸŽฏ Starting reexport organization...") codebase = Codebase("./", programming_language=ProgrammingLanguage.TYPESCRIPT)