class RuboCop::AST::HashElementNode::HashElementDelta

A helper class for comparing the positions of different parts of a `pair` node.

Attributes

first[R]
second[R]

Public Class Methods

new(first, second) click to toggle source
# File lib/rubocop/ast/node/mixin/hash_element_node.rb, line 67
def initialize(first, second)
  @first = first
  @second = second

  raise ArgumentError unless valid_argument_types?
end

Public Instance Methods

delimiter_delta() click to toggle source
# File lib/rubocop/ast/node/mixin/hash_element_node.rb, line 88
def delimiter_delta
  return 0 if first.same_line?(second)
  return 0 if first.delimiter != second.delimiter

  delta(first.loc.operator, second.loc.operator)
end
key_delta(alignment = :left) click to toggle source
# File lib/rubocop/ast/node/mixin/hash_element_node.rb, line 74
def key_delta(alignment = :left)
  return 0 if first.same_line?(second)
  return 0 if keyword_splat? && alignment == :right

  delta(first.key.loc, second.key.loc, alignment)
end
value_delta() click to toggle source
# File lib/rubocop/ast/node/mixin/hash_element_node.rb, line 81
def value_delta
  return 0 if first.same_line?(second)
  return 0 if keyword_splat?

  delta(first.value.loc, second.value.loc)
end

Private Instance Methods

delta(first, second, alignment = :left) click to toggle source
# File lib/rubocop/ast/node/mixin/hash_element_node.rb, line 105
def delta(first, second, alignment = :left)
  case alignment
  when :left
    first.column - second.column
  when :right
    first.last_column - second.last_column
  else
    0
  end
end
keyword_splat?() click to toggle source
# File lib/rubocop/ast/node/mixin/hash_element_node.rb, line 116
def keyword_splat?
  [first, second].any?(&:kwsplat_type?)
end
valid_argument_types?() click to toggle source
# File lib/rubocop/ast/node/mixin/hash_element_node.rb, line 99
def valid_argument_types?
  [first, second].all? do |argument|
    argument.pair_type? || argument.kwsplat_type?
  end
end