Class: Parser::AST::Node
- Inherits:
-
Object
- Object
- Parser::AST::Node
- Defined in:
- lib/synvert/node_ext.rb
Overview
Parser::AST::Node monkey patch.
Instance Method Summary collapse
-
#arguments ⇒ Array<Parser::AST::Node>
Get arguments node of :send, :block or :defined? node.
-
#body ⇒ Array<Parser::AST::Node>
Get body node of :begin or :block node.
-
#caller ⇒ Parser::AST::Node
Get caller node of :block node.
-
#condition ⇒ Parser::AST::Node
Get condition node of :if node.
-
#indent ⇒ Integer
Get the indent of current node.
-
#key ⇒ Parser::AST::Node
Get key node of hash :pair node.
-
#keys ⇒ Array<Parser::AST::Node>
Get keys node of :hash node.
-
#match?(instance, rules) ⇒ Boolean
Match current node with rules.
-
#message ⇒ Parser::AST::Node
Get message node of :send node.
-
#name ⇒ Parser::AST::Node
Get name node of :class, :module, :def and :defs node.
-
#receiver ⇒ Parser::AST::Node
Get receiver node of :send node.
-
#recursive_children {|child| ... } ⇒ Object
Recursively iterate all child nodes of current node.
-
#rewritten_source(code) ⇒ String
Get rewritten source code.
-
#source(instance) ⇒ String
Get the source code of current node.
-
#value ⇒ Parser::AST::Node
Get value node of hash :pair node.
-
#values ⇒ Array<Parser::AST::Node>
Get values node of :hash node.
Instance Method Details
#arguments ⇒ Array<Parser::AST::Node>
Get arguments node of :send, :block or :defined? node.
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/synvert/node_ext.rb', line 46 def arguments case self.type when :send self.children[2..-1] when :block self.children[1].children when :defined? self.children else raise Synvert::MethodNotSupported.new "arguments is not handled for #{self.inspect}" end end |
#body ⇒ Array<Parser::AST::Node>
Get body node of :begin or :block node.
75 76 77 78 79 80 81 82 83 84 |
# File 'lib/synvert/node_ext.rb', line 75 def body case self.type when :begin self.children when :block :begin == self.children[2].type ? self.children[2].children : [self.children[2]] else raise Synvert::MethodNotSupported.new "body is not handled for #{self.inspect}" end end |
#caller ⇒ Parser::AST::Node
Get caller node of :block node.
63 64 65 66 67 68 69 |
# File 'lib/synvert/node_ext.rb', line 63 def caller if :block == self.type self.children[0] else raise Synvert::MethodNotSupported.new "caller is not handled for #{self.inspect}" end end |
#condition ⇒ Parser::AST::Node
Get condition node of :if node.
90 91 92 93 94 95 96 |
# File 'lib/synvert/node_ext.rb', line 90 def condition if :if == self.type self.children[0] else raise Synvert::MethodNotSupported.new "condition is not handled for #{self.inspect}" end end |
#indent ⇒ Integer
Get the indent of current node.
159 160 161 |
# File 'lib/synvert/node_ext.rb', line 159 def indent self.loc.expression.column end |
#key ⇒ Parser::AST::Node
Get key node of hash :pair node.
126 127 128 129 130 131 132 |
# File 'lib/synvert/node_ext.rb', line 126 def key if :pair == self.type self.children.first else raise Synvert::MethodNotSupported.new "key is not handled for #{self.inspect}" end end |
#keys ⇒ Array<Parser::AST::Node>
Get keys node of :hash node.
102 103 104 105 106 107 108 |
# File 'lib/synvert/node_ext.rb', line 102 def keys if :hash == self.type self.children.map { |child| child.children[0] } else raise Synvert::MethodNotSupported.new "keys is not handled for #{self.inspect}" end end |
#match?(instance, rules) ⇒ Boolean
Match current node with rules.
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/synvert/node_ext.rb', line 181 def match?(instance, rules) flat_hash(rules).keys.all? do |multi_keys| if multi_keys.last == :any actual_values = actual_value(self, instance, multi_keys[0...-1]) expected = expected_value(rules, multi_keys) actual_values.any? { |actual| match_value?(instance, actual, expected) } elsif multi_keys.last == :not actual = actual_value(self, instance, multi_keys[0...-1]) expected = expected_value(rules, multi_keys) !match_value?(instance, actual, expected) else actual = actual_value(self, instance, multi_keys) expected = expected_value(rules, multi_keys) match_value?(instance, actual, expected) end end end |
#message ⇒ Parser::AST::Node
Get message node of :send node.
34 35 36 37 38 39 40 |
# File 'lib/synvert/node_ext.rb', line 34 def if :send == self.type self.children[1] else raise Synvert::MethodNotSupported.new "message is not handled for #{self.inspect}" end end |
#name ⇒ Parser::AST::Node
Get name node of :class, :module, :def and :defs node.
7 8 9 10 11 12 13 14 15 16 |
# File 'lib/synvert/node_ext.rb', line 7 def name case self.type when :class, :module, :def self.children[0] when :defs self.children[1] else raise Synvert::MethodNotSupported.new "name is not handled for #{self.inspect}" end end |
#receiver ⇒ Parser::AST::Node
Get receiver node of :send node.
22 23 24 25 26 27 28 |
# File 'lib/synvert/node_ext.rb', line 22 def receiver if :send == self.type self.children[0] else raise Synvert::MethodNotSupported.new "receiver is not handled for #{self.inspect}" end end |
#recursive_children {|child| ... } ⇒ Object
Recursively iterate all child nodes of current node.
167 168 169 170 171 172 173 174 |
# File 'lib/synvert/node_ext.rb', line 167 def recursive_children self.children.each do |child| if Parser::AST::Node === child yield child child.recursive_children { |c| yield c } end end end |
#rewritten_source(code) ⇒ String
Get rewritten source code.
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/synvert/node_ext.rb', line 206 def rewritten_source(code) code.gsub(/{{(.*?)}}/m) do evaluated = self.instance_eval $1 case evaluated when Parser::AST::Node source = evaluated.loc.expression.source_buffer.source source[evaluated.loc.expression.begin_pos...evaluated.loc.expression.end_pos] when Array if evaluated.size > 0 source = evaluated.first.loc.expression.source_buffer.source source[evaluated.first.loc.expression.begin_pos...evaluated.last.loc.expression.end_pos] end when String evaluated when NilClass 'nil' else raise Synvert::MethodNotSupported.new "rewritten_source is not handled for #{evaluated.inspect}" end end end |
#source(instance) ⇒ String
Get the source code of current node.
150 151 152 153 154 |
# File 'lib/synvert/node_ext.rb', line 150 def source(instance) if self.loc.expression instance.current_source[self.loc.expression.begin_pos...self.loc.expression.end_pos] end end |
#value ⇒ Parser::AST::Node
Get value node of hash :pair node.
138 139 140 141 142 143 144 |
# File 'lib/synvert/node_ext.rb', line 138 def value if :pair == self.type self.children.last else raise Synvert::MethodNotSupported.new "value is not handled for #{self.inspect}" end end |
#values ⇒ Array<Parser::AST::Node>
Get values node of :hash node.
114 115 116 117 118 119 120 |
# File 'lib/synvert/node_ext.rb', line 114 def values if :hash == self.type self.children.map { |child| child.children[1] } else raise Synvert::MethodNotSupported.new "keys is not handled for #{self.inspect}" end end |