|
8 | 8 | from codegen.sdk.core.expressions import Name
|
9 | 9 | from codegen.sdk.core.import_resolution import Import, ImportResolution, WildcardImport
|
10 | 10 | from codegen.sdk.core.interfaces.exportable import Exportable
|
11 |
| -from codegen.sdk.enums import ImportType, NodeType |
| 11 | +from codegen.sdk.enums import ImportType, NodeType, SymbolType |
12 | 12 | from codegen.sdk.utils import find_all_descendants, find_first_ancestor, find_first_descendant
|
13 | 13 | from codegen.shared.decorators.docs import noapidoc, ts_apidoc
|
14 | 14 |
|
|
24 | 24 | from codegen.sdk.core.statements.import_statement import ImportStatement
|
25 | 25 | from codegen.sdk.core.symbol import Symbol
|
26 | 26 | from codegen.sdk.typescript.file import TSFile
|
| 27 | + from codegen.sdk.typescript.namespace import TSNamespace |
27 | 28 | from codegen.sdk.typescript.statements.import_statement import TSImportStatement
|
28 | 29 |
|
29 | 30 |
|
@@ -578,6 +579,48 @@ def names(self) -> Generator[tuple[str, Self | WildcardImport[Self]], None, None
|
578 | 579 | return
|
579 | 580 | yield from super().names
|
580 | 581 |
|
| 582 | + @property |
| 583 | + def namespace_imports(self) -> list[TSNamespace]: |
| 584 | + """Returns any namespace objects imported by this import statement. |
| 585 | +
|
| 586 | + For example: |
| 587 | + import * as MyNS from './mymodule'; |
| 588 | +
|
| 589 | + Returns: |
| 590 | + List of namespace objects imported |
| 591 | + """ |
| 592 | + if not self.is_namespace_import(): |
| 593 | + return [] |
| 594 | + |
| 595 | + from codegen.sdk.typescript.namespace import TSNamespace |
| 596 | + |
| 597 | + resolved = self.resolved_symbol |
| 598 | + if resolved is None or not isinstance(resolved, TSNamespace): |
| 599 | + return [] |
| 600 | + |
| 601 | + return [resolved] |
| 602 | + |
| 603 | + @property |
| 604 | + def is_namespace_import(self) -> bool: |
| 605 | + """Returns True if this import is importing a namespace. |
| 606 | +
|
| 607 | + Examples: |
| 608 | + import { MathUtils } from './file1'; # True if MathUtils is a namespace |
| 609 | + import * as AllUtils from './utils'; # True |
| 610 | + """ |
| 611 | + # For wildcard imports with namespace alias |
| 612 | + if self.import_type == ImportType.WILDCARD and self.namespace: |
| 613 | + return True |
| 614 | + |
| 615 | + # For named imports, check if any imported symbol is a namespace |
| 616 | + if self.import_type == ImportType.NAMED_EXPORT: |
| 617 | + for name, _ in self.names: |
| 618 | + symbol = self.resolved_symbol |
| 619 | + if symbol and symbol.symbol_type == SymbolType.Namespace: |
| 620 | + return True |
| 621 | + |
| 622 | + return False |
| 623 | + |
581 | 624 | @override
|
582 | 625 | def set_import_module(self, new_module: str) -> None:
|
583 | 626 | """Sets the module of an import.
|
|
0 commit comments