Sha256: 5d8b7b3b19fa9a6ee9922f5dbbe3f1f9762ca393e536a3321eda074aa508553f

Contents?: true

Size: 1.62 KB

Versions: 7

Compression:

Stored size: 1.62 KB

Contents

# frozen_string_literal: true

# Extend Array.
class Array
  # Get child node by the name.
  #
  # @param child_name [String] name of child node.
  # @return [Parser::AST::Node] the child node.
  def child_node_by_name(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)
    return child_direct_child_node.child_node_by_name(nested_child_name) if nested_child_name
    return child_direct_child_node if child_direct_child_node

    raise Synvert::Core::MethodNotSupported,
          "child_node_by_name is not handled for #{map(&:debug_info).join("\n")}, child_name: #{child_name}"
  end

  # Get the source range of child node.
  #
  # @param child_name [String] name of child node.
  # @return [Parser::Source::Range] source range of child node.
  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
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
synvert-core-1.1.1 lib/synvert/core/array_ext.rb
synvert-core-1.1.0 lib/synvert/core/array_ext.rb
synvert-core-1.0.5 lib/synvert/core/array_ext.rb
synvert-core-1.0.4 lib/synvert/core/array_ext.rb
synvert-core-1.0.3 lib/synvert/core/array_ext.rb
synvert-core-1.0.2 lib/synvert/core/array_ext.rb
synvert-core-1.0.1 lib/synvert/core/array_ext.rb