Refactor
Simple, hassle-free, dependency-free, AST based fragmental source code refactoring
and transformation toolkit.
Why?
Our framework is primarily built on the principle of "simple but effective
transformations". We focus on refactorings that target a small span of
source code, and work our way out from it. What this enables for us is
being able to operate directly on a single format for both analyses and
transformations. This is what we shine at compared to other similar tools.
How?
Let's not get into too much details, but just to give a sneak peek we
can try to write a rule that would replace the identifier placeholder
with 42
.
import ast
from refactor import Rule, Replace, run
class FillPlaceholders(Rule):
def match(self, node: ast.AST) -> Replace:
assert isinstance(node, ast.Name)
assert node.id == "placeholder"
replacement = ast.Constant(42)
return Replace(node, replacement)
if __name__ == "__main__":
run(rules=[FillPlaceholders])
If we run the rule above on a file, we can see how it performs:
@@ -1,11 +1,11 @@
def main():
- print(placeholder * 3 + 2)
+ print(42 * 3 + 2)
- print(2 + placeholder + 3)
+ print(2 + 42 + 3)
# some comments
- placeholder # maybe other comments
+ 42 # maybe other comments
if something:
other_thing
- print(placeholder)
+ print(42)
if __name__ == "__main__":
main()
For learning more, check our documentation out!