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 .utils import find_first_function_descendant
11+ from codegen .sdk .utils import find_first_function_descendant , find_import_node
1212
1313if TYPE_CHECKING :
1414 from tree_sitter import Node as TSNode
@@ -108,6 +108,7 @@ def parse_ts_statements(self, node: TSNode, file_node_id: NodeId, ctx: CodebaseC
108108 from codegen .sdk .typescript .statements .comment import TSComment
109109 from codegen .sdk .typescript .statements .for_loop_statement import TSForLoopStatement
110110 from codegen .sdk .typescript .statements .if_block_statement import TSIfBlockStatement
111+ from codegen .sdk .typescript .statements .import_statement import TSImportStatement
111112 from codegen .sdk .typescript .statements .labeled_statement import TSLabeledStatement
112113 from codegen .sdk .typescript .statements .switch_statement import TSSwitchStatement
113114 from codegen .sdk .typescript .statements .try_catch_statement import TSTryCatchStatement
@@ -117,11 +118,13 @@ def parse_ts_statements(self, node: TSNode, file_node_id: NodeId, ctx: CodebaseC
117118
118119 if node .type in self .expressions or node .type == "expression_statement" :
119120 return [ExpressionStatement (node , file_node_id , ctx , parent , 0 , expression_node = node )]
121+
120122 for child in node .named_children :
121123 # =====[ Functions + Methods ]=====
122124 if child .type in _VALID_TYPE_NAMES :
123125 statements .append (SymbolStatement (child , file_node_id , ctx , parent , len (statements )))
124-
126+ elif child .type == "import_statement" :
127+ statements .append (TSImportStatement (child , file_node_id , ctx , parent , len (statements )))
125128 # =====[ Classes ]=====
126129 elif child .type in ("class_declaration" , "abstract_class_declaration" ):
127130 statements .append (SymbolStatement (child , file_node_id , ctx , parent , len (statements )))
@@ -132,7 +135,10 @@ def parse_ts_statements(self, node: TSNode, file_node_id: NodeId, ctx: CodebaseC
132135
133136 # =====[ Type Alias Declarations ]=====
134137 elif child .type == "type_alias_declaration" :
135- statements .append (SymbolStatement (child , file_node_id , ctx , parent , len (statements )))
138+ if import_node := find_import_node (child ):
139+ statements .append (TSImportStatement (child , file_node_id , ctx , parent , len (statements ), source_node = import_node ))
140+ else :
141+ statements .append (SymbolStatement (child , file_node_id , ctx , parent , len (statements )))
136142
137143 # =====[ Enum Declarations ]=====
138144 elif child .type == "enum_declaration" :
@@ -142,11 +148,6 @@ def parse_ts_statements(self, node: TSNode, file_node_id: NodeId, ctx: CodebaseC
142148 elif child .type == "export_statement" or child .text .decode ("utf-8" ) == "export *;" :
143149 statements .append (ExportStatement (child , file_node_id , ctx , parent , len (statements )))
144150
145- # =====[ Imports ] =====
146- elif child .type == "import_statement" :
147- # statements.append(TSImportStatement(child, file_node_id, ctx, parent, len(statements)))
148- pass # Temporarily opting to identify all imports using find_all_descendants
149-
150151 # =====[ Non-symbol statements ] =====
151152 elif child .type == "comment" :
152153 statements .append (TSComment .from_code_block (child , parent , pos = len (statements )))
@@ -167,6 +168,8 @@ def parse_ts_statements(self, node: TSNode, file_node_id: NodeId, ctx: CodebaseC
167168 elif child .type in ["lexical_declaration" , "variable_declaration" ]:
168169 if function_node := find_first_function_descendant (child ):
169170 statements .append (SymbolStatement (child , file_node_id , ctx , parent , len (statements ), function_node ))
171+ elif import_node := find_import_node (child ):
172+ statements .append (TSImportStatement (child , file_node_id , ctx , parent , len (statements ), source_node = import_node ))
170173 else :
171174 statements .append (
172175 TSAssignmentStatement .from_assignment (
@@ -176,6 +179,10 @@ def parse_ts_statements(self, node: TSNode, file_node_id: NodeId, ctx: CodebaseC
176179 elif child .type in ["public_field_definition" , "property_signature" , "enum_assignment" ]:
177180 statements .append (TSAttribute (child , file_node_id , ctx , parent , pos = len (statements )))
178181 elif child .type == "expression_statement" :
182+ if import_node := find_import_node (child ):
183+ statements .append (TSImportStatement (child , file_node_id , ctx , parent , pos = len (statements ), source_node = import_node ))
184+ continue
185+
179186 for var in child .named_children :
180187 if var .type == "string" :
181188 statements .append (TSComment .from_code_block (var , parent , pos = len (statements )))
@@ -185,7 +192,6 @@ def parse_ts_statements(self, node: TSNode, file_node_id: NodeId, ctx: CodebaseC
185192 statements .append (ExpressionStatement (child , file_node_id , ctx , parent , pos = len (statements ), expression_node = var ))
186193 elif child .type in self .expressions :
187194 statements .append (ExpressionStatement (child , file_node_id , ctx , parent , len (statements ), expression_node = child ))
188-
189195 else :
190196 self .log ("Couldn't parse statement with type: %s" , child .type )
191197 statements .append (Statement .from_code_block (child , parent , pos = len (statements )))
@@ -204,6 +210,7 @@ def parse_py_statements(self, node: TSNode, file_node_id: NodeId, ctx: CodebaseC
204210 from codegen .sdk .python .statements .comment import PyComment
205211 from codegen .sdk .python .statements .for_loop_statement import PyForLoopStatement
206212 from codegen .sdk .python .statements .if_block_statement import PyIfBlockStatement
213+ from codegen .sdk .python .statements .import_statement import PyImportStatement
207214 from codegen .sdk .python .statements .match_statement import PyMatchStatement
208215 from codegen .sdk .python .statements .pass_statement import PyPassStatement
209216 from codegen .sdk .python .statements .try_catch_statement import PyTryCatchStatement
@@ -237,9 +244,7 @@ def parse_py_statements(self, node: TSNode, file_node_id: NodeId, ctx: CodebaseC
237244
238245 # =====[ Imports ] =====
239246 elif child .type in ["import_statement" , "import_from_statement" , "future_import_statement" ]:
240- # statements.append(PyImportStatement(child, file_node_id, ctx, parent, len(statements)))
241- pass # Temporarily opting to identify all imports using find_all_descendants
242-
247+ statements .append (PyImportStatement (child , file_node_id , ctx , parent , len (statements )))
243248 # =====[ Non-symbol statements ] =====
244249 elif child .type == "comment" :
245250 statements .append (PyComment .from_code_block (child , parent , pos = len (statements )))
0 commit comments