Coverage for src/renaissance/recipes/cleanup_refactoring.py: 100%
13 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-10-15 10:34 +0000
« prev ^ index » next coverage.py v7.16.2, created at 2026-10-15 10:34 +0000
1# Copyright (C) 2024-2026 TNO
2#
3# Licensed under the terms specified in the LICENSE file in the root directory
4# of this repository, also available at:
5# https://github.com/TNO/Renaissance.Py/blob/main/LICENSE
7"""AI: Recipe steps for cleaning up AST content, such as removing unused variables."""
9from more_itertools import flatten
11from renaissance.syntax_tree import ASTProcessor
12from renaissance.syntax_tree.ast_finder import find_semantic_kind
13from renaissance.syntax_tree.semantic_kind import SemanticKind
16class CleanupRefactoring:
17 """AI: Static-only namespace exposing AST cleanup recipe steps (e.g. removing unused variables)."""
19 def __init__(self) -> None:
20 """AI: Prevent instantiation; this class only exposes static cleanup recipe steps."""
21 raise Exception("This class should not be instantiated")
23 @staticmethod
24 def remove_unused_variables(ast_refactor: ASTProcessor) -> None:
25 """Remove all unused variables from a function."""
26 refs = flatten(
27 find_semantic_kind(n, SemanticKind.DECLARATION) for n in find_semantic_kind(ast_refactor.node, SemanticKind.STATEMENT)
28 )
29 unused_parents = {id(ref.parent): ref.parent for ref in refs if not ref.referenced_by}
30 for parent in unused_parents.values():
31 ast_refactor.remove(parent, True, True)