How do I remove a node in a ast tree using esprima in python?

161 Views Asked by At

How can I remove the nodes in a ast tree using esprima? Here is my code:

import esprima

class MyVisitor(esprima.NodeVisitor):

    def visit_ExpressionStatement(self, node):
        return None


visitor = MyVisitor()

tree = esprima.parse("""
alert('first alert');
function foo() {
    var i2= 20;
    alert('foo function');
}
""")

visitor.visit(tree)

print(tree)

I want to remove all ExpressionStatement nodes from this ast tree but I am not sure how to do it. Please help. Thank you!

0

There are 0 best solutions below