Method: Array#child_node_range

Defined in:
lib/synvert/core/array_ext.rb

#child_node_range(child_name) ⇒ Parser::Source::Range

Get the source range of child node.

Parameters:

  • child_name (String)

    name of child node.

Returns:

  • (Parser::Source::Range)

    source range of child node.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/synvert/core/array_ext.rb', line 23

def child_node_range(child_name)
  direct_child_name, nested_child_name = child_name.split('.', 2)
  child_direct_child_node = direct_child_name =~ /\A\d+\z/ ? self[direct_child_name.to_i - 1] : self.send(direct_child_name)
  if nested_child_name
    return child_direct_child_node.child_node_range(nested_child_name)
  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 #{map(&:debug_info).join("\n")}, child_name: #{child_name}"
  end
end