Class: BetterHtml::TestHelper::RubyNode
- Inherits:
-
AST::Node
- Object
- AST::Node
- AST::Node
- BetterHtml::TestHelper::RubyNode
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
#arguments ⇒ Object
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
88
89
90
|
# File 'lib/better_html/test_helper/ruby_node.rb', line 88
def begin?
type?(:begin)
end
|
#child_nodes ⇒ Object
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
80
81
82
|
# File 'lib/better_html/test_helper/ruby_node.rb', line 80
def hash?
type?(:hash)
end
|
#method_call? ⇒ Boolean
76
77
78
|
# File 'lib/better_html/test_helper/ruby_node.rb', line 76
def method_call?
[:send, :csend].include?(type)
end
|
#method_name ⇒ Object
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
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
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
84
85
86
|
# File 'lib/better_html/test_helper/ruby_node.rb', line 84
def pair?
type?(:pair)
end
|
#receiver ⇒ Object
100
101
102
|
# File 'lib/better_html/test_helper/ruby_node.rb', line 100
def receiver
children[0] if method_call?
end
|
#return_values ⇒ Object
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
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
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
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
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
|