class RSpec::Support::Source::Node

@private A wrapper for Ripper AST node which is generated with `Ripper.sexp`.

Attributes

parent[R]
sexp[R]

Public Class Methods

new(ripper_sexp, parent=nil) click to toggle source
# File lib/rspec/support/source/node.rb, line 17
def initialize(ripper_sexp, parent=nil)
  @sexp = ripper_sexp.freeze
  @parent = parent
end
sexp?(array) click to toggle source
# File lib/rspec/support/source/node.rb, line 13
def self.sexp?(array)
  array.is_a?(Array) && array.first.is_a?(Symbol)
end

Public Instance Methods

args() click to toggle source
# File lib/rspec/support/source/node.rb, line 26
def args
  @args ||= raw_args.map do |raw_arg|
    if Node.sexp?(raw_arg)
      Node.new(raw_arg, self)
    elsif Location.location?(raw_arg)
      Location.new(*raw_arg)
    elsif raw_arg.is_a?(Array)
      ExpressionSequenceNode.new(raw_arg, self)
    else
      raw_arg
    end
  end.freeze
end
children() click to toggle source
# File lib/rspec/support/source/node.rb, line 40
def children
  @children ||= args.select { |arg| arg.is_a?(Node) }.freeze
end
each() { |current_node| ... } click to toggle source

We use a loop here (instead of recursion) to prevent SystemStackError

# File lib/rspec/support/source/node.rb, line 49
def each
  return to_enum(__method__) unless block_given?

  node_queue = []
  node_queue << self

  while (current_node = node_queue.shift)
    yield current_node
    node_queue.concat(current_node.children)
  end
end
each_ancestor() { |current_node| ... } click to toggle source
# File lib/rspec/support/source/node.rb, line 61
def each_ancestor
  return to_enum(__method__) unless block_given?

  current_node = self

  while (current_node = current_node.parent)
    yield current_node
  end
end
inspect() click to toggle source
# File lib/rspec/support/source/node.rb, line 71
def inspect
  "#<#{self.class} #{type}>"
end
location() click to toggle source
# File lib/rspec/support/source/node.rb, line 44
def location
  @location ||= args.find { |arg| arg.is_a?(Location) }
end
type() click to toggle source
# File lib/rspec/support/source/node.rb, line 22
def type
  sexp[0]
end

Private Instance Methods

raw_args() click to toggle source
# File lib/rspec/support/source/node.rb, line 77
def raw_args
  sexp[1..-1] || []
end