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

    Class NodeVisitor

    Base class for writing tree-walking visitors over an AST, mirroring Python's ast.NodeVisitor.

    Subclasses define visit<NodeType> (or visit_<NodeType>) methods for the node types they care about, e.g. visitFunctionDef for FunctionDef nodes. Any node type without a matching method falls back to NodeVisitor.genericVisit, which simply recurses into the node's children without doing anything else. Visitor methods are responsible for calling NodeVisitor.visit (or genericVisit) themselves if they want traversal to continue into a node's children.

    class NameCollector extends NodeVisitor {
    names: string[] = [];

    visitName(node: Name) {
    this.names.push(node.id);
    }
    }

    const collector = new NameCollector();
    collector.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 visit behavior used when a subclass has not defined a visit<NodeType>/visit_<NodeType> method for node's type: it recurses into each child node (found by scanning own properties for arrays/objects that look like AST nodes) by calling NodeVisitor.visit on them, without collecting or returning any results.

      Parameters

      • node: ASTNodeUnion

        The AST node whose children should be visited.

      Returns void