class Prism::Visitor

Visitor 是一个类,它为节点上定义的每个 accept 方法提供了默认实现。这意味着它可以遍历树,而无需调用者定义任何特殊处理。这允许您处理树的一部分,同时仍然遍历整个树。

例如,要查找所有调用 foo 方法的方法调用,您可以编写

class FooCalls < Prism::Visitor
  def visit_call_node(node)
    if node.name == :foo
      # Do something with the node
    end

    # Call super so that the visitor continues walking the tree
    super
  end
end