@@ -261,17 +261,12 @@ def _move_to_file(
261
261
encountered_symbols : set [Symbol | Import ],
262
262
include_dependencies : bool = True ,
263
263
strategy : Literal ["add_back_edge" , "update_all_imports" , "duplicate_dependencies" ] = "update_all_imports" ,
264
- cleanup_unused_imports : bool = True ,
265
264
) -> tuple [NodeId , NodeId ]:
266
265
# TODO: Prevent creation of import loops (!) - raise a ValueError and make the agent fix it
267
266
# =====[ Arg checking ]=====
268
267
if file == self .file :
269
268
return file .file_node_id , self .node_id
270
269
271
- if imp := file .get_import (self .name ):
272
- encountered_symbols .add (imp )
273
- imp .remove ()
274
-
275
270
# =====[ Move over dependencies recursively ]=====
276
271
if include_dependencies :
277
272
try :
@@ -324,12 +319,7 @@ def _move_to_file(
324
319
325
320
# =====[ Make a new symbol in the new file ]=====
326
321
# 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 )
333
323
import_line = self .get_import_string (module = file .import_module_name )
334
324
335
325
# =====[ Checks if symbol is used in original file ]=====
@@ -339,18 +329,16 @@ def _move_to_file(
339
329
# ======[ Strategy: Duplicate Dependencies ]=====
340
330
if strategy == "duplicate_dependencies" :
341
331
# 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 )
343
332
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 ):
344
333
self .remove ()
345
334
346
335
# ======[ Strategy: Add Back Edge ]=====
347
336
# Here, we will add a "back edge" to the old file importing the self
348
337
elif strategy == "add_back_edge" :
349
338
if is_used_in_file :
350
- back_edge_line = import_line
339
+ self . file . add_import ( import_line )
351
340
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 } }}" )
354
342
elif self .is_exported :
355
343
module_name = file .name
356
344
self .file .add_import (f"export {{ { self .name } }} from '{ module_name } '" )
@@ -361,26 +349,23 @@ def _move_to_file(
361
349
# Update the imports in all the files which use this symbol to get it from the new file now
362
350
elif strategy == "update_all_imports" :
363
351
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 ):
365
353
# 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 ()
368
357
elif usage .usage_type == UsageType .CHAINED :
369
358
# Update all previous usages of import * to the new import name
370
359
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 ):
372
361
usage .match .get_name ().edit (self .name )
373
362
if isinstance (usage .match , ChainedAttribute ):
374
363
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 )
378
365
if is_used_in_file :
379
- self .file .add_import (imp = import_line )
366
+ self .file .add_import (import_line )
380
367
# Delete the original symbol
381
368
self .remove ()
382
- if cleanup_unused_imports :
383
- self ._post_move_import_cleanup (encountered_symbols , strategy )
384
369
385
370
def _convert_proptype_to_typescript (self , prop_type : Editable , param : Parameter | None , level : int ) -> str :
386
371
"""Converts a PropType definition to its TypeScript equivalent."""
0 commit comments