class BetterHtml::TestHelper::RubyNode

Constants

BLOCK_EXPR
STATIC_TYPES

Public Class Methods

parse(code) click to toggle source
# File lib/better_html/test_helper/ruby_node.rb, line 17
def self.parse(code)
  parser = ::Parser::CurrentRuby.new(Builder.new)
  parser.diagnostics.ignore_warnings = true
  parser.diagnostics.all_errors_are_fatal = false
  parser.diagnostics.consumer = nil

  buf = ::Parser::Source::Buffer.new('(string)')
  buf.source = code.sub(BLOCK_EXPR, '')
  parser.parse(buf)
end

Public Instance Methods

arguments() click to toggle source
# File lib/better_html/test_helper/ruby_node.rb, line 91
def arguments
  children[2..-1] if method_call?
end
begin?() click to toggle source
# File lib/better_html/test_helper/ruby_node.rb, line 83
def begin?
  type?(:begin)
end
child_nodes() click to toggle source
# File lib/better_html/test_helper/ruby_node.rb, line 28
def child_nodes
  children.select { |child| node?(child) }
end
hash?() click to toggle source
# File lib/better_html/test_helper/ruby_node.rb, line 75
def hash?
  type?(:hash)
end
method_call?() click to toggle source
# File lib/better_html/test_helper/ruby_node.rb, line 71
def method_call?
  [:send, :csend].include?(type)
end
method_name() click to toggle source
# File lib/better_html/test_helper/ruby_node.rb, line 87
def method_name
  children[1] if method_call?
end
method_name?(name) click to toggle source
# File lib/better_html/test_helper/ruby_node.rb, line 99
def method_name?(name)
  method_call? && method_name == name
end
node?(current) click to toggle source
# File lib/better_html/test_helper/ruby_node.rb, line 32
def node?(current)
  current.is_a?(self.class)
end
pair?() click to toggle source
# File lib/better_html/test_helper/ruby_node.rb, line 79
def pair?
  type?(:pair)
end
receiver() click to toggle source
# File lib/better_html/test_helper/ruby_node.rb, line 95
def receiver
  children[0] if method_call?
end
return_values() click to toggle source
# File lib/better_html/test_helper/ruby_node.rb, line 47
def return_values
  Enumerator.new do |yielder|
    case type
    when :send, :csend, :ivar, *STATIC_TYPES
      yielder.yield(self)
    when :if, :masgn, :lvasgn
      # first child is ignored as it does not contain return values
      # for example, in `foo ? x : y` we only care about x and y, not foo
      children[1..-1].each do |child|
        child.return_values.each { |v| yielder.yield(v) } if node?(child)
      end
    else
      child_nodes.each do |child|
        child.return_values.each { |v| yielder.yield(v) }
      end
    end
  end
end
static_return_value?() click to toggle source
# File lib/better_html/test_helper/ruby_node.rb, line 66
def static_return_value?
  return false if (possible_values = return_values.to_a).empty?
  possible_values.all?(&:static_value?)
end
static_value?() click to toggle source
# File lib/better_html/test_helper/ruby_node.rb, line 42
def static_value?
  type?(STATIC_TYPES) ||
    (type?(:dstr) && !children.any? { |child| !child.type?(:str) })
end
type?(wanted_type) click to toggle source
# File lib/better_html/test_helper/ruby_node.rb, line 36
def type?(wanted_type)
  Array.wrap(wanted_type).include?(type)
end