class RuboCop::Cop::HashAlignmentStyles::KeyAlignment

Handles calculation of deltas when the enforced style is 'key'.

Public Instance Methods

checkable_layout?(_node) click to toggle source
# File lib/rubocop/cop/mixin/hash_alignment_styles.rb, line 9
def checkable_layout?(_node)
  true
end
deltas(first_pair, current_pair) click to toggle source
# File lib/rubocop/cop/mixin/hash_alignment_styles.rb, line 20
def deltas(first_pair, current_pair)
  if Util.begins_its_line?(current_pair.source_range)
    key_delta = first_pair.key_delta(current_pair)
    separator_delta = separator_delta(current_pair)
    value_delta = value_delta(current_pair)

    { key: key_delta, separator: separator_delta, value: value_delta }
  else
    {}
  end
end
deltas_for_first_pair(first_pair, _node) click to toggle source
# File lib/rubocop/cop/mixin/hash_alignment_styles.rb, line 13
def deltas_for_first_pair(first_pair, _node)
  {
    separator: separator_delta(first_pair),
    value: value_delta(first_pair)
  }
end

Private Instance Methods

separator_delta(pair) click to toggle source
# File lib/rubocop/cop/mixin/hash_alignment_styles.rb, line 34
def separator_delta(pair)
  if pair.hash_rocket?
    correct_separator_column = pair.key.loc.expression.end.column + 1
    actual_separator_column = pair.loc.operator.column

    correct_separator_column - actual_separator_column
  else
    0
  end
end
value_delta(pair) click to toggle source
# File lib/rubocop/cop/mixin/hash_alignment_styles.rb, line 45
def value_delta(pair)
  return 0 if pair.value_on_new_line? || pair.value_omission?

  correct_value_column = pair.loc.operator.end.column + 1
  actual_value_column = pair.value.loc.column

  correct_value_column - actual_value_column
end