Skip to content

Commit

Permalink
[Fix rubocop#7066] Fix Layout/AlignHash when mixed Hash styles are used
Browse files Browse the repository at this point in the history
  • Loading branch information
rmm5t committed May 22, 2019
1 parent b82b3ef commit 9b073e6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## master (unreleased)

### Bug fixes

* [#7066](https://github.com/rubocop-hq/rubocop/issues/7066): Fix `Layout/AlignHash` when mixed Hash styles are used. ([@rmm5t][])

### Changes

* [#5976](https://github.com/rubocop-hq/rubocop/issues/5976): Warn for Rails Cops. ([@koic][])
Expand Down
4 changes: 4 additions & 0 deletions lib/rubocop/cop/mixin/hash_alignment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ def separator_delta(first_pair, current_pair, key_delta)
class TableAlignment
include ValueAlignment

def initialize
self.max_key_width = 0
end

def deltas_for_first_pair(first_pair, node)
self.max_key_width = node.keys.map { |key| key.source.length }.max

Expand Down
19 changes: 19 additions & 0 deletions spec/rubocop/cop/layout/align_hash_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,15 @@ def self.scenarios_order
RUBY
end

it 'accepts aligned hash keys with mixed hash style' do
expect_no_offenses(<<~RUBY)
headers = {
"Content-Type" => 0,
Authorization: 1
}
RUBY
end

it 'accepts an empty hash' do
expect_no_offenses('h = {}')
end
Expand Down Expand Up @@ -811,6 +820,16 @@ def self.scenarios_order
RUBY
end

it 'for misaligned hash keys with mixed hash style' do
expect_offense(<<~RUBY)
headers = {
"Content-Type" => 0,
Authorization: 1
^^^^^^^^^^^^^^^^ Align the elements of a hash literal if they span more than one line.
}
RUBY
end

it 'for misaligned hash values, works separate for each hash' do
expect_offense(<<-RUBY.strip_indent)
hash = {
Expand Down

0 comments on commit 9b073e6

Please sign in to comment.