Class: Parser::AST::Node
- Inherits:
-
Object
- Object
- Parser::AST::Node
- Defined in:
- lib/synvert/core/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.
-
#child_node_range(child_name) ⇒ Parser::Source::Range
Get the source range of child node.
-
#column ⇒ Integer
Get the column of current node.
-
#condition ⇒ Parser::AST::Node
Get condition node of :if node.
- #debug_info ⇒ Object
-
#filename ⇒ String
Get the file name of the current node.
-
#hash_value(key) ⇒ Parser::AST::Node
Get hash value node according to specified key.
-
#key ⇒ Parser::AST::Node
Get key node of hash :pair node.
-
#key?(key) ⇒ Boolean
Test if hash node contains specified key.
-
#keys ⇒ Array<Parser::AST::Node>
Get keys node of :hash node.
-
#left_value ⇒ Parser::AST::Node
Return the left value.
-
#line ⇒ Integer
Get the line of current node.
-
#match?(rules) ⇒ Boolean
Match current node with rules.
-
#message ⇒ Parser::AST::Node
Get message node of :super or :send node.
-
#method_missing(method_name, *args, &block) ⇒ Object
Respond key value for hash node, e.g.
-
#name ⇒ Parser::AST::Node
Get name node of :class, :module, :const, :mlhs, :def and :defs node.
-
#parent_class ⇒ Parser::AST::Node
Get parent_class node of :class node.
-
#parent_const ⇒ Parser::AST::Node
Get parent constant node of :const node.
-
#receiver ⇒ Parser::AST::Node
Get receiver node of :send node.
-
#recursive_children {|child| ... } ⇒ Object
Recursively iterate all child nodes of current node.
- #respond_to_missing?(method_name, *args) ⇒ Boolean
-
#rewritten_source(code) ⇒ String
Get rewritten source code.
-
#right_value ⇒ Array<Parser::AST::Node>
Return the right value.
-
#strip_curly_braces ⇒ Object
strip curly braces for hash.
-
#to_lambda_literal ⇒ Object
convert lambda {} to -> {}.
- #to_s ⇒ Object
-
#to_single_quote ⇒ Object
get single quote string.
-
#to_source ⇒ String
Get the source code of current node.
-
#to_symbol ⇒ Object
convert string to symbol.
-
#to_value ⇒ Object
Return the exact value.
-
#value ⇒ Parser::AST::Node
Get value node of hash :pair node.
-
#values ⇒ Array<Parser::AST::Node>
Get values node of :hash node.
-
#wrap_curly_braces ⇒ Object
wrap curly braces for hash.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
Respond key value for hash node, e.g.
Current node is s(:hash, s(:pair, s(:sym, :number), s(:int, 10))) node.number_value is 10
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
# File 'lib/synvert/core/node_ext.rb', line 268 def method_missing(method_name, *args, &block) if :args == type && children.respond_to?(method_name) return children.send(method_name, *args, &block) elsif :hash == type && method_name.to_s.include?('_value') key = method_name.to_s.sub('_value', '') return hash_value(key.to_sym)&.to_value if key?(key.to_sym) return hash_value(key.to_s)&.to_value if key?(key.to_s) return nil elsif :hash == type && method_name.to_s.include?('_source') key = method_name.to_s.sub('_source', '') return hash_value(key.to_sym)&.to_source if key?(key.to_sym) return hash_value(key.to_s)&.to_source if key?(key.to_s) return nil end super end |
Instance Method Details
#arguments ⇒ Array<Parser::AST::Node>
Get arguments node of :send, :block or :defined? node.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/synvert/core/node_ext.rb', line 78 def arguments case type when :def, :block children[1] when :defs children[2] when :send children[2..-1] when :defined? children else raise Synvert::Core::MethodNotSupported, "arguments is not handled for #{debug_info}" end end |
#body ⇒ Array<Parser::AST::Node>
Get body node of :begin or :block node.
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/synvert/core/node_ext.rb', line 109 def body case type when :begin children when :def, :block, :class, :module return [] if children[2].nil? :begin == children[2].type ? children[2].body : children[2..-1] when :defs return [] if children[3].nil? :begin == children[3].type ? children[3].body : children[3..-1] else raise Synvert::Core::MethodNotSupported, "body is not handled for #{debug_info}" end end |
#caller ⇒ Parser::AST::Node
Get caller node of :block node.
97 98 99 100 101 102 103 |
# File 'lib/synvert/core/node_ext.rb', line 97 def caller if :block == type children[0] else raise Synvert::Core::MethodNotSupported, "caller is not handled for #{debug_info}" end end |
#child_node_range(child_name) ⇒ Parser::Source::Range
Get the source range of child node.
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 |
# File 'lib/synvert/core/node_ext.rb', line 350 def child_node_range(child_name) case [type, child_name.to_sym] when i[block pipes], i[def parentheses], i[defs parentheses] Parser::Source::Range.new('(string)', arguments.loc.expression.begin_pos, arguments.loc.expression.end_pos) when i[block arguments], i[def arguments], i[defs arguments] Parser::Source::Range.new( '(string)', arguments.first.loc.expression.begin_pos, arguments.last.loc.expression.end_pos ) when i[class name], i[def name], i[defs name] loc.name when i[defs dot] loc.operator when i[defs self] Parser::Source::Range.new('(string)', loc.operator.begin_pos - 'self'.length, loc.operator.begin_pos) when i[send dot] loc.dot when i[send ] if loc.operator Parser::Source::Range.new('(string)', loc.selector.begin_pos, loc.operator.end_pos) else loc.selector end when i[send parentheses] if loc.begin && loc.end Parser::Source::Range.new('(string)', loc.begin.begin_pos, loc.end.end_pos) end else direct_child_name, nested_child_name = child_name.to_s.split('.', 2) if respond_to?(direct_child_name) child_node = send(direct_child_name) if nested_child_name if child_node.is_a?(Array) child_direct_child_name, *child_nested_child_name = nested_child_name child_direct_child_node = child_direct_child_name =~ /\A\d+\z/ ? child_node[child_direct_child_name] : child_node.send(child_direct_child_name) if child_nested_child_name.length > 0 return child_direct_child_node.child_node_range(child_nested_child_name.join('.')) elsif child_direct_child_node return ( Parser::Source::Range.new( '(string)', child_direct_child_node.loc.expression.begin_pos, child_direct_child_node.loc.expression.end_pos ) ) else raise Synvert::Core::MethodNotSupported, "child_node_range is not handled for #{debug_info}, child_name: #{child_name}" end end return child_node.child_node_range(nested_child_name) end return nil if child_node.nil? if child_node.is_a?(Parser::AST::Node) return( Parser::Source::Range.new( '(string)', child_node.loc.expression.begin_pos, child_node.loc.expression.end_pos ) ) end # arguments return nil if child_node.empty? return( Parser::Source::Range.new( '(string)', child_node.first.loc.expression.begin_pos, child_node.last.loc.expression.end_pos ) ) end raise Synvert::Core::MethodNotSupported, "child_node_range is not handled for #{debug_info}, child_name: #{child_name}" end end |
#column ⇒ Integer
Get the column of current node.
335 336 337 |
# File 'lib/synvert/core/node_ext.rb', line 335 def column loc.expression.column end |
#condition ⇒ Parser::AST::Node
Get condition node of :if node.
130 131 132 133 134 135 136 |
# File 'lib/synvert/core/node_ext.rb', line 130 def condition if :if == type children[0] else raise Synvert::Core::MethodNotSupported, "condition is not handled for #{debug_info}" end end |
#debug_info ⇒ Object
308 309 310 311 312 313 314 315 316 |
# File 'lib/synvert/core/node_ext.rb', line 308 def debug_info "\n" + [ "file: #{loc.expression.source_buffer.name}", "line: #{loc.expression.line}", "source: #{to_source}", "node: #{inspect}" ].join("\n") end |
#filename ⇒ String
Get the file name of the current node.
321 322 323 |
# File 'lib/synvert/core/node_ext.rb', line 321 def filename loc.expression&.source_buffer.name end |
#hash_value(key) ⇒ Parser::AST::Node
Get hash value node according to specified key.
180 181 182 183 184 185 186 187 |
# File 'lib/synvert/core/node_ext.rb', line 180 def hash_value(key) if :hash == type value_node = children.find { |pair_node| pair_node.key.to_value == key } value_node&.value else raise Synvert::Core::MethodNotSupported, "hash_value is not handled for #{debug_info}" end end |
#key ⇒ Parser::AST::Node
Get key node of hash :pair node.
193 194 195 196 197 198 199 |
# File 'lib/synvert/core/node_ext.rb', line 193 def key if :pair == type children.first else raise Synvert::Core::MethodNotSupported, "key is not handled for #{debug_info}" end end |
#key?(key) ⇒ Boolean
Test if hash node contains specified key.
167 168 169 170 171 172 173 |
# File 'lib/synvert/core/node_ext.rb', line 167 def key?(key) if :hash == type children.any? { |pair_node| pair_node.key.to_value == key } else raise Synvert::Core::MethodNotSupported, "key? is not handled for #{debug_info}" end end |
#keys ⇒ Array<Parser::AST::Node>
Get keys node of :hash node.
142 143 144 145 146 147 148 |
# File 'lib/synvert/core/node_ext.rb', line 142 def keys if :hash == type children.map { |child| child.children[0] } else raise Synvert::Core::MethodNotSupported, "keys is not handled for #{debug_info}" end end |
#left_value ⇒ Parser::AST::Node
Return the left value.
217 218 219 220 221 222 223 224 225 |
# File 'lib/synvert/core/node_ext.rb', line 217 def left_value if i[masgn lvasgn ivasgn cvasgn and or].include? type children[0] elsif :or_asgn == type children[0].children[0] else raise Synvert::Core::MethodNotSupported, "left_value is not handled for #{debug_info}" end end |
#line ⇒ Integer
Get the line of current node.
342 343 344 |
# File 'lib/synvert/core/node_ext.rb', line 342 def line loc.expression.line end |
#match?(rules) ⇒ Boolean
Match current node with rules.
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 |
# File 'lib/synvert/core/node_ext.rb', line 452 def match?(rules) flat_hash(rules).keys.all? do |multi_keys| case multi_keys.last when :any, :contain actual_values = actual_value(self, multi_keys[0...-1]) expected = expected_value(rules, multi_keys) actual_values.any? { |actual| match_value?(actual, expected) } when :not actual = actual_value(self, multi_keys[0...-1]) expected = expected_value(rules, multi_keys) !match_value?(actual, expected) when :in actual = actual_value(self, multi_keys[0...-1]) expected_values = expected_value(rules, multi_keys) expected_values.any? { |expected| match_value?(actual, expected) } when :not_in actual = actual_value(self, multi_keys[0...-1]) expected_values = expected_value(rules, multi_keys) expected_values.all? { |expected| !match_value?(actual, expected) } else actual = actual_value(self, multi_keys) expected = expected_value(rules, multi_keys) match_value?(actual, expected) end end end |
#message ⇒ Parser::AST::Node
Get message node of :super or :send node.
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/synvert/core/node_ext.rb', line 63 def case type when :super, :zsuper :super when :send children[1] else raise Synvert::Core::MethodNotSupported, "message is not handled for #{debug_info}" end end |
#name ⇒ Parser::AST::Node
Get name node of :class, :module, :const, :mlhs, :def and :defs node.
10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/synvert/core/node_ext.rb', line 10 def name case type when :class, :module, :def, :arg, :blockarg, :restarg, :lvar, :ivar, :cvar children[0] when :defs, :const children[1] when :mlhs self else raise Synvert::Core::MethodNotSupported, "name is not handled for #{debug_info}" end end |
#parent_class ⇒ Parser::AST::Node
Get parent_class node of :class node.
27 28 29 30 31 32 33 |
# File 'lib/synvert/core/node_ext.rb', line 27 def parent_class if :class == type children[1] else raise Synvert::Core::MethodNotSupported, "parent_class is not handled for #{debug_info}" end end |
#parent_const ⇒ Parser::AST::Node
Get parent constant node of :const node.
39 40 41 42 43 44 45 |
# File 'lib/synvert/core/node_ext.rb', line 39 def parent_const if :const == type children[0] else raise Synvert::Core::MethodNotSupported, "parent_const is not handled for #{debug_info}" end end |
#receiver ⇒ Parser::AST::Node
Get receiver node of :send node.
51 52 53 54 55 56 57 |
# File 'lib/synvert/core/node_ext.rb', line 51 def receiver if :send == type children[0] else raise Synvert::Core::MethodNotSupported, "receiver is not handled for #{debug_info}" end end |
#recursive_children {|child| ... } ⇒ Object
Recursively iterate all child nodes of current node.
439 440 441 442 443 444 445 446 |
# File 'lib/synvert/core/node_ext.rb', line 439 def recursive_children(&block) children.each do |child| if child.is_a?(Parser::AST::Node) stop = yield child child.recursive_children(&block) unless stop == :stop end end end |
#respond_to_missing?(method_name, *args) ⇒ Boolean
288 289 290 291 292 293 294 295 296 297 298 299 300 |
# File 'lib/synvert/core/node_ext.rb', line 288 def respond_to_missing?(method_name, *args) if :args == type && children.respond_to?(method_name) return true elsif :hash == type && method_name.to_s.include?('_value') key = method_name.to_s.sub('_value', '') return true if key?(key.to_sym) || key?(key.to_s) elsif :hash == type && method_name.to_s.include?('_source') key = method_name.to_s.sub('_source', '') return true if key?(key.to_sym) || key?(key.to_s) end super end |
#rewritten_source(code) ⇒ String
Get rewritten source code.
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 |
# File 'lib/synvert/core/node_ext.rb', line 486 def rewritten_source(code) code.gsub(/{{(.*?)}}/m) do old_code = Regexp.last_match(1) if respond_to? old_code.split(/\.|\[/).first evaluated = instance_eval old_code case evaluated when Parser::AST::Node if evaluated.type == :args evaluated.loc.expression.source[1...-1] else evaluated.loc.expression.source end when Array if evaluated.size > 0 file_source = evaluated.first.loc.expression.source_buffer.source source = file_source[evaluated.first.loc.expression.begin_pos...evaluated.last.loc.expression.end_pos] lines = source.split "\n" lines_count = lines.length if lines_count > 1 && lines_count == evaluated.size new_code = [] lines.each_with_index { |line, index| new_code << (index == 0 ? line : line[evaluated.first.indent - 2..-1]) } new_code.join("\n") else source end end when String, Symbol, Integer, Float evaluated when NilClass 'nil' else raise Synvert::Core::MethodNotSupported, "rewritten_source is not handled for #{evaluated.inspect}" end else "{{#{old_code}}}" end end end |
#right_value ⇒ Array<Parser::AST::Node>
Return the right value.
231 232 233 234 235 236 237 |
# File 'lib/synvert/core/node_ext.rb', line 231 def right_value if i[masgn lvasgn ivasgn cvasgn or_asgn and or].include? type children[1] else raise Synvert::Core::MethodNotSupported, "right_value is not handled for #{debug_info}" end end |
#strip_curly_braces ⇒ Object
strip curly braces for hash
528 529 530 531 532 |
# File 'lib/synvert/core/node_ext.rb', line 528 def strip_curly_braces return to_source unless type == :hash to_source.sub(/^{(.*)}$/) { Regexp.last_match(1).strip } end |
#to_lambda_literal ⇒ Object
convert lambda {} to -> {}
556 557 558 559 560 561 562 563 564 565 566 567 568 569 |
# File 'lib/synvert/core/node_ext.rb', line 556 def to_lambda_literal if type == :block && caller.type == :send && caller.receiver.nil? && caller. == :lambda new_source = to_source if arguments.size > 1 new_source = new_source[0...arguments.loc.begin.to_range.begin - 1] + new_source[arguments.loc.end.to_range.end..-1] new_source = new_source.sub('lambda', "->(#{arguments.map(&:to_source).join(', ')})") else new_source = new_source.sub('lambda', '->') end new_source else to_source end end |
#to_s ⇒ Object
302 303 304 305 306 |
# File 'lib/synvert/core/node_ext.rb', line 302 def to_s if :mlhs == type "(#{children.map(&:name).join(', ')})" end end |
#to_single_quote ⇒ Object
get single quote string
542 543 544 545 546 |
# File 'lib/synvert/core/node_ext.rb', line 542 def to_single_quote return to_source unless type == :str "'#{to_value}'" end |
#to_source ⇒ String
Get the source code of current node.
328 329 330 |
# File 'lib/synvert/core/node_ext.rb', line 328 def to_source loc.expression&.source end |
#to_symbol ⇒ Object
convert string to symbol
549 550 551 552 553 |
# File 'lib/synvert/core/node_ext.rb', line 549 def to_symbol return to_source unless type == :str ":#{to_value}" end |
#to_value ⇒ Object
Return the exact value.
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/synvert/core/node_ext.rb', line 243 def to_value case type when :int, :float, :str, :sym children.last when :true true when :false false when :array children.map(&:to_value) when :irange (children.first.to_value..children.last.to_value) when :erange (children.first.to_value...children.last.to_value) when :begin children.first.to_value else self end end |
#value ⇒ Parser::AST::Node
Get value node of hash :pair node.
205 206 207 208 209 210 211 |
# File 'lib/synvert/core/node_ext.rb', line 205 def value if :pair == type children.last else raise Synvert::Core::MethodNotSupported, "value is not handled for #{debug_info}" end end |
#values ⇒ Array<Parser::AST::Node>
Get values node of :hash node.
154 155 156 157 158 159 160 |
# File 'lib/synvert/core/node_ext.rb', line 154 def values if :hash == type children.map { |child| child.children[1] } else raise Synvert::Core::MethodNotSupported, "keys is not handled for #{debug_info}" end end |
#wrap_curly_braces ⇒ Object
wrap curly braces for hash
535 536 537 538 539 |
# File 'lib/synvert/core/node_ext.rb', line 535 def wrap_curly_braces return to_source unless type == :hash "{ #{to_source} }" end |