py-ast - v1.16.0
    Preparing search index...

    Class NodeTransformer

    A NodeVisitor subclass that can rewrite an AST while walking it, mirroring Python's ast.NodeTransformer.

    Like NodeVisitor, subclasses define visit<NodeType>/visit_<NodeType> methods for the node types they want to transform. A visitor method should return: the (possibly modified) node to replace the original with, an array of nodes to splice in its place, or null/undefined to drop the node entirely. Node types without a matching method fall back to NodeTransformer.genericVisit, which shallow-clones the node and recursively transforms its children, leaving nodes without children unchanged.

    class ConstantFolder extends NodeTransformer {
    visitBinOp(node: BinOp) {
    return this.genericVisit(node);
    }
    }

    const transformed = new ConstantFolder().visit(moduleNode);

    Hierarchy (View Summary)

    Index
    • Dispatches a node to its specific visit<NodeType>/visit_<NodeType> method if one is defined on the instance, falling back to NodeVisitor.genericVisit otherwise.

      Parameters

      Returns any

      Whatever the matched visitor method (or genericVisit) returns; the base implementation imposes no fixed return type since subclasses may return arbitrary values from their visit methods.

    • Default transform behavior used when a subclass has not defined a visit<NodeType>/visit_<NodeType> method for node's type: it shallow-clones node and replaces each child (found by scanning own properties for arrays/objects that look like AST nodes) with the result of visiting it via NodeVisitor.visit. Array children whose visited result is itself an array are spliced in flattened; null/undefined results are dropped from arrays.

      Parameters

      Returns ASTNodeUnion

      A new node of the same shape as node with its children replaced by their transformed results.