123456789_123456789_123456789_123456789_123456789_

Module: RuboCop::Cop::HashAlignmentStyles::ValueAlignment

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Included In:
Defined in: lib/rubocop/cop/mixin/hash_alignment_styles.rb

Overview

Common functionality for checking alignment of hash values.

Instance Method Summary

Instance Method Details

#alignable_values?(node) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/hash_alignment_styles.rb', line 79

def alignable_values?(node)
  return false if node.pairs.first.value_omission?

  node.pairs.none? { |pair| value_on_later_line?(pair) }
end

#checkable_layout?(node) ⇒ Boolean

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/hash_alignment_styles.rb', line 57

def checkable_layout?(node)
  !node.pairs_on_same_line? && !node.mixed_delimiters? && alignable_values?(node) &&
    !pair_after_kwsplat_on_same_line?(node)
end

#deltas(first_pair, current_pair)

With several enforced styles, deltas are computed even for a hash this style can’t check. A pair that doesn’t begin its line can’t be moved without deleting whatever precedes it, so it’s left alone.

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/hash_alignment_styles.rb', line 65

def deltas(first_pair, current_pair)
  return {} unless current_pair.equal?(first_pair) ||
                   Util.begins_its_line?(current_pair.source_range)

  key_delta = key_delta(first_pair, current_pair)
  shift = separator_shift(current_pair, key_delta)
  separator_delta = separator_delta(first_pair, current_pair, shift)
  value_delta = value_delta(first_pair, current_pair) - shift - separator_delta

  { key: key_delta, separator: separator_delta, value: value_delta }
end

#pair_after_kwsplat_on_same_line?(node) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/hash_alignment_styles.rb', line 89

def pair_after_kwsplat_on_same_line?(node)
  first_pair = node.pairs.first

  node.children.each_cons(2).any? do |previous, current|
    current.pair_type? && !current.equal?(first_pair) &&
      previous.last_line == current.first_line
  end
end

#separator_delta(first_pair, current_pair, shift) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/hash_alignment_styles.rb', line 102

def separator_delta(first_pair, current_pair, shift)
  if current_pair.hash_rocket?
    hash_rocket_delta(first_pair, current_pair) - shift
  else
    0
  end
end

#separator_shift(current_pair, key_delta) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/hash_alignment_styles.rb', line 98

def separator_shift(current_pair, key_delta)
  current_pair.key.multiline? ? 0 : key_delta
end

#value_on_later_line?(pair) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/hash_alignment_styles.rb', line 85

def value_on_later_line?(pair)
  pair.value && pair.key.last_line != pair.value.first_line
end