Class: RuboCop::Cop::Layout::HashAlignment
- 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.
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
Instance Attribute Summary collapse
-
#column_deltas ⇒ Object
Returns the value of attribute column_deltas.
-
#offenses_by ⇒ Object
Returns the value of attribute offenses_by.
Attributes inherited from Base
Instance Method Summary collapse
- #on_hash(node) ⇒ Object
- #on_send(node) ⇒ Object (also: #on_super, #on_yield)
Methods included from AutoCorrector
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, #string_literals_frozen_by_default?, support_autocorrect?, support_multiple_source?, #target_rails_version, #target_ruby_version
Methods included from ExcludeLimit
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
Constructor Details
This class inherits a constructor from RuboCop::Cop::Base
Instance Attribute Details
#column_deltas ⇒ Object
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_by ⇒ Object
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 |