Skip to content

Commit

Permalink
[Fix rubocop#9811] Fix an error for Layout/ArgumentAlignment
Browse files Browse the repository at this point in the history
Fixes rubocop#9811.

This PR fixes an error for `Layout/ArgumentAlignment` with
`Layout/FirstHashElementIndentation` when setting
`EnforcedStyle: with_fixed_indentation`.
  • Loading branch information
koic committed May 19, 2021
1 parent b3f37bc commit 8b56098
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 3 deletions.
1 change: 1 addition & 0 deletions changelog/fix_an_error_for_layout_argument_alignment.md
@@ -0,0 +1 @@
* [#9811](https://github.com/rubocop/rubocop/issues/9811): Fix an error for `Layout/ArgumentAlignment` with `Layout/FirstHashElementIndentation` when setting `EnforcedStyle: with_fixed_indentation`. ([@koic][])
5 changes: 3 additions & 2 deletions lib/rubocop/cop/layout/argument_alignment.rb
Expand Up @@ -56,8 +56,9 @@ def on_send(node)
first_arg = node.first_argument
return if !multiple_arguments?(node, first_arg) || node.send_type? && node.method?(:[]=)

if first_arg.hash_type?
check_alignment(first_arg.pairs, base_column(node, first_arg.pairs.first))
if first_arg.hash_type? && !first_arg.braces?
pairs = first_arg.pairs
check_alignment(pairs, base_column(node, pairs.first))
else
check_alignment(node.arguments, base_column(node, first_arg))
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/layout/hash_alignment.rb
Expand Up @@ -215,7 +215,7 @@ def on_hash(node)
private

def autocorrect_incompatible_with_other_cops?(node)
enforce_first_argument_with_fixed_indentation? && node.parent&.call_type?
enforce_first_argument_with_fixed_indentation? && !node.braces? && node.parent&.call_type?
end

def reset!
Expand Down
46 changes: 46 additions & 0 deletions spec/rubocop/cli/autocorrect_spec.rb
Expand Up @@ -1871,6 +1871,52 @@ def do_even_more_stuff
RUBY
end

it 'corrects when specifying `EnforcedStyle: with_fixed_indentation` of `Layout/ArgumentAlignment` and ' \
'`Layout/HashAlignment` and `Layout/FirstHashElementIndentation`' do
create_file('example.rb', <<~RUBY)
do_something(
{
foo: 'bar',
baz: 'qux'
}
)
do_something(
foo: 'bar',
baz: 'qux'
)
RUBY

create_file('.rubocop.yml', <<~YAML)
Layout/ArgumentAlignment:
EnforcedStyle: with_fixed_indentation
YAML

expect(
cli.run(
[
'--auto-correct',
'--only',
'Layout/ArgumentAlignment,Layout/HashAlignment,Layout/FirstHashElementIndentation'
]
)
).to eq(0)
expect($stderr.string).to eq('')
expect(File.read('example.rb')).to eq(<<~RUBY)
do_something(
{
foo: 'bar',
baz: 'qux'
}
)
do_something(
foo: 'bar',
baz: 'qux'
)
RUBY
end

it 'does not crash Lint/SafeNavigationWithEmpty and offenses and accepts Style/SafeNavigation ' \
'when checking `foo&.empty?` in a conditional' do
create_file('example.rb', <<~RUBY)
Expand Down
11 changes: 11 additions & 0 deletions spec/rubocop/cop/layout/argument_alignment_spec.rb
Expand Up @@ -594,5 +594,16 @@ class MyModel < ActiveRecord::Base
end
end
end

it 'does not register an offense when using aligned braced hash as a argument' do
expect_no_offenses(<<~RUBY)
do_something(
{
foo: 'bar',
baz: 'qux'
}
)
RUBY
end
end
end

0 comments on commit 8b56098

Please sign in to comment.