lib/rubocop/cop/layout/hash_alignment.rb in rubocop-1.15.0 vs lib/rubocop/cop/layout/hash_alignment.rb in rubocop-1.16.0

- old
+ new

@@ -183,11 +183,13 @@ 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.' }.freeze + '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 def on_send(node) return if double_splat?(node) return unless node.arguments? @@ -199,11 +201,11 @@ end alias on_super on_send alias on_yield on_send def on_hash(node) - return if enforce_first_argument_with_fixed_indentation? || ignored_node?(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) @@ -212,10 +214,14 @@ attr_accessor :offences_by, :column_deltas private + def autocorrect_incompatible_with_other_cops?(node) + enforce_first_argument_with_fixed_indentation? && !node.braces? && node.parent&.call_type? + end + def reset! self.offences_by = {} self.column_deltas = Hash.new { |hash, key| hash[key] = {} } end @@ -241,11 +247,18 @@ add_offences end def add_offences + kwsplat_offences = offences_by.delete(KeywordSplatAlignment) + register_offences_with_format(kwsplat_offences, KeywordSplatAlignment) + format, offences = offences_by.min_by { |_, v| v.length } + register_offences_with_format(offences, format) + end + + def register_offences_with_format(offences, format) (offences || []).each do |offence| add_offense(offence, message: MESSAGES[format]) do |corrector| delta = column_deltas[alignment_for(offence).first.class][offence] correct_node(corrector, offence, delta) unless delta.nil? @@ -269,10 +282,12 @@ when 'ignore_implicit' then !node.braces? end end def alignment_for(pair) - if pair.hash_rocket? + if pair.kwsplat_type? + [KeywordSplatAlignment.new] + elsif pair.hash_rocket? alignment_for_hash_rockets else alignment_for_colons end end