Class: BetterHtml::TestHelper::RubyNode

Inherits:
AST::Node
  • Object
show all
Defined in:
lib/better_html/test_helper/ruby_node.rb

Defined Under Namespace

Classes: Builder, ParseError

Constant Summary collapse

BLOCK_EXPR =
/\s*((\s+|\))do|\{)(\s*\|[^|]*\|)?\s*\Z/
STATIC_TYPES =
[:str, :int, :true, :false, :nil]

Instance Attribute Summary

Attributes inherited from AST::Node

#loc

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AST::Node

#descendants, #location

Class Method Details

.parse(code) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/better_html/test_helper/ruby_node.rb', line 20

def 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

Instance Method Details

#argumentsObject



96
97
98
# File 'lib/better_html/test_helper/ruby_node.rb', line 96

def arguments
  children[2..-1] if method_call?
end

#begin?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/better_html/test_helper/ruby_node.rb', line 88

def begin?
  type?(:begin)
end

#child_nodesObject



32
33
34
# File 'lib/better_html/test_helper/ruby_node.rb', line 32

def child_nodes
  children.select { |child| node?(child) }
end

#hash?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/better_html/test_helper/ruby_node.rb', line 80

def hash?
  type?(:hash)
end

#method_call?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/better_html/test_helper/ruby_node.rb', line 76

def method_call?
  [:send, :csend].include?(type)
end

#method_nameObject



92
93
94
# File 'lib/better_html/test_helper/ruby_node.rb', line 92

def method_name
  children[1] if method_call?
end

#method_name?(name) ⇒ Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/better_html/test_helper/ruby_node.rb', line 104

def method_name?(name)
  method_call? && method_name == name
end

#node?(current) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/better_html/test_helper/ruby_node.rb', line 36

def node?(current)
  current.is_a?(self.class)
end

#pair?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/better_html/test_helper/ruby_node.rb', line 84

def pair?
  type?(:pair)
end

#receiverObject



100
101
102
# File 'lib/better_html/test_helper/ruby_node.rb', line 100

def receiver
  children[0] if method_call?
end

#return_valuesObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/better_html/test_helper/ruby_node.rb', line 51

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?Boolean

Returns:

  • (Boolean)


70
71
72
73
74
# File 'lib/better_html/test_helper/ruby_node.rb', line 70

def static_return_value?
  return false if (possible_values = return_values.to_a).empty?

  possible_values.all?(&:static_value?)
end

#static_value?Boolean

Returns:

  • (Boolean)


46
47
48
49
# File 'lib/better_html/test_helper/ruby_node.rb', line 46

def static_value?
  type?(STATIC_TYPES) ||
    (type?(:dstr) && !children.any? { |child| !child.type?(:str) })
end

#type?(wanted_type) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/better_html/test_helper/ruby_node.rb', line 40

def type?(wanted_type)
  Array.wrap(wanted_type).include?(type)
end