Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix #10751] Fix autocorrect for Layout/FirstHashElementIndentation #10759

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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