88from codegen .sdk .core .expressions .placeholder_type import PlaceholderType
99from codegen .sdk .core .expressions .value import Value
1010from codegen .sdk .core .statements .symbol_statement import SymbolStatement
11- from codegen .sdk .extensions .utils import find_all_descendants , find_first_descendant
12- from codegen .sdk .utils import find_first_function_descendant
11+ from codegen .sdk .utils import find_first_function_descendant , find_import_node
1312
1413if TYPE_CHECKING :
1514 from tree_sitter import Node as TSNode
@@ -81,42 +80,6 @@ def parse_expression(self, node: TSNode | None, file_node_id: NodeId, ctx: Codeb
8180 ret .children
8281 return ret
8382
84- def get_import_node (self , node : TSNode ) -> TSNode | None :
85- """Get the import node from a node that may contain an import.
86- Returns None if the node does not contain an import.
87-
88- Returns:
89- TSNode | None: The import_statement or call_expression node if it's an import, None otherwise
90- """
91- # Static imports
92- if node .type == "import_statement" :
93- return node
94-
95- # Dynamic imports and requires can be either:
96- # 1. Inside expression_statement -> call_expression
97- # 2. Direct call_expression
98-
99- # we only parse imports inside expressions and variable declarations
100- call_expression = find_first_descendant (node , ["call_expression" ])
101- if member_expression := find_first_descendant (node , ["member_expression" ]):
102- # there may be multiple call expressions (for cases such as import(a).then(module => module).then(module => module)
103- descendants = find_all_descendants (member_expression , ["call_expression" ])
104- if descendants :
105- import_node = descendants [- 1 ]
106- else :
107- # this means this is NOT a dynamic import()
108- return None
109- else :
110- import_node = call_expression
111-
112- # thus we only consider the deepest one
113- if import_node :
114- function = import_node .child_by_field_name ("function" )
115- if function and (function .type == "import" or (function .type == "identifier" and function .text .decode ("utf-8" ) == "require" )):
116- return import_node
117-
118- return None
119-
12083 def log_unparsed (self , node : TSNode ) -> None :
12184 if self ._should_log and node .is_named and node .type not in self ._uncovered_nodes :
12285 self ._uncovered_nodes .add (node .type )
@@ -172,7 +135,7 @@ def parse_ts_statements(self, node: TSNode, file_node_id: NodeId, ctx: CodebaseC
172135
173136 # =====[ Type Alias Declarations ]=====
174137 elif child .type == "type_alias_declaration" :
175- if import_node := self . get_import_node (child ):
138+ if import_node := find_import_node (child ):
176139 statements .append (TSImportStatement (import_node , file_node_id , ctx , parent , len (statements )))
177140 else :
178141 statements .append (SymbolStatement (child , file_node_id , ctx , parent , len (statements )))
@@ -185,11 +148,6 @@ def parse_ts_statements(self, node: TSNode, file_node_id: NodeId, ctx: CodebaseC
185148 elif child .type == "export_statement" or child .text .decode ("utf-8" ) == "export *;" :
186149 statements .append (ExportStatement (child , file_node_id , ctx , parent , len (statements )))
187150
188- # # =====[ Imports ] =====
189- # elif child.type == "import_statement":
190- # # statements.append(TSImportStatement(child, file_node_id, ctx, parent, len(statements)))
191- # pass # Temporarily opting to identify all imports using find_all_descendants
192-
193151 # =====[ Non-symbol statements ] =====
194152 elif child .type == "comment" :
195153 statements .append (TSComment .from_code_block (child , parent , pos = len (statements )))
@@ -210,7 +168,7 @@ def parse_ts_statements(self, node: TSNode, file_node_id: NodeId, ctx: CodebaseC
210168 elif child .type in ["lexical_declaration" , "variable_declaration" ]:
211169 if function_node := find_first_function_descendant (child ):
212170 statements .append (SymbolStatement (child , file_node_id , ctx , parent , len (statements ), function_node ))
213- elif import_node := self . get_import_node (child ):
171+ elif import_node := find_import_node (child ):
214172 statements .append (TSImportStatement (import_node , file_node_id , ctx , parent , len (statements )))
215173 else :
216174 statements .append (
@@ -221,7 +179,7 @@ def parse_ts_statements(self, node: TSNode, file_node_id: NodeId, ctx: CodebaseC
221179 elif child .type in ["public_field_definition" , "property_signature" , "enum_assignment" ]:
222180 statements .append (TSAttribute (child , file_node_id , ctx , parent , pos = len (statements )))
223181 elif child .type == "expression_statement" :
224- if import_node := self . get_import_node (child ):
182+ if import_node := find_import_node (child ):
225183 statements .append (TSImportStatement (import_node , file_node_id , ctx , parent , pos = len (statements )))
226184 continue
227185
0 commit comments