class RuboCop::Cop::Layout::AlignHash

Check that the keys, separators, and values of a multi-line hash literal are aligned according to configuration. The configuration options are:

- key (left align keys, one space before hash rockets and values)
- separator (align hash rockets and colons, right align keys)
- table (left align keys, hash rockets, and values)

The treatment of hashes passed as the last argument to a method call can also be configured. The options are:

- always_inspect
- always_ignore
- ignore_implicit (without curly braces)

Alternatively you can specify multiple allowed styles. That's done by passing a list of styles to EnforcedStyles.

@example EnforcedHashRocketStyle: key (default)

# bad
{
  :foo => bar,
   :ba => baz
}
{
  :foo => bar,
  :ba  => baz
}

# good
{
  :foo => bar,
  :ba => baz
}

@example EnforcedHashRocketStyle: separator

# bad
{
  :foo => bar,
  :ba => baz
}
{
  :foo => bar,
  :ba  => baz
}

# good
{
  :foo => bar,
   :ba => baz
}

@example EnforcedHashRocketStyle: table

# bad
{
  :foo => bar,
   :ba => baz
}

# good
{
  :foo => bar,
  :ba  => baz
}

@example EnforcedColonStyle: key (default)

# bad
{
  foo: bar,
   ba: baz
}
{
  foo: bar,
  ba:  baz
}

# good
{
  foo: bar,
  ba: baz
}

@example EnforcedColonStyle: separator

# bad
{
  foo: bar,
  ba: baz
}

# good
{
  foo: bar,
   ba: baz
}

@example EnforcedColonStyle: table

# bad
{
  foo: bar,
  ba: baz
}

# good
{
  foo: bar,
  ba:  baz
}

@example EnforcedLastArgumentHashStyle: always_inspect (default)

# Inspect both implicit and explicit hashes.

# bad
do_something(foo: 1,
  bar: 2)

# bad
do_something({foo: 1,
  bar: 2})

# good
do_something(foo: 1,
             bar: 2)

# good
do_something(
  foo: 1,
  bar: 2
)

# good
do_something({foo: 1,
              bar: 2})

# good
do_something({
  foo: 1,
  bar: 2
})

@example EnforcedLastArgumentHashStyle: always_ignore

# Ignore both implicit and explicit hashes.

# good
do_something(foo: 1,
  bar: 2)

# good
do_something({foo: 1,
  bar: 2})

@example EnforcedLastArgumentHashStyle: ignore_implicit

# Ignore only implicit hashes.

# bad
do_something({foo: 1,
  bar: 2})

# good
do_something(foo: 1,
  bar: 2)

@example EnforcedLastArgumentHashStyle: ignore_explicit

# Ignore only explicit hashes.

# bad
do_something(foo: 1,
  bar: 2)

# good
do_something({foo: 1,
  bar: 2})

Constants

MSG

Attributes

column_deltas[RW]
offences_by[RW]

Public Instance Methods

autocorrect(node) click to toggle source
# File lib/rubocop/cop/layout/align_hash.rb, line 211
def autocorrect(node)
  delta = column_deltas[alignment_for(node).first.class][node]
  return if delta.nil?

  correct_node(node, delta)
end
on_hash(node) click to toggle source
# File lib/rubocop/cop/layout/align_hash.rb, line 199
def on_hash(node)
  return if ignored_node?(node)
  return if node.pairs.empty? || node.single_line?

  return unless alignment_for_hash_rockets
                .any? { |a| a.checkable_layout?(node) } &&
                alignment_for_colons
                .any? { |a| a.checkable_layout?(node) }

  check_pairs(node)
end
on_send(node) click to toggle source
# File lib/rubocop/cop/layout/align_hash.rb, line 185
def on_send(node)
  return if double_splat?(node)
  return unless node.arguments?

  last_argument = node.last_argument

  return unless last_argument.hash_type? &&
                ignore_hash_argument?(last_argument)

  ignore_node(last_argument)
end
Also aliased as: on_super, on_yield
on_super(node)
Alias for: on_send
on_yield(node)
Alias for: on_send

Private Instance Methods

add_offences() click to toggle source
# File lib/rubocop/cop/layout/align_hash.rb, line 247
def add_offences
  _format, offences = offences_by.min_by { |_, v| v.length }
  (offences || []).each do |offence|
    add_offense offence
  end
end
adjust(corrector, delta, range) click to toggle source
# File lib/rubocop/cop/layout/align_hash.rb, line 343
def adjust(corrector, delta, range)
  if delta.positive?
    corrector.insert_before(range, ' ' * delta)
  elsif delta.negative?
    range = range_between(range.begin_pos - delta.abs, range.begin_pos)
    corrector.remove(range)
  end
