Class: RuboCop::Cop::Layout::HashAlignment

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
HashAlignmentStyles, RangeHelp
Defined in:
lib/rubocop/cop/layout/hash_alignment.rb

Overview

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.

Examples:

EnforcedHashRocketStyle: key (default)

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

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

EnforcedHashRocketStyle: separator

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

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

EnforcedHashRocketStyle: table

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

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

EnforcedColonStyle: key (default)

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

# good
{
  foo: bar,
  ba: baz
}

EnforcedColonStyle: separator

# bad
{
  foo: bar,
  ba: baz
}

# good
{
  foo: bar,
   ba: baz
}

EnforcedColonStyle: table

# bad
{
  foo: bar,
  ba: baz
}

# good
{
  foo: bar,
  ba:  baz
}

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
})

EnforcedLastArgumentHashStyle: always_ignore

# Ignore both implicit and explicit hashes.

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

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

EnforcedLastArgumentHashStyle: ignore_implicit

# Ignore only implicit hashes.

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

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

EnforcedLastArgumentHashStyle: ignore_explicit

# Ignore only explicit hashes.

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

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

Constant Summary collapse

MESSAGES =
{
  KeyAlignment => 'Align the keys of a hash literal if they span more than one line.',
  SeparatorAlignment => 'Align the separators of a hash literal if they span more than ' \
                        'one line.',
  TableAlignment => 'Align the keys and values of a hash literal if they span more than ' \
                    'one line.',
  KeywordSplatAlignment => 'Align keyword splats with the rest of the hash if it spans ' \
                           'more than one line.'
}.freeze
SEPARATOR_ALIGNMENT_STYLES =
%w[EnforcedColonStyle EnforcedHashRocketStyle].freeze

Constants inherited from Base

Base::RESTRICT_ON_SEND

Instance Attribute Summary collapse

Attributes inherited from Base

#config, #processed_source

Instance Method Summary collapse

Methods included from AutoCorrector

support_autocorrect?

Methods inherited from Base

#active_support_extensions_enabled?, #add_global_offense, #add_offense, #always_autocorrect?, autocorrect_incompatible_with, badge, #begin_investigation, callbacks_needed, #callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #contextual_autocorrect?, #cop_config, cop_name, #cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, #initialize, #inspect, joining_forces, lint?, match?, #message, #offenses, #on_investigation_end, #on_new_investigation, #on_other_file, #parse, #parser_engine, #ready, #relevant_file?, requires_gem, support_autocorrect?, support_multiple_source?, #target_rails_version, #target_ruby_version

Methods included from ExcludeLimit

#exclude_limit

Methods included from AutocorrectLogic

#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #autocorrect_with_disable_uncorrectable?, #correctable?, #disable_uncorrectable?, #safe_autocorrect?

Methods included from IgnoredNode

#ignore_node, #ignored_node?, #part_of_ignored_node?

Methods included from Util

silence_warnings

Constructor Details

This class inherits a constructor from RuboCop::Cop::Base

Instance Attribute Details

#column_deltasObject

Returns the value of attribute column_deltas.



218
219
220
# File 'lib/rubocop/cop/layout/hash_alignment.rb', line 218

def column_deltas
  @column_deltas
end

#offenses_byObject

Returns the value of attribute offenses_by.



218
219
220
# File 'lib/rubocop/cop/layout/hash_alignment.rb', line 218

def offenses_by
  @offenses_by
end

Instance Method Details

#on_hash(node) ⇒ Object



208
209
210
211
212
213
214
215
216
# File 'lib/rubocop/cop/layout/hash_alignment.rb', line 208

def on_hash(node)
  return if autocorrect_incompatible_with_other_cops?(node) || ignored_node?(node) ||
            node.pairs.empty? || node.single_line?

  proc = ->(a) { a.checkable_layout?(node) }
  return unless alignment_for_hash_rockets.any?(proc) && alignment_for_colons.any?(proc)

  check_pairs(node)
end

#on_send(node) ⇒ Object Also known as: on_super, on_yield



195
196
197
198
199
200
201
202
203
204
# File 'lib/rubocop/cop/layout/hash_alignment.rb', line 195

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