diff --git a/lib/rubocop/cop/layout/hash_alignment.rb b/lib/rubocop/cop/layout/hash_alignment.rb index 64ba0155571..d8951f5d34c 100644 --- a/lib/rubocop/cop/layout/hash_alignment.rb +++ b/lib/rubocop/cop/layout/hash_alignment.rb @@ -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) @@ -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 diff --git a/spec/rubocop/cop/layout/hash_alignment_spec.rb b/spec/rubocop/cop/layout/hash_alignment_spec.rb index 2b9eaa96403..82494c12a7f 100644 --- a/spec/rubocop/cop/layout/hash_alignment_spec.rb +++ b/spec/rubocop/cop/layout/hash_alignment_spec.rb @@ -50,7 +50,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 @@ -58,7 +58,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 @@ -66,7 +66,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 @@ -74,7 +74,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 @@ -82,7 +82,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 @@ -90,7 +90,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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 } @@ -355,7 +355,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 @@ -363,7 +363,7 @@ def example 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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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