Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
a65955d
old changes
Jan 23, 2025
9000cdb
Merge branch 'develop' into tom-cg-8705-implement-namespace-as-ts-gra…
frainfreeze Feb 7, 2025
6cf569b
Automated pre-commit update
tkfoss Feb 7, 2025
1c6e694
update
frainfreeze Feb 7, 2025
7b13433
Automated pre-commit update
tkfoss Feb 7, 2025
3651517
Merge branch 'develop' into tom-cg-8705-implement-namespace-as-ts-gra…
Mar 12, 2025
bc810bd
ns
Mar 12, 2025
6fe9b1b
Automated pre-commit update
tkfoss Mar 12, 2025
d5a880d
Merge branch 'develop' into tom-cg-8705-implement-namespace-as-ts-gra…
Mar 12, 2025
ba97d40
Merge branch 'develop' into tom-cg-8705-implement-namespace-as-ts-gra…
Mar 12, 2025
795f12e
Merge branch 'develop' into tom-cg-8705-implement-namespace-as-ts-gra…
Mar 12, 2025
aa4475b
Merge branch 'develop' into tom-cg-8705-implement-namespace-as-ts-gra…
Mar 12, 2025
0a194ac
rm
Mar 13, 2025
c8825dd
ns fix
frainfreeze Mar 15, 2025
d8f43e2
Merge branch 'develop' into tom-cg-8705-implement-namespace-as-ts-gra…
Mar 15, 2025
f8c56b2
Automated pre-commit update
tkfoss Mar 15, 2025
457c9ac
fix
frainfreeze Mar 18, 2025
944406c
Merge branch 'develop' into tom-cg-8705-implement-namespace-as-ts-gra…
Mar 18, 2025
b90d111
ns fix
frainfreeze Mar 18, 2025
2cd662c
Automated pre-commit update
tkfoss Mar 18, 2025
97d1340
Merge branch 'develop' into tom-cg-8705-implement-namespace-as-ts-gra…
Mar 18, 2025
9686d59
fix
frainfreeze Mar 18, 2025
dc9a939
Merge branch 'develop' into tom-cg-8705-implement-namespace-as-ts-gra…
Mar 18, 2025
3957665
Merge branch 'develop' into tom-cg-8705-implement-namespace-as-ts-gra…
Mar 19, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions src/codegen/sdk/core/expressions/chained_attribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@

def __init__(self, ts_node, file_node_id, ctx, parent: Parent, object: TSNode, attribute: TSNode):
super().__init__(ts_node, file_node_id, ctx, parent=parent)
self._object = self._parse_expression(object, default=Name)

Check failure on line 41 in src/codegen/sdk/core/expressions/chained_attribute.py

View workflow job for this annotation

GitHub Actions / mypy

error: Incompatible types in assignment (expression has type "Expression[ChainedAttribute[Object, Attribute, Parent]]", variable has type "Object") [assignment]
if self.ctx.parser._should_log:
if not isinstance(self._object, Chainable):
msg = f"{self._object.__class__} is not chainable: {self._object.source}\nfile: {self.filepath}"
raise ValueError(msg)
self._attribute = self._parse_expression(attribute, default=Name)

Check failure on line 46 in src/codegen/sdk/core/expressions/chained_attribute.py

View workflow job for this annotation

GitHub Actions / mypy

