Skip to content

Commit

Permalink
[Fix #7291] Improve the offense message for Layout/HashAlignment (#7547)
Browse files Browse the repository at this point in the history
Now it displays different messages based on the currently enforced style.
  • Loading branch information
jsartin513 authored and bbatsov committed Dec 30, 2019
1 parent 6fd61ce commit 371da87
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 48 deletions.
12 changes: 8 additions & 4 deletions lib/rubocop/cop/layout/hash_alignment.rb
Expand Up @@ -179,8 +179,12 @@ class HashAlignment < Cop
include HashAlignmentStyles
include RangeHelp

MSG = 'Align the elements of a hash literal if they span more than ' \
'one line.'
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

def on_send(node)
return if double_splat?(node)
Expand Down Expand Up @@ -249,9 +253,9 @@ def check_pairs(node)
end

def add_offences
_format, offences = offences_by.min_by { |_, v| v.length }
format, offences = offences_by.min_by { |_, v| v.length }
(offences || []).each do |offence|
add_offense offence
add_offense(offence, message: MESSAGES[format])
end
end

Expand Down
88 changes: 44 additions & 44 deletions spec/rubocop/cop/layout/hash_alignment_spec.rb
Expand Up @@ -50,47 +50,47 @@ def example
expect_offense(<<~RUBY)
func(a: 0,
b: 1)
^^^^ Align the elements of a hash literal if they span more than one line.
^^^^ Align the keys of a hash literal if they span more than one line.
RUBY
end

it 'registers offense for misaligned keys in explicit hash' do
expect_offense(<<~RUBY)
func({a: 0,
b: 1})
^^^^ Align the elements of a hash literal if they span more than one line.
^^^^ Align the keys of a hash literal if they span more than one line.
RUBY
end

it 'registers offense for misaligned keys in implicit hash for super' do
expect_offense(<<~RUBY)
super(a: 0,
b: 1)
^^^^ Align the elements of a hash literal if they span more than one line.
^^^^ Align the keys of a hash literal if they span more than one line.
RUBY
end

it 'registers offense for misaligned keys in explicit hash for super' do
expect_offense(<<~RUBY)
super({a: 0,
b: 1})
^^^^ Align the elements of a hash literal if they span more than one line.
^^^^ Align the keys of a hash literal if they span more than one line.
RUBY
end

it 'registers offense for misaligned keys in implicit hash for yield' do
expect_offense(<<~RUBY)
yield(a: 0,
b: 1)
^^^^ Align the elements of a hash literal if they span more than one line.
^^^^ Align the keys of a hash literal if they span more than one line.
RUBY
end

it 'registers offense for misaligned keys in explicit hash for yield' do
expect_offense(<<~RUBY)
yield({a: 0,
b: 1})
^^^^ Align the elements of a hash literal if they span more than one line.
^^^^ Align the keys of a hash literal if they span more than one line.
RUBY
end
end
Expand Down Expand Up @@ -163,7 +163,7 @@ def example
expect_offense(<<~RUBY)
func({a: 0,
b: 1})
^^^^ Align the elements of a hash literal if they span more than one line.
^^^^ Align the keys of a hash literal if they span more than one line.
RUBY
end

Expand All @@ -178,7 +178,7 @@ def example
expect_offense(<<~RUBY)
super({a: 0,
b: 1})
^^^^ Align the elements of a hash literal if they span more than one line.
^^^^ Align the keys of a hash literal if they span more than one line.
RUBY
end

Expand All @@ -193,7 +193,7 @@ def example
expect_offense(<<~RUBY)
yield({a: 0,
b: 1})
^^^^ Align the elements of a hash literal if they span more than one line.
^^^^ Align the keys of a hash literal if they span more than one line.
RUBY
end
end
Expand All @@ -209,7 +209,7 @@ def example
expect_offense(<<~RUBY)
func(a: 0,
b: 1)
^^^^ Align the elements of a hash literal if they span more than one line.
^^^^ Align the keys of a hash literal if they span more than one line.
RUBY
end

Expand All @@ -224,7 +224,7 @@ def example
expect_offense(<<~RUBY)
super(a: 0,
b: 1)
^^^^ Align the elements of a hash literal if they span more than one line.
^^^^ Align the keys of a hash literal if they span more than one line.
RUBY
end

Expand All @@ -239,7 +239,7 @@ def example
expect_offense(<<~RUBY)
yield(a: 0,
b: 1)
^^^^ Align the elements of a hash literal if they span more than one line.
^^^^ Align the keys of a hash literal if they span more than one line.
RUBY
end

Expand All @@ -257,12 +257,12 @@ def example
hash1 = {
a: 0,
bb: 1
^^^^^ Align the elements of a hash literal if they span more than one line.
^^^^^ Align the keys of a hash literal if they span more than one line.
}
hash2 = {
'ccc' => 2,
'dddd' => 2
^^^^^^^^^^^^^ Align the elements of a hash literal if they span more than one line.
^^^^^^^^^^^^^ Align the keys of a hash literal if they span more than one line.
}
RUBY
end
Expand All @@ -271,7 +271,7 @@ def example
expect_offense(<<~RUBY)
hash = { a: 1, b: 2,
c: 3 }
^^^^ Align the elements of a hash literal if they span more than one line.
^^^^ Align the keys of a hash literal if they span more than one line.
RUBY
end

Expand All @@ -296,15 +296,15 @@ def example
expect_offense(<<~RUBY)
hash1 = {
a: 0,
^^^^^^ Align the elements of a hash literal if they span more than one line.
^^^^^^ Align the keys of a hash literal if they span more than one line.
bb:1,
^^^^ Align the elements of a hash literal if they span more than one line.
^^^^ Align the keys of a hash literal if they span more than one line.
}
hash2 = {
'ccc'=> 2,
^^^^^^^^^ Align the elements of a hash literal if they span more than one line.
^^^^^^^^^ Align the keys of a hash literal if they span more than one line.
'dddd' => 3
^^^^^^^^^^^^ Align the elements of a hash literal if they span more than one line.
^^^^^^^^^^^^ Align the keys of a hash literal if they span more than one line.
}
RUBY
end
Expand All @@ -314,7 +314,7 @@ def example
hash = {
'a' => 0,
'bbb' => 1
^^^^^^^^^^ Align the elements of a hash literal if they span more than one line.
^^^^^^^^^^ Align the keys of a hash literal if they span more than one line.
}
RUBY
end
Expand All @@ -323,7 +323,7 @@ def example
expect_offense(<<~RUBY)
hash = {
'a' => 0,
^^^^^^^^^^ Align the elements of a hash literal if they span more than one line.
^^^^^^^^^^ Align the keys of a hash literal if they span more than one line.
'bbb' => 1
}
RUBY
Expand All @@ -333,7 +333,7 @@ def example
expect_offense(<<~RUBY)
hash = {
'a' => (
^^^^^^^^^ Align the elements of a hash literal if they span more than one line.
^^^^^^^^^ Align the keys of a hash literal if they span more than one line.
),
'bbb' => 1
}
Expand All @@ -355,15 +355,15 @@ def example
expect_offense(<<~RUBY)
func(a: 0,
b: 1)
^^^^ Align the elements of a hash literal if they span more than one line.
^^^^ Align the keys of a hash literal if they span more than one line.
RUBY
end

it 'registers an offense for right alignment of keys' do
expect_offense(<<~RUBY)
func(a: 0,
bbb: 1)
^^^^^^ Align the elements of a hash literal if they span more than one line.
^^^^^^ Align the keys of a hash literal if they span more than one line.
RUBY
end

Expand Down Expand Up @@ -528,13 +528,13 @@ def self.scenarios_order
expect_offense(<<~RUBY)
hash1 = {
'a' => 0,
^^^^^^^^^^^ Align the elements of a hash literal if they span more than one line.
^^^^^^^^^^^ Align the keys and values of a hash literal if they span more than one line.
'bbb' => 1
}
hash2 = {
a: 0,
bbb:1
^^^^^ Align the elements of a hash literal if they span more than one line.
^^^^^ Align the keys and values of a hash literal if they span more than one line.
}
RUBY
end
Expand All @@ -543,15 +543,15 @@ def self.scenarios_order
expect_offense(<<~RUBY)
hash1 = {
'a' => 0,
^^^^^^^^^^^ Align the elements of a hash literal if they span more than one line.
^^^^^^^^^^^ Align the keys and values of a hash literal if they span more than one line.
'bbb' => 1
^^^^^^^^^^^^ Align the elements of a hash literal if they span more than one line.
^^^^^^^^^^^^ Align the keys and values of a hash literal if they span more than one line.
}
hash2 = {
a: 0,
^^^^^ Align the elements of a hash literal if they span more than one line.
^^^^^ Align the keys and values of a hash literal if they span more than one line.
bbb: 1
^^^^^^ Align the elements of a hash literal if they span more than one line.
^^^^^^ Align the keys and values of a hash literal if they span more than one line.
}
RUBY
end
Expand All @@ -561,7 +561,7 @@ def self.scenarios_order
hash = {
'a' => 0,
'bbb' => 1
^^^^^^^^^^^ Align the elements of a hash literal if they span more than one line.
^^^^^^^^^^^ Align the keys and values of a hash literal if they span more than one line.
}
RUBY
end
Expand Down Expand Up @@ -648,7 +648,7 @@ def self.scenarios_order
hash = {
'a' => 0,
'bbb' => 1
^^^^^^^^^^ Align the elements of a hash literal if they span more than one line.
^^^^^^^^^^ Align the separators of a hash literal if they span more than one line.
}
RUBY
end
Expand All @@ -658,7 +658,7 @@ def self.scenarios_order
hash = {
'a' => 0,
'bbb' => 1
^^^^^^^^^^^ Align the elements of a hash literal if they span more than one line.
^^^^^^^^^^^ Align the separators of a hash literal if they span more than one line.
}
RUBY
end
Expand Down Expand Up @@ -783,9 +783,9 @@ def self.scenarios_order
expect_offense(<<~RUBY)
hash = {
'a' => 0,
^^^^^^^^^ Align the elements of a hash literal if they span more than one line.
^^^^^^^^^ Align the keys of a hash literal if they span more than one line.
'bbb' => 1
^^^^^^^^^^ Align the elements of a hash literal if they span more than one line.
^^^^^^^^^^ Align the keys of a hash literal if they span more than one line.
}
RUBY
end
Expand All @@ -798,9 +798,9 @@ def self.scenarios_order
'gijk' => 0,
'a' => 0,
'b' => 1,
^^^^^^^^ Align the elements of a hash literal if they span more than one line.
^^^^^^^^ Align the keys and values of a hash literal if they span more than one line.
'c' => 1
^^^^^^^^ Align the elements of a hash literal if they span more than one line.
^^^^^^^^ Align the keys and values of a hash literal if they span more than one line.
}
RUBY
end
Expand All @@ -810,12 +810,12 @@ def self.scenarios_order
hash = {
'abcdefg' => 0,
'abcdef' => 0,
^^^^^^^^^^^^^^ Align the elements of a hash literal if they span more than one line.
^^^^^^^^^^^^^^ Align the keys of a hash literal if they span more than one line.
'gijk' => 0,
'a' => 0,
'b' => 1,
'c' => 1
^^^^^^^^ Align the elements of a hash literal if they span more than one line.
^^^^^^^^ Align the keys of a hash literal if they span more than one line.
}
RUBY
end
Expand All @@ -825,7 +825,7 @@ def self.scenarios_order
headers = {
"Content-Type" => 0,
Authorization: 1
^^^^^^^^^^^^^^^^ Align the elements of a hash literal if they span more than one line.
^^^^^^^^^^^^^^^^ Align the keys of a hash literal if they span more than one line.
}
RUBY
end
Expand All @@ -835,14 +835,14 @@ def self.scenarios_order
hash = {
'abcdefg' => 0,
'abcdef' => 0,
^^^^^^^^^^^^^^ Align the elements of a hash literal if they span more than one line.
^^^^^^^^^^^^^^ Align the keys of a hash literal if they span more than one line.
'gijk' => 0
}
hash = {
'abcdefg' => 0,
'abcdef' => 0,
^^^^^^^^^^^^^^^^^^^ Align the elements of a hash literal if they span more than one line.
^^^^^^^^^^^^^^^^^^^ Align the keys of a hash literal if they span more than one line.
'gijk' => 0
}
RUBY
Expand Down Expand Up @@ -940,12 +940,12 @@ def self.scenarios_order
hash1 = {
a: 0,
bbb: 1
^^^^^^ Align the elements of a hash literal if they span more than one line.
^^^^^^ Align the separators of a hash literal if they span more than one line.
}
hash2 = {
'a' => 0,
'bbb' => 1
^^^^^^^^^^ Align the elements of a hash literal if they span more than one line.
^^^^^^^^^^ Align the keys of a hash literal if they span more than one line.
}
RUBY
end
Expand Down

0 comments on commit 371da87

Please sign in to comment.