@@ -85,7 +85,7 @@ def parse_code(self, code: bytes, file_path: Path):
8585 root_node = tree .root_node
8686 codeFile = CodeFileModel (
8787 file_path = str (file_path ),
88- raw = self ._get_content (code , root_node , preserve_indentation = True )
88+ raw = self ._get_content (code , root_node )
8989 )
9090 self ._process_node (root_node , code , codeFile )
9191 return codeFile
@@ -175,7 +175,6 @@ def _process_import_node(cls, node: Node, code: bytes, codeFile: CodeFileModel):
175175 name = name ,
176176 alias = alias
177177 )
178- print (f"{ importStatement = } " )
179178
180179 codeFile .add_import (importStatement )
181180 cls ._generate_unique_import_id (codeFile .imports [- 1 ])
@@ -184,13 +183,13 @@ def _process_import_node(cls, node: Node, code: bytes, codeFile: CodeFileModel):
184183 def _process_class_node (cls , node : Node , code : bytes , codeFile : CodeFileModel ):
185184 class_name = None
186185 bases = []
187- raw = cls ._get_content (code , node , preserve_indentation = True )
186+ raw = cls ._get_content (code , node )
188187 for child in node .children :
189188 if child .type == "type_identifier" and class_name is None :
190189 class_name = cls ._get_content (code , child )
191- elif child .type == "heritage_clause " :
190+ elif child .type == "class_heritage " :
192191 for base_child in child .children :
193- if base_child .type == "expression_with_type_arguments " :
192+ if base_child .type == "extends_clause " :
194193 for expr_child in base_child .children :
195194 if expr_child .type == "identifier" :
196195 bases .append (cls ._get_content (code , expr_child ))
@@ -207,90 +206,89 @@ def _process_class_node(cls, node: Node, code: bytes, codeFile: CodeFileModel):
207206 def _process_class_body (cls , node : Node , code : bytes , codeFile : CodeFileModel ):
208207 for child in node .children :
209208 if child .type == "method_definition" :
210- cls ._process_method_definition (child , code , codeFile )
209+ cls ._process_function_definition (child , code , codeFile , is_method = True )
211210 elif child .type == "public_field_definition" :
212211 cls ._process_class_attribute (child , code , codeFile )
213212
214- @classmethod
215- def _process_method_definition (cls , node : Node , code : bytes , codeFile : CodeFileModel ):
216- method_name = None
217- signature = FunctionSignature ()
218- modifiers = []
219- decorators = []
220- raw = cls ._get_content (code , node , preserve_indentation = True )
221- for child in node .children :
222- if child .type == "property_identifier" and method_name is None :
223- method_name = cls ._get_content (code , child )
224- elif child .type == "formal_parameters" :
225- signature .parameters = cls ._process_parameters (child , code )
226- elif child .type == "type_annotation" :
227- signature .return_type = cls ._get_content (code , child )
228- elif child .type == "decorator" :
229- decorators .append (cls ._get_content (code , child ))
230- elif child .type == "public" :
231- modifiers .append ("public" )
232- elif child .type == "private" :
233- modifiers .append ("private" )
234- elif child .type == "protected" :
235- modifiers .append ("protected" )
236- elif child .type == "static" :
237- modifiers .append ("static" )
238- elif child .type == "async" :
239- modifiers .append ("async" )
240- codeFile .classes [- 1 ].add_method (MethodDefinition (
241- name = method_name ,
242- signature = signature ,
243- decorators = decorators ,
244- modifiers = modifiers ,
245- raw = raw
246- ))
247-
248213 @classmethod
249214 def _process_class_attribute (cls , node : Node , code : bytes , codeFile : CodeFileModel ):
250215 attribute = None
251216 type_hint = None
252217 value = None
253- raw = cls ._get_content (code , node , preserve_indentation = True )
218+ modifiers = []
219+ next_is_assignment = False
220+ raw = cls ._get_content (code , node )
254221 for child in node .children :
255222 if child .type == "property_identifier" and attribute is None :
256223 attribute = cls ._get_content (code , child )
257224 elif child .type == "type_annotation" :
258- type_hint = cls ._get_content (code , child )
225+ type_hint = cls ._get_content (code , child ).replace (": " , "" )
226+ elif child .type == "accessibility_modifier" :
227+ modifiers .append (cls ._get_content (code , child ))
228+ elif child .type == "=" :
229+ next_is_assignment = True
230+ elif next_is_assignment :
231+ value = cls ._get_content (code , child )
232+ next_is_assignment = False
259233 elif child .type == "assignment_expression" :
260234 for assign_child in child .children :
261235 if assign_child .type == "expression" :
262236 value = cls ._get_content (code , assign_child )
263237 codeFile .classes [- 1 ].add_attribute (ClassAttribute (
264238 name = attribute ,
265239 type_hint = type_hint ,
240+ modifiers = modifiers ,
266241 value = value ,
267242 raw = raw
268243 ))
269244
270245 @classmethod
271- def _process_function_definition (cls , node : Node , code : bytes , codeFile : CodeFileModel ):
246+ def _process_function_definition (cls , node : Node , code : bytes , codeFile : CodeFileModel , is_method : bool = False ):
272247 definition = None
273248 signature = FunctionSignature ()
274249 modifiers = []
275250 decorators = []
276- raw = cls ._get_content (code , node , preserve_indentation = True )
251+ raw = cls ._get_content (code , node )
277252 for child in node .children :
278- print (f"{ child .type = } , { cls ._get_content (code , child )} " )
279253 if child .type == "identifier" and definition is None :
280254 definition = cls ._get_content (code , child )
255+ elif child .type == "property_identifier" and definition is None :
256+ definition = cls ._get_content (code , child )
281257 elif child .type == "formal_parameters" :
282258 signature .parameters = cls ._process_parameters (child , code )
283259 elif child .type == "type_annotation" :
284- signature .return_type = cls ._get_content (code , child )
260+ signature .return_type = cls ._get_content (code , child ). replace ( ": " , "" )
285261 elif child .type == "async" :
286262 modifiers .append ("async" )
287- codeFile .add_function (FunctionDefinition (
288- name = definition ,
289- signature = signature ,
290- decorators = decorators ,
291- modifiers = modifiers ,
292- raw = raw
293- ))
263+ elif child .type == "decorator" :
264+ decorators .append (cls ._get_content (code , child ))
265+ elif child .type == "public" :
266+ modifiers .append ("public" )
267+ elif child .type == "private" :
268+ modifiers .append ("private" )
269+ elif child .type == "protected" :
270+ modifiers .append ("protected" )
271+ elif child .type == "static" :
272+ modifiers .append ("static" )
273+ elif child .type == "async" :
274+ modifiers .append ("async" )
275+ if not is_method :
276+ codeFile .add_function (FunctionDefinition (
277+ name = definition ,
278+ signature = signature ,
279+ decorators = decorators ,
280+ modifiers = modifiers ,
281+ raw = raw
282+ ))
283+ else :
284+ codeFile .classes [- 1 ].add_method (MethodDefinition (
285+ name = definition ,
286+ signature = signature ,
287+ decorators = decorators ,
288+ modifiers = modifiers ,
289+ raw = raw
290+ ))
291+
294292
295293 @classmethod
296294 def _process_variable_declaration (cls , node : Node , code : bytes , codeFile : CodeFileModel ):
0 commit comments