error: Incompatible types in assignment (expression has type "Expression[ChainedAttribute[Object, Attribute, Parent]]", variable has type "Attribute") [assignment]
if self.ctx.parser._should_log:
if not isinstance(self._attribute, Resolvable):
msg = f"{self._attribute.__class__} is not resolvable: {self._attribute.source}\nfile: {self.filepath}"
Expand Down Expand Up @@ -89,12 +89,12 @@
"""
from codegen.sdk.core.detached_symbols.function_call import FunctionCall

ret = []

Check failure on line 92 in src/codegen/sdk/core/expressions/chained_attribute.py

View workflow job for this annotation

GitHub Actions / mypy

error: Need type annotation for "ret" (hint: "ret: list[<type>] = ...") [var-annotated]
curr = self

# Traverse backwards in code (children of tree node)
while isinstance(curr, ChainedAttribute):
curr = curr.object

Check failure on line 97 in src/codegen/sdk/core/expressions/chained_attribute.py

View workflow job for this annotation

GitHub Actions / mypy

error: Incompatible types in assignment (expression has type "Object", variable has type "ChainedAttribute[Object, Attribute, Parent]") [assignment]

if isinstance(curr, FunctionCall):
ret.insert(0, curr)
Expand Down Expand Up @@ -134,19 +134,16 @@
@noapidoc
@override
def _resolved_types(self) -> Generator[ResolutionStack[Self], None, None]:
from codegen.sdk.typescript.namespace import TSNamespace

if not self.ctx.config.method_usages:
return
if res := self.file.valid_import_names.get(self.full_name, None):
# Module imports
yield from self.with_resolution_frame(res)
return
# HACK: This is a hack to skip the resolved types for namespaces
if isinstance(self.object, TSNamespace):
return

for resolved_type in self.object.resolved_type_frames:
top = resolved_type.top

if not isinstance(top.node, HasAttribute):
generics: dict = resolved_type.generics.copy()
if top.node.source.lower() == "dict" and self.attribute.source in ("values", "get", "pop"):
Expand All @@ -160,14 +157,14 @@
yield from self.with_resolution_frame(attr, chained=True, generics=resolved_type.generics)
else:
self._log_parse("Couldn't resolve attribute %s on %s", name, top.node)
yield from self.with_resolution_frame(top.node, direct=resolved_type.is_direct_usage, chained=True)

Check failure on line 160 in src/codegen/sdk/core/expressions/chained_attribute.py

View workflow job for this annotation

GitHub Actions / mypy

error: Argument 1 to "with_resolution_frame" of "Chainable" has incompatible type "HasAttribute[Any]"; expected "Editable[Any]" [arg-type]

@noapidoc
@commiter
def _compute_dependencies(self, usage_type: UsageKind, dest: Optional["HasName | None"] = None) -> None:
edges = []
for used_frame in self.resolved_type_frames:
edges.extend(used_frame.get_edges(self, usage_type, dest, self.ctx))

Check failure on line 167 in src/codegen/sdk/core/expressions/chained_attribute.py

View workflow job for this annotation

GitHub Actions / mypy

error: "ResolutionStack[ChainedAttribute[Object, Attribute, Parent]]" has no attribute "get_edges" [attr-defined]
edges = list(dict.fromkeys(edges))
self.ctx.add_edges(edges)
if self.object.source not in ("self", "this"):
Expand Down
2 changes: 1 addition & 1 deletion src/codegen/sdk/python/import_resolution.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
from tree_sitter import Node as TSNode

from codegen.sdk.codebase.codebase_context import CodebaseContext
from codegen.sdk.core.file import SourceFile
from codegen.sdk.core.interfaces.editable import Editable
from codegen.sdk.core.interfaces.exportable import Exportable
from codegen.sdk.core.node_id_factory import NodeId
from codegen.sdk.core.statements.import_statement import ImportStatement
from codegen.sdk.python.file import PyFile
from src.codegen.sdk.core.file import SourceFile


logger = get_logger(__name__)
Expand Down
45 changes: 44 additions & 1 deletion src/codegen/sdk/typescript/import_resolution.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from codegen.sdk.core.expressions import Name
from codegen.sdk.core.import_resolution import Import, ImportResolution, WildcardImport
from codegen.sdk.core.interfaces.exportable import Exportable
from codegen.sdk.enums import ImportType, NodeType
from codegen.sdk.enums import ImportType, NodeType, SymbolType
from codegen.sdk.utils import find_all_descendants, find_first_ancestor, find_first_descendant
from codegen.shared.decorators.docs import noapidoc, ts_apidoc

Expand All @@ -24,6 +24,7 @@
from codegen.sdk.core.statements.import_statement import ImportStatement
from codegen.sdk.core.symbol import Symbol
from codegen.sdk.typescript.file import TSFile
from codegen.sdk.typescript.namespace import TSNamespace
from codegen.sdk.typescript.statements.import_statement import TSImportStatement


Expand Down Expand Up @@ -51,7 +52,7 @@
while call_node.parent and call_node.parent.type in ["await_expression", "parenthesized_expression"]:
call_node = call_node.parent
sibling = call_node.next_named_sibling
return sibling and sibling.type == "property_identifier"

Check failure on line 55 in src/codegen/sdk/typescript/import_resolution.py

View workflow job for this annotation

GitHub Actions / mypy

error: Incompatible return value type (got "bool | None", expected "bool") [return-value]
return False

@reader
Expand Down Expand Up @@ -122,7 +123,7 @@
return []

if not self.is_module_import():
return [] if self.imported_symbol.export is None else [self.imported_symbol.export]

Check failure on line 126 in src/codegen/sdk/typescript/import_resolution.py

View workflow job for this annotation

GitHub Actions / mypy

error: Item "Symbol[Any, Any]" of "Symbol[Any, Any] | ExternalModule | TSFile | Import[Any]" has no attribute "export" [union-attr]

Check failure on line 126 in src/codegen/sdk/typescript/import_resolution.py

View workflow job for this annotation

GitHub Actions / mypy

error: Item "ExternalModule" of "Symbol[Any, Any] | ExternalModule | TSFile | Import[Any]" has no attribute "export" [union-attr]

Check failure on line 126 in src/codegen/sdk/typescript/import_resolution.py

View workflow job for this annotation

GitHub Actions / mypy

error: Item "Import[Any]" of "Symbol[Any, Any] | ExternalModule | TSFile | Import[Any]" has no attribute "export" [union-attr]

from_file = self.imported_symbol
if from_file.node_type != NodeType.FILE:
Expand Down Expand Up @@ -578,6 +579,48 @@
return
yield from super().names

@property
def namespace_imports(self) -> list[TSNamespace]:
"""Returns any namespace objects imported by this import statement.

For example:
import * as MyNS from './mymodule';

Returns:
List of namespace objects imported
"""
if not self.is_namespace_import():
return []

from codegen.sdk.typescript.namespace import TSNamespace

resolved = self.resolved_symbol
if resolved is None or not isinstance(resolved, TSNamespace):
return []

return [resolved]

@property
def is_namespace_import(self) -> bool:
"""Returns True if this import is importing a namespace.

Examples:
import { MathUtils } from './file1'; # True if MathUtils is a namespace
import * as AllUtils from './utils'; # True
"""
# For wildcard imports with namespace alias
if self.import_type == ImportType.WILDCARD and self.namespace:
return True

# For named imports, check if any imported symbol is a namespace
if self.import_type == ImportType.NAMED_EXPORT:
for name, _ in self.names:
symbol = self.resolved_symbol
if symbol and symbol.symbol_type == SymbolType.Namespace:
return True

return False

@override
def set_import_module(self, new_module: str) -> None:
"""Sets the module of an import.
Expand Down
Loading