The dispatcher class fires events for nodes that are found while walking an AST to all registered listeners. It’s useful for performing different types of analysis on the AST while only having to walk the tree once.

To use the dispatcher, you would first instantiate it and register listeners for the events you’re interested in:

class OctalListener
  def on_integer_node_enter(node)
    if node.octal? && !node.slice.start_with?("0o")
      warn("Octal integers should be written with the 0o prefix")
    end
  end
end

dispatcher = Dispatcher.new
dispatcher.register(listener, :on_integer_node_enter)

Then, you can walk any number of trees and dispatch events to the listeners:

result = Prism.parse("001 + 002 + 003")
dispatcher.dispatch(result.value)

Optionally, you can also use ‘#dispatch_once` to dispatch enter and leave events for a single node without recursing further down the tree. This can be useful in circumstances where you want to reuse the listeners you already have registers but want to stop walking the tree at a certain point.

integer = result.value.statements.body.first.receiver.receiver
dispatcher.dispatch_once(integer)
Attributes
Read

attr_reader listeners: Hash[Symbol, Array]

Class Methods

Initialize a new dispatcher.

Instance Methods

Dispatches a single event for ‘node` to all registered listeners.

def dispatch_once: (Node) -> void

Register a listener for one or more events.

def register: (Listener, *Symbol) -> void

Dispatch enter and leave events for AliasGlobalVariableNode nodes and continue walking the tree.

Dispatch enter and leave events for AliasMethodNode nodes and continue walking the tree.

Dispatch enter and leave events for AlternationPatternNode nodes and continue walking the tree.

Dispatch enter and leave events for AndNode nodes and continue walking the tree.

Dispatch enter and leave events for ArgumentsNode nodes and continue walking the tree.

Dispatch enter and leave events for ArrayNode nodes and continue walking the tree.

Dispatch enter and leave events for ArrayPatternNode nodes and continue walking the tree.

Dispatch enter and leave events for AssocNode nodes and continue walking the tree.

Dispatch enter and leave events for AssocSplatNode nodes and continue walking the tree.

Dispatch enter and leave events for BackReferenceReadNode nodes and continue walking the tree.

Dispatch enter and leave events for BeginNode nodes and continue walking the tree.

Dispatch enter and leave events for BlockArgumentNode nodes and continue walking the tree.

Dispatch enter and leave events for BlockLocalVariableNode nodes and continue walking the tree.

Dispatch enter and leave events for BlockNode nodes and continue walking the tree.

Dispatch enter and leave events for BlockParameterNode nodes and continue walking the tree.

Dispatch enter and leave events for BlockParametersNode nodes and continue walking the tree.

Dispatch enter and leave events for BreakNode nodes and continue walking the tree.

Dispatch enter and leave events for CallAndWriteNode nodes and continue walking the tree.

Dispatch enter and leave events for CallNode nodes and continue walking the tree.

Dispatch enter and leave events for CallOperatorWriteNode nodes and continue walking the tree.

Dispatch enter and leave events for CallOrWriteNode nodes and continue walking the tree.

Dispatch enter and leave events for CallTargetNode nodes and continue walking the tree.

Dispatch enter and leave events for CapturePatternNode nodes and continue walking the tree.

Dispatch enter and leave events for CaseMatchNode nodes and continue walking the tree.

Dispatch enter and leave events for CaseNode nodes and continue walking the tree.

Dispatch enter and leave events for ClassNode nodes and continue walking the tree.

Dispatch enter and leave events for ClassVariableAndWriteNode nodes and continue walking the tree.

Dispatch enter and leave events for ClassVariableOperatorWriteNode nodes and continue walking the tree.

Dispatch enter and leave events for ClassVariableOrWriteNode nodes and continue walking the tree.

Dispatch enter and leave events for ClassVariableReadNode nodes and continue walking the tree.

Dispatch enter and leave events for ClassVariableTargetNode nodes and continue walking the tree.

Dispatch enter and leave events for ClassVariableWriteNode nodes and continue walking the tree.

Dispatch enter and leave events for ConstantAndWriteNode nodes and continue walking the tree.

Dispatch enter and leave events for ConstantOperatorWriteNode nodes and continue walking the tree.

Dispatch enter and leave events for ConstantOrWriteNode nodes and continue walking the tree.

Dispatch enter and leave events for ConstantPathAndWriteNode nodes and continue walking the tree.

Dispatch enter and leave events for ConstantPathNode nodes and continue walking the tree.

Dispatch enter and leave events for ConstantPathOperatorWriteNode nodes and continue walking the tree.

Dispatch enter and leave events for ConstantPathOrWriteNode nodes and continue walking the tree.

Dispatch enter and leave events for ConstantPathTargetNode nodes and continue walking the tree.

Dispatch enter and leave events for ConstantPathWriteNode nodes and continue walking the tree.

Dispatch enter and leave events for ConstantReadNode nodes and continue walking the tree.

Dispatch enter and leave events for ConstantTargetNode nodes and continue walking the tree.

Dispatch enter and leave events for ConstantWriteNode nodes and continue walking the tree.

Dispatch enter and leave events for DefNode nodes and continue walking the tree.

Dispatch enter and leave events for DefinedNode nodes and continue walking the tree.

Dispatch enter and leave events for ElseNode nodes and continue walking the tree.

Dispatch enter and leave events for EmbeddedStatementsNode nodes and continue walking the tree.

Dispatch enter and leave events for EmbeddedVariableNode nodes and continue walking the tree.

Dispatch enter and leave events for EnsureNode nodes and continue walking the tree.

Dispatch enter and leave events for FalseNode nodes and continue walking the tree.

Dispatch enter and leave events for FindPatternNode nodes and continue walking the tree.

Dispatch enter and leave events for FlipFlopNode nodes and continue walking the tree.

Dispatch enter and leave events for FloatNode nodes and continue walking the tree.

Dispatch enter and leave events for ForNode nodes and continue walking the tree.

Dispatch enter and leave events for ForwardingArgumentsNode nodes and continue walking the tree.

Dispatch enter and leave events for ForwardingParameterNode nodes and continue walking the tree.

Dispatch enter and leave events for ForwardingSuperNode nodes and continue walking the tree.

Dispatch enter and leave events for GlobalVariableAndWriteNode nodes and continue walking the tree.

Dispatch enter and leave events for GlobalVariableOperatorWriteNode nodes and continue walking the tree.

Dispatch enter and leave events for GlobalVariableOrWriteNode nodes and continue walking the tree.

Dispatch enter and leave events for GlobalVariableReadNode nodes and continue walking the tree.

Dispatch enter and leave events for GlobalVariableTargetNode nodes and continue walking the tree.

Dispatch enter and leave events for GlobalVariableWriteNode nodes and continue walking the tree.

Dispatch enter and leave events for HashNode nodes and continue walking the tree.

Dispatch enter and leave events for HashPatternNode nodes and continue walking the tree.

Dispatch enter and leave events for IfNode nodes and continue walking the tree.

Dispatch enter and leave events for ImaginaryNode nodes and continue walking the tree.

Dispatch enter and leave events for ImplicitNode nodes and continue walking the tree.

Dispatch enter and leave events for ImplicitRestNode nodes and continue walking the tree.

Dispatch enter and leave events for InNode nodes and continue walking the tree.

Dispatch enter and leave events for IndexAndWriteNode nodes and continue walking the tree.

Dispatch enter and leave events for IndexOperatorWriteNode nodes and continue walking the tree.

Dispatch enter and leave events for IndexOrWriteNode nodes and continue walking the tree.

Dispatch enter and leave events for IndexTargetNode nodes and continue walking the tree.

Dispatch enter and leave events for InstanceVariableAndWriteNode nodes and continue walking the tree.

Dispatch enter and leave events for InstanceVariableOperatorWriteNode nodes and continue walking the tree.

Dispatch enter and leave events for InstanceVariableOrWriteNode nodes and continue walking the tree.

Dispatch enter and leave events for InstanceVariableReadNode nodes and continue walking the tree.

Dispatch enter and leave events for InstanceVariableTargetNode nodes and continue walking the tree.

Dispatch enter and leave events for InstanceVariableWriteNode nodes and continue walking the tree.

Dispatch enter and leave events for IntegerNode nodes and continue walking the tree.

Dispatch enter and leave events for InterpolatedMatchLastLineNode nodes and continue walking the tree.

Dispatch enter and leave events for InterpolatedRegularExpressionNode nodes and continue walking the tree.

Dispatch enter and leave events for InterpolatedStringNode nodes and continue walking the tree.

Dispatch enter and leave events for InterpolatedSymbolNode nodes and continue walking the tree.

Dispatch enter and leave events for InterpolatedXStringNode nodes and continue walking the tree.

Dispatch enter and leave events for KeywordHashNode nodes and continue walking the tree.

Dispatch enter and leave events for KeywordRestParameterNode nodes and continue walking the tree.

Dispatch enter and leave events for LambdaNode nodes and continue walking the tree.

Dispatch enter and leave events for LocalVariableAndWriteNode nodes and continue walking the tree.

Dispatch enter and leave events for LocalVariableOperatorWriteNode nodes and continue walking the tree.

Dispatch enter and leave events for LocalVariableOrWriteNode nodes and continue walking the tree.

Dispatch enter and leave events for LocalVariableReadNode nodes and continue walking the tree.

Dispatch enter and leave events for LocalVariableTargetNode nodes and continue walking the tree.

Dispatch enter and leave events for LocalVariableWriteNode nodes and continue walking the tree.

Dispatch enter and leave events for MatchLastLineNode nodes and continue walking the tree.

Dispatch enter and leave events for MatchPredicateNode nodes and continue walking the tree.

Dispatch enter and leave events for MatchRequiredNode nodes and continue walking the tree.

Dispatch enter and leave events for MatchWriteNode nodes and continue walking the tree.

Dispatch enter and leave events for MissingNode nodes and continue walking the tree.

Dispatch enter and leave events for ModuleNode nodes and continue walking the tree.

Dispatch enter and leave events for MultiTargetNode nodes and continue walking the tree.

Dispatch enter and leave events for MultiWriteNode nodes and continue walking the tree.

Dispatch enter and leave events for NextNode nodes and continue walking the tree.

Dispatch enter and leave events for NilNode nodes and continue walking the tree.

Dispatch enter and leave events for NoKeywordsParameterNode nodes and continue walking the tree.

Dispatch enter and leave events for NumberedParametersNode nodes and continue walking the tree.

Dispatch enter and leave events for NumberedReferenceReadNode nodes and continue walking the tree.

Dispatch enter and leave events for OptionalKeywordParameterNode nodes and continue walking the tree.

Dispatch enter and leave events for OptionalParameterNode nodes and continue walking the tree.

Dispatch enter and leave events for OrNode nodes and continue walking the tree.

Dispatch enter and leave events for ParametersNode nodes and continue walking the tree.

Dispatch enter and leave events for ParenthesesNode nodes and continue walking the tree.

Dispatch enter and leave events for PinnedExpressionNode nodes and continue walking the tree.

Dispatch enter and leave events for PinnedVariableNode nodes and continue walking the tree.

Dispatch enter and leave events for PostExecutionNode nodes and continue walking the tree.

Dispatch enter and leave events for PreExecutionNode nodes and continue walking the tree.

Dispatch enter and leave events for ProgramNode nodes and continue walking the tree.

Dispatch enter and leave events for RangeNode nodes and continue walking the tree.

Dispatch enter and leave events for RationalNode nodes and continue walking the tree.

Dispatch enter and leave events for RedoNode nodes and continue walking the tree.

Dispatch enter and leave events for RegularExpressionNode nodes and continue walking the tree.

Dispatch enter and leave events for RequiredKeywordParameterNode nodes and continue walking the tree.

Dispatch enter and leave events for RequiredParameterNode nodes and continue walking the tree.

Dispatch enter and leave events for RescueModifierNode nodes and continue walking the tree.

Dispatch enter and leave events for RescueNode nodes and continue walking the tree.

Dispatch enter and leave events for RestParameterNode nodes and continue walking the tree.

Dispatch enter and leave events for RetryNode nodes and continue walking the tree.

Dispatch enter and leave events for ReturnNode nodes and continue walking the tree.

Dispatch enter and leave events for SelfNode nodes and continue walking the tree.

Dispatch enter and leave events for SingletonClassNode nodes and continue walking the tree.

Dispatch enter and leave events for SourceEncodingNode nodes and continue walking the tree.

Dispatch enter and leave events for SourceFileNode nodes and continue walking the tree.

Dispatch enter and leave events for SourceLineNode nodes and continue walking the tree.

Dispatch enter and leave events for SplatNode nodes and continue walking the tree.

Dispatch enter and leave events for StatementsNode nodes and continue walking the tree.

Dispatch enter and leave events for StringNode nodes and continue walking the tree.

Dispatch enter and leave events for SuperNode nodes and continue walking the tree.

Dispatch enter and leave events for SymbolNode nodes and continue walking the tree.

Dispatch enter and leave events for TrueNode nodes and continue walking the tree.

Dispatch enter and leave events for UndefNode nodes and continue walking the tree.

Dispatch enter and leave events for UnlessNode nodes and continue walking the tree.

Dispatch enter and leave events for UntilNode nodes and continue walking the tree.

Dispatch enter and leave events for WhenNode nodes and continue walking the tree.

Dispatch enter and leave events for WhileNode nodes and continue walking the tree.

Dispatch enter and leave events for XStringNode nodes and continue walking the tree.

Dispatch enter and leave events for YieldNode nodes and continue walking the tree.