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



16
17
18
19
20
21
22
23
24
25
# File 'lib/better_html/test_helper/ruby_node.rb', line 16

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

Instance Method Details

#argumentsObject



90
91
92
# File 'lib/better_html/test_helper/ruby_node.rb', line 90

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

#begin?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/better_html/test_helper/ruby_node.rb', line 82

def begin?
  type?(:begin)
end

#child_nodesObject



27
28
29
# File 'lib/better_html/test_helper/ruby_node.rb', line 27

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

#hash?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/better_html/test_helper/ruby_node.rb', line 74

def hash?
  type?(:hash)
end

#method_call?Boolean

Returns:

  • (Boolean)


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

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

#method_nameObject



86
87
88
# File 'lib/better_html/test_helper/ruby_node.rb', line 86

def method_name
  children[1] if method_call?
end

#method_name?(name) ⇒ Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/better_html/test_helper/ruby_node.rb', line 98

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

#node?(current) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#pair?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/better_html/test_helper/ruby_node.rb', line 78

def pair?
  type?(:pair)
end

#receiverObject



94
95
96
# File 'lib/better_html/test_helper/ruby_node.rb', line 94

def receiver
  children[0] if method_call?
end

#return_valuesObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/better_html/test_helper/ruby_node.rb', line 46

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)


65
66
67
68
# File 'lib/better_html/test_helper/ruby_node.rb', line 65

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)


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

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

#type?(wanted_type) ⇒ Boolean

Returns:

  • (Boolean)


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

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