Skip to content

Commit

Permalink
[Fix #9312] Fix Layout/FirstHashElementLineBreak for single line hash
Browse files Browse the repository at this point in the history
Fix Layout/FirstHashElementLineBreak to consider below as a multi-line
hash even though it only has one element:

    {:foo => {
      :bar => "baz",
    }}

Now FirstHashElementLineBreak will correct the above to:

    {
      :foo => {
        :bar => "baz",
      },
    }

Fixes #9312.
  • Loading branch information
Muir Manders authored and bbatsov committed Jan 4, 2021
1 parent 1b8388f commit 1d75a82
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#9312](https://github.com/rubocop-hq/rubocop/issues/9312): Fix `Layout/FirstHashElementLineBreak` to apply to multi-line hashes with only a single element. ([@muirdm][])
2 changes: 1 addition & 1 deletion lib/rubocop/cop/mixin/first_element_line_break.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def method_uses_parens?(node, limit)
end

def check_children_line_break(node, children, start = node)
return if children.size < 2
return if children.empty?

line = start.first_line

Expand Down
16 changes: 16 additions & 0 deletions spec/rubocop/cop/layout/first_hash_element_line_break_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,22 @@
RUBY
end

it 'registers an offense and corrects single element multi-line hash' do
expect_offense(<<~RUBY)
{ foo: {
^^^^^^ Add a line break before the first element of a multi-line hash.
bar: 2,
} }
RUBY

expect_correction(<<~RUBY)
{#{trailing_whitespace}
foo: {
bar: 2,
} }
RUBY
end

it 'ignores implicit hashes in method calls with parens' do
expect_no_offenses(<<~RUBY)
method(
Expand Down

0 comments on commit 1d75a82

Please sign in to comment.