@@ -261,12 +261,17 @@ 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 ,
264265 ) -> tuple [NodeId , NodeId ]:
265266 # TODO: Prevent creation of import loops (!) - raise a ValueError and make the agent fix it
266267 # =====[ Arg checking ]=====
267268 if file == self .file :
268269 return file .file_node_id , self .node_id
269270
271+ if imp := file .get_import (self .name ):
272+ encountered_symbols .add (imp )
273+ imp .remove ()
274+
270275 # =====[ Move over dependencies recursively ]=====
271276 if include_dependencies :
272277 try :
@@ -319,7 +324,12 @@ def _move_to_file(
319324
320325 # =====[ Make a new symbol in the new file ]=====
321326 # This will update all edges etc.
322- file .add_symbol (self )
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 )
323333 import_line = self .get_import_string (module = file .import_module_name )
324334
325335 # =====[ Checks if symbol is used in original file ]=====
@@ -329,16 +339,18 @@ def _move_to_file(
329339 # ======[ Strategy: Duplicate Dependencies ]=====
330340 if strategy == "duplicate_dependencies" :
331341 # 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 )
332343 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 ):
333344 self .remove ()
334345
335346 # ======[ Strategy: Add Back Edge ]=====
336347 # Here, we will add a "back edge" to the old file importing the self
337348 elif strategy == "add_back_edge" :
338349 if is_used_in_file :
339- self . file . add_import ( import_line )
350+ back_edge_line = import_line
340351 if self .is_exported :
341- self .file .add_import (f"export {{ { self .name } }}" )
352+ back_edge_line = back_edge_line .replace ("import" , "export" )
353+ self .file .add_import (back_edge_line )
342354 elif self .is_exported :
343355 module_name = file .name
344356 self .file .add_import (f"export {{ { self .name } }} from '{ module_name } '" )
@@ -349,23 +361,26 @@ def _move_to_file(
349361 # Update the imports in all the files which use this symbol to get it from the new file now
350362 elif strategy == "update_all_imports" :
351363 for usage in self .usages :
352- if isinstance (usage .usage_symbol , TSImport ):
364+ if isinstance (usage .usage_symbol , TSImport ) and usage . usage_symbol . file != file :
353365 # Add updated import
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 ()
366+ usage .usage_symbol .file .add_import (import_line )
367+ usage .usage_symbol .remove ()
357368 elif usage .usage_type == UsageType .CHAINED :
358369 # Update all previous usages of import * to the new import name
359370 if usage .match and "." + self .name in usage .match :
360- if isinstance (usage .match , FunctionCall ):
371+ if isinstance (usage .match , FunctionCall ) and self . name in usage . match . get_name () :
361372 usage .match .get_name ().edit (self .name )
362373 if isinstance (usage .match , ChainedAttribute ):
363374 usage .match .edit (self .name )
364- usage .usage_symbol .file .add_import (import_line )
375+ usage .usage_symbol .file .add_import (imp = import_line )
376+
377+ # Add the import to the original file
365378 if is_used_in_file :
366- self .file .add_import (import_line )
379+ self .file .add_import (imp = import_line )
367380 # Delete the original symbol
368381 self .remove ()
382+ if cleanup_unused_imports :
383+ self ._post_move_import_cleanup (encountered_symbols , strategy )
369384
370385 def _convert_proptype_to_typescript (self , prop_type : Editable , param : Parameter | None , level : int ) -> str :
371386 """Converts a PropType definition to its TypeScript equivalent."""
0 commit comments