Sha256: 779e0125312e93d72cfa9ad5fdd965dca01356ea384861c6aa3dd2fef5d160ff

Contents?: true

Size: 1.72 KB

Versions: 5

Compression:

Stored size: 1.72 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 #{debug_info}, 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 #{debug_info}, child_name: #{child_name}"
    end
  end

  # Return the debug info.
  #
  # @return [String] file, line, source and node.
  def debug_info
    map(&:debug_info).join("\n")
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
synvert-core-1.4.0 lib/synvert/core/array_ext.rb
synvert-core-1.3.1 lib/synvert/core/array_ext.rb
synvert-core-1.3.0 lib/synvert/core/array_ext.rb
synvert-core-1.2.1 lib/synvert/core/array_ext.rb
synvert-core-1.2.0 lib/synvert/core/array_ext.rb