Skip to content

Refactor: Break down excessively long handle_import_from method in import_transformer.rs #173

Description

@coderabbitai

Problem

The handle_import_from method in crates/cribo/src/code_generator/import_transformer.rs is over 570 lines long with deeply nested conditionals and duplicated logic. This violates the single responsibility principle and makes the code difficult to understand and maintain.

Proposed Solution

Break this method down into smaller, focused helper methods:

  1. Extract namespace object creation logic (currently duplicated around lines 710-866 and 873-1022) into a dedicated helper method like create_namespace_object_assignments

  2. Extract entry module duplicate check logic (lines 605-633) into a separate method

  3. Extract submodule import handling logic (lines 641-1056) into a separate method

  4. Extract wrapper module import handling (lines 1098-1137) into a separate method

Benefits

  • Improved readability and maintainability
  • Reduced code duplication
  • Better adherence to single responsibility principle
  • Easier testing of individual components
  • Reduced cognitive complexity

References

Acceptance Criteria

  • handle_import_from method is under 100 lines
  • Namespace creation logic is extracted into reusable helper method
  • Each helper method has a single, clear responsibility
  • No functionality is lost during refactoring
  • All existing tests continue to pass

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions