123456789_123456789_123456789_123456789_123456789_

Class: RuboCop::AST::HashElementNode::HashElementDelta

Relationships & Source Files
Inherits: Object
Defined in: lib/rubocop/ast/node/mixin/hash_element_node.rb

Overview

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

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(first, second) ⇒ HashElementDelta

Raises:

  • (ArgumentError)
[ GitHub ]

  
# File 'lib/rubocop/ast/node/mixin/hash_element_node.rb', line 68

def initialize(first, second)
  @first = first
  @second = second

  raise ArgumentError unless valid_argument_types?
end

Instance Attribute Details

#first (readonly, private)

[ GitHub ]

  
# File 'lib/rubocop/ast/node/mixin/hash_element_node.rb', line 98

attr_reader :first, :second

#keyword_splat?Boolean (readonly, private)

[ GitHub ]

  
# File 'lib/rubocop/ast/node/mixin/hash_element_node.rb', line 119

def keyword_splat?
  [first, second].any?(&:kwsplat_type?)
end

#second (readonly, private)

[ GitHub ]

  
# File 'lib/rubocop/ast/node/mixin/hash_element_node.rb', line 98

attr_reader :first, :second

#valid_argument_types?Boolean (readonly, private)

[ GitHub ]

  
# File 'lib/rubocop/ast/node/mixin/hash_element_node.rb', line 100

def valid_argument_types?
  [first, second].all? do |argument|
    # rubocop:disable InternalAffairs/NodeTypeMultiplePredicates
    argument.pair_type? || argument.kwsplat_type?
    # rubocop:enable InternalAffairs/NodeTypeMultiplePredicates
  end
end

Instance Method Details

#delimiter_delta

[ GitHub ]

  
# File 'lib/rubocop/ast/node/mixin/hash_element_node.rb', line 89

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

#delta(first, second, alignment = :left) (private)

[ GitHub ]

  
# File 'lib/rubocop/ast/node/mixin/hash_element_node.rb', line 108

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

#key_delta(alignment = :left)

[ GitHub ]

  
# File 'lib/rubocop/ast/node/mixin/hash_element_node.rb', line 75

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

[ GitHub ]

  
# File 'lib/rubocop/ast/node/mixin/hash_element_node.rb', line 82

def value_delta
  return 0 if first.same_line?(second)
  return 0 if keyword_splat?

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