Skip to content

Commit

Permalink
[Fix #7066] Fix Layout/AlignHash when mixed Hash styles are used (#7068)
Browse files Browse the repository at this point in the history
  • Loading branch information
rmm5t authored and bbatsov committed May 23, 2019
1 parent 2c82b01 commit 3384933
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Bug fixes

* [#7066](https://github.com/rubocop-hq/rubocop/issues/7066): Fix `Layout/AlignHash` when mixed Hash styles are used. ([@rmm5t][])
* [#7073](https://github.com/rubocop-hq/rubocop/issues/7073): Fix false positive in `Naming/RescuedExceptionsVariableName` cop. ([@tejasbubane][])

### Changes
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 3384933

Please sign in to comment.