Coverage for src / rejuvenation / python_lst_example.py: 92%
40 statements
« prev ^ index » next coverage.py v7.13.4, created at 2026-09-09 14:04 +0000
« prev ^ index » next coverage.py v7.13.4, created at 2026-09-09 14:04 +0000
1import textwrap
3from renaissance.integrations.python.ast.factory import PythonFactory, PythonPatternFactory
4from renaissance.integrations.tree_sitter.lst import LSTNode
5from renaissance.integrations.types import Call
6from renaissance.syntax_tree import ASTRewriter, ASTShower
7from renaissance.syntax_tree.ast_finder import find_ast_type
8from renaissance.syntax_tree.match_finder import match_pattern
10example_code = """
11from module import foo, bar, baz, quux
12ba(51)
13na(52)
14na(53)
15pa(54)
16if pa():
17 ba()
18pa(54)
19"""
22def python_lst_smoke_test():
24 # adapter = TreeSitterAdapter(tree_sitter_python)
25 # tree = adapter.parse_code(code)
26 # lst = adapter.to_lst(code, tree)
28 factory = PythonFactory(LSTNode)
29 pattern_factory = PythonPatternFactory(factory)
31 atu = factory.create_from_text(example_code, "example.py")
33 pattern1 = pattern_factory.create_statement("if pa(): $$stmts")
34 pattern2 = pattern_factory.create_expression("na($a)")
36 print("_______________pattern 1____________________________________")
37 ASTShower.show_node(pattern1.node, include_properties=True)
38 print("_______________pattern 1____________________________________")
39 ASTShower.show_node(pattern2.node, include_properties=False)
40 print("_______________ast____________________________________")
41 ASTShower.focus = "ba"
42 ASTShower.show_node(atu)
44 print("_______________simple find____________________________________")
45 nodes = find_ast_type(atu, Call)
47 ASTShower.show_node(nodes[0])
49 pattern1replacement = textwrap.dedent("""
50 # changed if expr to const
51 isAOne=True
52 if(isAOne):
53 $$stmts
54 """)
55 pattern2replacement = "# changed function f1 to f2\nf2($a,123456)\n"
57 rewriter = ASTRewriter(atu)
59 for match in match_pattern(atu.children, [pattern1]):
60 refactor(match, pattern1replacement, rewriter)
62 for match in match_pattern(atu.children, [pattern2]):
63 refactor(match, pattern2replacement, rewriter)
65 return rewriter.apply_to_string()
68def refactor(match, replacement_text, rewriter):
69 for placeholder in match.expansions:
70 replacement_text = replacement_text.replace(placeholder, match[placeholder])
71 return rewriter.replace(replacement_text, match.nodes)
74if __name__ == "__main__":
75 result = python_lst_smoke_test()
76 print("_______________end result_________________________________")
77 print(result)