|
1 | 1 | # Import Resolution |
2 | 2 |
|
3 | | -TODO |
| 3 | +Import resolution follows AST construction in the code analysis pipeline. It identifies dependencies between modules and builds a graph of relationships across the codebase. |
| 4 | + |
| 5 | +> NOTE: This is an actively evolving part of Codegen SDK, so some details here may be imcomplete, outdated, or incorrect. |
| 6 | +
|
| 7 | +## Purpose |
| 8 | + |
| 9 | +The import resolution system serves these purposes: |
| 10 | + |
| 11 | +1. **Dependency Tracking**: Maps relationships between files by resolving import statements. |
| 12 | +1. **Symbol Resolution**: Connects imported symbols to their definitions. |
| 13 | +1. **Module Graph Construction**: Builds a directed graph of module dependencies. |
| 14 | +1. **(WIP) Cross-Language Support**: Provides implementations for different programming languages. |
| 15 | + |
| 16 | +## Core Components |
| 17 | + |
| 18 | +### ImportResolution Class |
| 19 | + |
| 20 | +The `ImportResolution` class represents the outcome of resolving an import statement. It contains: |
| 21 | + |
| 22 | +- The source file containing the imported symbol |
| 23 | +- The specific symbol being imported (if applicable) |
| 24 | +- Whether the import references an entire file/module |
| 25 | + |
| 26 | +### Import Base Class |
| 27 | + |
| 28 | +The `Import` class is the foundation for language-specific import implementations. It: |
| 29 | + |
| 30 | +- Stores metadata about the import (module path, symbol name, alias) |
| 31 | +- Provides the abstract `resolve_import()` method |
| 32 | +- Adds symbol resolution edges to the codebase graph |
| 33 | + |
| 34 | +### Language-Specific Implementations |
| 35 | + |
| 36 | +#### Python Import Resolution |
| 37 | + |
| 38 | +The `PyImport` class extends the base `Import` class with Python-specific logic: |
| 39 | + |
| 40 | +- Handles relative imports |
| 41 | +- Supports module imports, named imports, and wildcard imports |
| 42 | +- Resolves imports using configurable resolution paths and `sys.path` |
| 43 | +- Handles special cases like `__init__.py` files |
| 44 | + |
| 45 | +#### TypeScript Import Resolution |
| 46 | + |
| 47 | +The `TSImport` class implements TypeScript-specific resolution: |
| 48 | + |
| 49 | +- Supports named imports, default imports, and namespace imports |
| 50 | +- Handles type imports and dynamic imports |
| 51 | +- Resolves imports using TSConfig path mappings |
| 52 | +- Supports file extension resolution |
| 53 | + |
| 54 | +## Implementation |
| 55 | + |
| 56 | +After file and directory parse, we loop through all import nodes and perform `add_symbol_resolution_edge`. This then invokes the language-specific `resolve_import` method that converts the import statement into a resolvable `ImportResolution` object (or None if the import cannot be resolved). This import symbol and the `ImportResolution` object are then used to add a symbol resolution edge to the graph, where it can then be used in future steps to resolve symbols. |
4 | 57 |
|
5 | 58 | ## Next Step |
6 | 59 |
|
7 | | -After import resolution, the system analyzes [Export Analysis](./B.%20Exports.md) and handles [TSConfig Support](./C.%20TSConfig.md) for TypeScript projects. This is followed by comprehensive [Type Analysis](../4.%20type-analysis/A.%20Type%20Analysis.md). |
| 60 | +After import resolution, the system analyzes [Export Analysis](./B.%20Exports.md) and handles [TSConfig Support](./C.%20TSConfig.md) for TypeScript projects. This is followed by [Type Analysis](../4.%20type-analysis/A.%20Type%20Analysis.md). |
0 commit comments