end
alignment_for(pair) click to toggle source
# File lib/rubocop/cop/layout/align_hash.rb, line 271
def alignment_for(pair)
  if pair.hash_rocket?
    alignment_for_hash_rockets
  else
    alignment_for_colons
  end
end
alignment_for_colons() click to toggle source
# File lib/rubocop/cop/layout/align_hash.rb, line 284
def alignment_for_colons
  @alignment_for_colons ||=
    new_alignment('EnforcedColonStyle')
end
alignment_for_hash_rockets() click to toggle source
# File lib/rubocop/cop/layout/align_hash.rb, line 279
def alignment_for_hash_rockets
  @alignment_for_hash_rockets ||=
    new_alignment('EnforcedHashRocketStyle')
end
check_delta(delta, node:, alignment:) click to toggle source
# File lib/rubocop/cop/layout/align_hash.rb, line 254
def check_delta(delta, node:, alignment:)
  offences_by[alignment.class] ||= []
  return if good_alignment? delta

  column_deltas[alignment.class][node] = delta
  offences_by[alignment.class].push(node)
end
check_pairs(node) click to toggle source
# File lib/rubocop/cop/layout/align_hash.rb, line 227
def check_pairs(node)
  first_pair = node.pairs.first
  self.offences_by = {}
  self.column_deltas = Hash.new { |hash, key| hash[key] = {} }

  alignment_for(first_pair).each do |alignment|
    delta = alignment.deltas_for_first_pair(first_pair, node)
    check_delta delta, node: first_pair, alignment: alignment
  end

  node.children.each do |current|
    alignment_for(current).each do |alignment|
      delta = alignment.deltas(first_pair, current)
      check_delta delta, node: current, alignment: alignment
    end
  end

  add_offences
end
correct_key_value(delta, key, value, separator) click to toggle source
# File lib/rubocop/cop/layout/align_hash.rb, line 307
def correct_key_value(delta, key, value, separator)
  # We can't use the instance variable inside the lambda. That would
  # just give each lambda the same reference and they would all get the
  # last value of each. Some local variables fix the problem.
  separator_delta = delta[:separator] || 0
  value_delta     = delta[:value]     || 0
  key_delta       = delta[:key]       || 0

  key_column = key.column
  key_delta = -key_column if key_delta < -key_column

  lambda do |corrector|
    adjust(corrector, key_delta, key)
    adjust(corrector, separator_delta, separator)
    adjust(corrector, value_delta, value)
  end
end
correct_no_value(key_delta, key) click to toggle source
# File lib/rubocop/cop/layout/align_hash.rb, line 303
def correct_no_value(key_delta, key)
  ->(corrector) { adjust(corrector, key_delta, key) }
end
correct_node(node, delta) click to toggle source
# File lib/rubocop/cop/layout/align_hash.rb, line 289
def correct_node(node, delta)
  # We can't use the instance variable inside the lambda. That would
  # just give each lambda the same reference and they would all get the
  # last value of each. A local variable fixes the problem.

  if !node.value
    correct_no_value(delta[:key] || 0, node.source_range)
  else
    correct_key_value(delta, node.key.source_range,
                      node.value.source_range,
                      node.loc.operator)
  end
end
double_splat?(node) click to toggle source
# File lib/rubocop/cop/layout/align_hash.rb, line 223
def double_splat?(node)
  node.children.last.is_a?(Symbol)
end
good_alignment?(column_deltas) click to toggle source
# File lib/rubocop/cop/layout/align_hash.rb, line 352
def good_alignment?(column_deltas)
  column_deltas.values.all?(&:zero?)
end
ignore_hash_argument?(node) click to toggle source
# File lib/rubocop/cop/layout/align_hash.rb, line 262
def ignore_hash_argument?(node)
  case cop_config['EnforcedLastArgumentHashStyle']
  when 'always_inspect'  then false
  when 'always_ignore'   then true
  when 'ignore_explicit' then node.braces?
  when 'ignore_implicit' then !node.braces?
  end
end
new_alignment(key) click to toggle source
# File lib/rubocop/cop/layout/align_hash.rb, line 325
def new_alignment(key)
  formats = cop_config[key]
  formats = [formats] if formats.is_a? String

  formats.uniq.map do |format|
    case format
    when 'key'
      KeyAlignment.new
    when 'table'
      TableAlignment.new
    when 'separator'
      SeparatorAlignment.new
    else
      raise "Unknown #{key}: #{formats}"
    end
  end
end