Skip to content

Commit

Permalink
[Fix rubocop#10751] Fix autocorrect for Layout/FirstHashElementIndent…
Browse files Browse the repository at this point in the history
…ation

Fixes rubocop#10751

`Layout/FirstHashElementIndentation` should allow a hash where the content of the first pair is indented relative to the base column specified with configured cop style, and the second pair begins on the same line with the right brace of the first pair; because those two keys won't be aligned with each other by `Layout/HashAlignment`, not like the case of rubocop#10689.

With no parenthesis
```ruby
post :create, params: {
  foo: { bar: 'Baz' }
}, as: :json
```

With the parenthesis and `special_inside_parentheses` style
```ruby
post(:create, params: {
       foo: { bar: 'Baz' }
     }, as: :json)
```

With the parenthesis and `consistent` style
```ruby
post(:create, params: {
  foo: { bar: 'Baz' }
}, as: :json)
```
  • Loading branch information
j-miyake committed Jun 28, 2022
1 parent f5b77c8 commit 9ca3076
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 3 deletions.
@@ -0,0 +1 @@
* [#10751](https://github.com/rubocop/rubocop/issues/10751): Fix autocorrect for Layout/FirstHashElementIndentation. ([@j-miyake][])
7 changes: 6 additions & 1 deletion lib/rubocop/cop/mixin/multiline_element_indentation.rb
Expand Up @@ -51,7 +51,8 @@ def indent_base(left_brace, left_parenthesis)
return [left_brace.column, :left_brace_or_bracket] if style == brace_alignment_style

pair = hash_pair_where_value_beginning_with(left_brace)
if pair && key_and_value_begin_on_same_line?(pair) && pair.right_sibling
if pair && key_and_value_begin_on_same_line?(pair) &&
right_sibling_begins_on_subsequent_line?(pair)
return [pair.loc.column, :parent_hash_key]
end

Expand Down Expand Up @@ -79,6 +80,10 @@ def key_and_value_begin_on_same_line?(pair)
same_line?(pair.key, pair.value)
end

def right_sibling_begins_on_subsequent_line?(pair)
pair.right_sibling && (pair.last_line < pair.right_sibling.first_line)
end

def detected_styles(actual_column, offset, left_parenthesis, left_brace)
base_column = actual_column - configured_indentation_width - offset
detected_styles_for_column(base_column, left_parenthesis, left_brace)
Expand Down
41 changes: 40 additions & 1 deletion spec/rubocop/cop/layout/first_array_element_indentation_spec.rb
Expand Up @@ -301,6 +301,19 @@
RUBY
end

it 'accepts indent based on the preceding left parenthesis ' \
'when the right bracket and its following pair is on the same line' do
expect_no_offenses(<<~RUBY)
func(:x, y: [
:a,
:b
], z: [
:c,
:d
])
RUBY
end

it 'accepts indent based on the left brace when the outer hash key and ' \
'the left bracket is not on the same line' do
expect_no_offenses(<<~RUBY)
Expand Down Expand Up @@ -403,6 +416,19 @@
RUBY
end

it 'accepts indent based on the start of the line where the left bracket is' \
'when the right bracket and its following pair is on the same line' do
expect_no_offenses(<<~RUBY)
func(:x, y: [
:a,
:b
], z: [
:c,
:d
])
RUBY
end

it 'accepts indent based on the left brace when the outer hash key and ' \
'the left bracket is not on the same line' do
expect_no_offenses(<<~RUBY)
Expand Down Expand Up @@ -489,7 +515,20 @@
RUBY
end

it 'accepts indent based on the left brace when the outer hash key and ' \
it 'accepts indent based on the start of the line where the left bracket is' \
'when the right bracket and its following pair is on the same line' do
expect_no_offenses(<<~RUBY)
func :x, y: [
:a,
:b
], z: [
:c,
:d
]
RUBY
end

it 'accepts indent based on the left bracket when the outer hash key and ' \
'the left bracket is not on the same line' do
expect_no_offenses(<<~RUBY)
func x:
Expand Down
41 changes: 40 additions & 1 deletion spec/rubocop/cop/layout/first_hash_element_indentation_spec.rb
Expand Up @@ -385,6 +385,19 @@
RUBY
end

it 'accepts indent based on the preceding left parenthesis' \
'when the right brace and its following pair is on the same line' do
expect_no_offenses(<<~RUBY)
func(:x, y: {
a: 1,
b: 2
}, z: {
c: 1,
d: 2
})
RUBY
end

it 'accepts indent based on the left brace when the outer hash key and ' \
'the left brace is not on the same line' do
expect_no_offenses(<<~RUBY)
Expand Down Expand Up @@ -487,6 +500,19 @@
RUBY
end

it 'accepts indent based on the start of the line where the left brace is' \
'when the right brace and its following pair is on the same line' do
expect_no_offenses(<<~RUBY)
func(:x, y: {
a: 1,
b: 2
}, z: {
c: 1,
d: 2
})
RUBY
end

it 'accepts indent based on the left brace when the outer hash key and ' \
'the left brace is not on the same line' do
expect_no_offenses(<<~RUBY)
Expand Down Expand Up @@ -534,7 +560,7 @@
end

it 'registers an offense for the first inner hash member not based on the start of line ' \
'where the outer hash key is when no other outer hash members follow' do
'when the outer hash pair has no following siblings' do
expect_offense(<<~RUBY)
func x: :foo, y: {
a: 1, b: 2 }
Expand Down Expand Up @@ -574,6 +600,19 @@
RUBY
end

it 'accepts indent based on the start of the line where the left brace is' \
'when the right brace and its following pair is on the same line' do
expect_no_offenses(<<~RUBY)
func :x, y: {
a: 1,
b: 2
}, z: {
c: 1,
d: 2
}
RUBY
end

it 'accepts indent based on the left brace when the outer hash key and ' \
'the left brace is not on the same line' do
expect_no_offenses(<<~RUBY)
Expand Down

0 comments on commit 9ca3076

Please sign in to comment.