class Rbr::Node

Wraps a Parser::AST::Node object and provides convenience methods

Public Class Methods

new(ast_node) click to toggle source
# File lib/rbr/node.rb, line 6
def initialize(ast_node)
  @ast_node = ast_node
end

Public Instance Methods

any_descendant_matches?(match_proc, root = self) click to toggle source

Call the the proc, passing in this node and all children recursively. Return true if any call evaluates to true.

# File lib/rbr/node.rb, line 72
def any_descendant_matches?(match_proc, root = self)
  if root.respond_to?(:children)
    root.children.any? { |c| any_descendant_matches?(match_proc, c) }
  else
    match_proc.call(root)
  end
end
assignment?() click to toggle source
# File lib/rbr/node.rb, line 10
def assignment?
  %i[lvasgn ivasgn cvasgn gvasgn casgn masgn].include? @ast_node.type
end
children() click to toggle source
# File lib/rbr/node.rb, line 52
def children
  @ast_node.children.map do |child|
    if child.is_a?(Parser::AST::Node)
      Rbr::Node.new(child)
    else
      child
    end
  end
end
const?() click to toggle source
# File lib/rbr/node.rb, line 14
def const?
  @ast_node.type == :const
end
expression() click to toggle source
# File lib/rbr/node.rb, line 48
def expression
  @ast_node.loc.expression
end
literal?() click to toggle source
# File lib/rbr/node.rb, line 18
def literal?
  %i[int float str].include? @ast_node.type
end
method_call?(names) click to toggle source
# File lib/rbr/node.rb, line 26
def method_call?(names)
  %i[send csend].include?(@ast_node.type) &&
    begin
      names = [names] unless names.is_a?(Array)

      names.include?(children[1]) ||
        (children[1] == :send && names.include?(children[2].value))
    end
end
nil?() click to toggle source
# File lib/rbr/node.rb, line 36
def nil?
  @ast_node.nil? || !(@ast_node.is_a? Parser::AST::Node)
end
number?() click to toggle source
# File lib/rbr/node.rb, line 22
def number?
  %i[int float].include? @ast_node.type
end
pretty_print() click to toggle source
# File lib/rbr/node.rb, line 66
def pretty_print
  "#{expression.line}: #{expression.source}"
end
str?() click to toggle source
# File lib/rbr/node.rb, line 40
def str?
  %i[str dstr xstr].include? @ast_node.type
end
type() click to toggle source
# File lib/rbr/node.rb, line 44
def type
  @ast_node.type
end
value() click to toggle source
# File lib/rbr/node.rb, line 62
def value
  @ast_node.children[0]
end