Add AST tree replacement class
Support for replacing the current node during Visitor traversal.
While using it, I wanted to edit the tree to deobfuscate Lua code, but it seems editing functionality is not supported—only traversal is allowed. Of course, it's also possible that I'm not using it correctly. If that's the case, please close this issue. Currently, the code runs well in my project, though I’m not sure if there are any unknown bugs.
class NumberVisitor(ast.ASTReplaceVisitor):
def visit_Number(self, node):
# Replace all Number types with a Number type of value 123. Of course, other types can be returned here, or conditional filtering can be applied.
return ast.Number(123)
# If it is not necessary to replace the current one
# return None
if key == 'values' or key == 'args':
obj_list = getattr(parent, key, None)
The determination of types appears to be incomplete; let's wait until it's corrected before proceeding.
Thanks I'm looking how I can integrate this feature in a more generic way into the existing visitor, I'll keep you updated. But for now you can use your implem directly in your code there is no blocker right?
Thank you very much! The code is currently working fine in my project without any blocking issues. Looking forward to hearing good news. Additionally, I have resolved the issue regarding the incomplete type matching and have already updated it.