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 #9811] Fix an error for Layout/ArgumentAlignment #9812

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
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