Module: Rigor::Source::NodeWalker

Defined in:
lib/rigor/source/node_walker.rb

Overview

Yields every ‘Prism::Node` reachable from a root in DFS pre-order.

The walker is the source-positioning analogue to ‘NodeLocator`: where the locator answers “what node is at this point?”, the walker enumerates the full set of Prism nodes for tooling that needs to operate on each one (coverage probes, lint passes, IDE outlines).

Non-Prism children (literals embedded in node attributes, virtual nodes, or ‘nil` slots) are silently skipped so callers can rely on every yielded value responding to the `Prism::Node` API.

Class Method Summary collapse

Class Method Details

.each(root) {|node| ... } ⇒ Enumerator

Returns when no block is given.

Yield Parameters:

  • node (Prism::Node)

Returns:

  • (Enumerator)

    when no block is given.



22
23
24
25
26
27
# File 'lib/rigor/source/node_walker.rb', line 22

def each(root, &)
  return to_enum(__method__, root) unless block_given?

  walk(root, &)
  nil
end

.each_with_ancestors(root) {|node, ancestors| ... } ⇒ Enumerator

Like each, but also yields the node’s lexical ancestor chain (outermost first, EXCLUDING the node itself). The yielded ‘ancestors` array is the live descent stack — callers that retain it past the block invocation MUST copy it (`Plugin::NodeContext` does). Used by the plugin engine to give `node_rule` blocks their enclosing class / method / block context (ADR-37 slice 1d).

Yield Parameters:

  • node (Prism::Node)
  • ancestors (Array<Prism::Node>)

Returns:

  • (Enumerator)

    when no block is given.



46
47
48
49
50
51
# File 'lib/rigor/source/node_walker.rb', line 46

def each_with_ancestors(root, &)
  return to_enum(__method__, root) unless block_given?

  walk_with_ancestors(root, [], &)
  nil
end

.walk(node) {|node| ... } ⇒ Object

Yields:

  • (node)


29
30
31
32
33
34
# File 'lib/rigor/source/node_walker.rb', line 29

def walk(node, &)
  return unless node.is_a?(Prism::Node)

  yield node
  node.compact_child_nodes.each { |child| walk(child, &) }
end

.walk_with_ancestors(node, ancestors, &block) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/rigor/source/node_walker.rb', line 53

def walk_with_ancestors(node, ancestors, &block)
  return unless node.is_a?(Prism::Node)

  block.call(node, ancestors)
  ancestors.push(node)
  node.compact_child_nodes.each { |child| walk_with_ancestors(child, ancestors, &block) }
  ancestors.pop
end