@@ -261,17 +261,12 @@ def _move_to_file(
261261 encountered_symbols : set [Symbol | Import ],
262262 include_dependencies : bool = True ,
263263 strategy : Literal ["add_back_edge" , "update_all_imports" , "duplicate_dependencies" ] = "update_all_imports" ,
264- cleanup_unused_imports : bool = True ,
265264 ) -> tuple [NodeId , NodeId ]:
266265 # TODO: Prevent creation of import loops (!) - raise a ValueError and make the agent fix it
267266 # =====[ Arg checking ]=====
268267 if file == self .file :
269268 return file .file_node_id , self .node_id
270269
271- if imp := file .get_import (self .name ):
272- encountered_symbols .add (imp )
273- imp .remove ()
274-
275270 # =====[ Move over dependencies recursively ]=====
276271 if include_dependencies :
277272 try :
@@ -324,12 +319,7 @@ def _move_to_file(
324319
325320 # =====[ Make a new symbol in the new file ]=====
326321 # This will update all edges etc.
327- should_export = False
328-
329- if self .is_exported or [usage for usage in self .usages if usage .usage_symbol not in encountered_symbols and not usage .usage_symbol .get_transaction_if_pending_removal ()]:
330- should_export = True
331-
332- file .add_symbol (self , should_export = should_export )
322+ file .add_symbol (self )
333323 import_line = self .get_import_string (module = file .import_module_name )
334324
335325 # =====[ Checks if symbol is used in original file ]=====
@@ -339,18 +329,16 @@ def _move_to_file(
339329 # ======[ Strategy: Duplicate Dependencies ]=====
340330 if strategy == "duplicate_dependencies" :
341331 # If not used in the original file. or if not imported from elsewhere, we can just remove the original symbol
342- is_used_in_file = any (usage .file == self .file and usage .node_type == NodeType .SYMBOL for usage in self .symbol_usages )
343332 if not is_used_in_file and not any (usage .kind is UsageKind .IMPORTED and usage .usage_symbol not in encountered_symbols for usage in self .usages ):
344333 self .remove ()
345334
346335 # ======[ Strategy: Add Back Edge ]=====
347336 # Here, we will add a "back edge" to the old file importing the self
348337 elif strategy == "add_back_edge" :
349338 if is_used_in_file :
350- back_edge_line = import_line
339+ self . file . add_import ( import_line )
351340 if self .is_exported :
352- back_edge_line = back_edge_line .replace ("import" , "export" )
353- self .file .add_import (back_edge_line )
341+ self .file .add_import (f"export {{ { self .name } }}" )
354342 elif self .is_exported :
355343 module_name = file .name
356344 self .file .add_import (f"export {{ { self .name } }} from '{ module_name } '" )
@@ -361,26 +349,23 @@ def _move_to_file(
361349 # Update the imports in all the files which use this symbol to get it from the new file now
362350 elif strategy == "update_all_imports" :
363351 for usage in self .usages :
364- if isinstance (usage .usage_symbol , TSImport ) and usage . usage_symbol . file != file :
352+ if isinstance (usage .usage_symbol , TSImport ):
365353 # Add updated import
366- usage .usage_symbol .file .add_import (import_line )
367- usage .usage_symbol .remove ()
354+ if usage .usage_symbol .resolved_symbol is not None and usage .usage_symbol .resolved_symbol .node_type == NodeType .SYMBOL and usage .usage_symbol .resolved_symbol == self :
355+ usage .usage_symbol .file .add_import (import_line )
356+ usage .usage_symbol .remove ()
368357 elif usage .usage_type == UsageType .CHAINED :
369358 # Update all previous usages of import * to the new import name
370359 if usage .match and "." + self .name in usage .match :
371- if isinstance (usage .match , FunctionCall ) and self . name in usage . match . get_name () :
360+ if isinstance (usage .match , FunctionCall ):
372361 usage .match .get_name ().edit (self .name )
373362 if isinstance (usage .match , ChainedAttribute ):
374363 usage .match .edit (self .name )
375- usage .usage_symbol .file .add_import (imp = import_line )
376-
377- # Add the import to the original file
364+ usage .usage_symbol .file .add_import (import_line )
378365 if is_used_in_file :
379- self .file .add_import (imp = import_line )
366+ self .file .add_import (import_line )
380367 # Delete the original symbol
381368 self .remove ()
382- if cleanup_unused_imports :
383- self ._post_move_import_cleanup (encountered_symbols , strategy )
384369
385370 def _convert_proptype_to_typescript (self , prop_type : Editable , param : Parameter | None , level : int ) -> str :
386371 """Converts a PropType definition to its TypeScript equivalent."""
0 commit comments