Coverage for src/renaissance/recipes/cleanup_refactoring.py: 100%

13 statements  

« prev     ^ index     » next       coverage.py v7.16.1, created at 2026-10-02 13:12 +0000

1"""AI: Recipe steps for cleaning up AST content, such as removing unused variables.""" 

2 

3from more_itertools import flatten 

4 

5from renaissance.syntax_tree import ASTProcessor 

6from renaissance.syntax_tree.ast_finder import find_semantic_kind 

7from renaissance.syntax_tree.semantic_kind import SemanticKind 

8 

9 

10class CleanupRefactoring: 

11 """AI: Static-only namespace exposing AST cleanup recipe steps (e.g. removing unused variables).""" 

12 

13 def __init__(self): 

14 """AI: Prevent instantiation; this class only exposes static cleanup recipe steps.""" 

15 raise Exception("This class should not be instantiated") 

16 

17 @staticmethod 

18 def remove_unused_variables(ast_refactor: ASTProcessor) -> None: 

19 """Remove all unused variables from a function.""" 

20 refs = flatten( 

21 find_semantic_kind(n, SemanticKind.DECLARATION) for n in find_semantic_kind(ast_refactor.node, SemanticKind.STATEMENT) 

22 ) 

23 unused_parents = {id(ref.parent): ref.parent for ref in refs if not ref.referenced_by} 

24 for parent in unused_parents.values(): 

25 ast_refactor.remove(parent, True, True)