8
8
from codegen .sdk .core .expressions .placeholder_type import PlaceholderType
9
9
from codegen .sdk .core .expressions .value import Value
10
10
from 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
13
12
14
13
if TYPE_CHECKING :
15
14
from tree_sitter import Node as TSNode
@@ -81,42 +80,6 @@ def parse_expression(self, node: TSNode | None, file_node_id: NodeId, ctx: Codeb
81
80
ret .children
82
81
return ret
83
82
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
-
120
83
def log_unparsed (self , node : TSNode ) -> None :
121
84
if self ._should_log and node .is_named and node .type not in self ._uncovered_nodes :
122
85
self ._uncovered_nodes .add (node .type )
@@ -172,7 +135,7 @@ def parse_ts_statements(self, node: TSNode, file_node_id: NodeId, ctx: CodebaseC
172
135
173
136
# =====[ Type Alias Declarations ]=====
174
137
elif child .type == "type_alias_declaration" :
175
- if import_node := self . get_import_node (child ):
138
+ if import_node := find_import_node (child ):
176
139
statements .append (TSImportStatement (import_node , file_node_id , ctx , parent , len (statements )))
177
140
else :
178
141
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
185
148
elif child .type == "export_statement" or child .text .decode ("utf-8" ) == "export *;" :
186
149
statements .append (ExportStatement (child , file_node_id , ctx , parent , len (statements )))
187
150
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
-
193
151
# =====[ Non-symbol statements ] =====
194
152
elif child .type == "comment" :
195
153
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
210
168
elif child .type in ["lexical_declaration" , "variable_declaration" ]:
211
169
if function_node := find_first_function_descendant (child ):
212
170
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 ):
214
172
statements .append (TSImportStatement (import_node , file_node_id , ctx , parent , len (statements )))
215
173
else :
216
174
statements .append (
@@ -221,7 +179,7 @@ def parse_ts_statements(self, node: TSNode, file_node_id: NodeId, ctx: CodebaseC
221
179
elif child .type in ["public_field_definition" , "property_signature" , "enum_assignment" ]:
222
180
statements .append (TSAttribute (child , file_node_id , ctx , parent , pos = len (statements )))
223
181
elif child .type == "expression_statement" :
224
- if import_node := self . get_import_node (child ):
182
+ if import_node := find_import_node (child ):
225
183
statements .append (TSImportStatement (import_node , file_node_id , ctx , parent , pos = len (statements )))
226
184
continue
227
185
0 commit comments