From 8b56098d85e6e7f97a0d195cfd4ded158ba410ce Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Wed, 19 May 2021 01:56:41 +0900 Subject: [PATCH] [Fix #9811] Fix an error for `Layout/ArgumentAlignment` Fixes #9811. This PR fixes an error for `Layout/ArgumentAlignment` with `Layout/FirstHashElementIndentation` when setting `EnforcedStyle: with_fixed_indentation`. --- ..._an_error_for_layout_argument_alignment.md | 1 + lib/rubocop/cop/layout/argument_alignment.rb | 5 +- lib/rubocop/cop/layout/hash_alignment.rb | 2 +- spec/rubocop/cli/autocorrect_spec.rb | 46 +++++++++++++++++++ .../cop/layout/argument_alignment_spec.rb | 11 +++++ 5 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 changelog/fix_an_error_for_layout_argument_alignment.md diff --git a/changelog/fix_an_error_for_layout_argument_alignment.md b/changelog/fix_an_error_for_layout_argument_alignment.md new file mode 100644 index 00000000000..30584f763b5 --- /dev/null +++ b/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][]) diff --git a/lib/rubocop/cop/layout/argument_alignment.rb b/lib/rubocop/cop/layout/argument_alignment.rb index b29d17cf56d..1bac3d6df34 100644 --- a/lib/rubocop/cop/layout/argument_alignment.rb +++ b/lib/rubocop/cop/layout/argument_alignment.rb @@ -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 diff --git a/lib/rubocop/cop/layout/hash_alignment.rb b/lib/rubocop/cop/layout/hash_alignment.rb index e8b3af0cfe3..a55f5011c89 100644 --- a/lib/rubocop/cop/layout/hash_alignment.rb +++ b/lib/rubocop/cop/layout/hash_alignment.rb @@ -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! diff --git a/spec/rubocop/cli/autocorrect_spec.rb b/spec/rubocop/cli/autocorrect_spec.rb index ca63408999d..1addd5a1656 100644 --- a/spec/rubocop/cli/autocorrect_spec.rb +++ b/spec/rubocop/cli/autocorrect_spec.rb @@ -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) diff --git a/spec/rubocop/cop/layout/argument_alignment_spec.rb b/spec/rubocop/cop/layout/argument_alignment_spec.rb index 4d797c47be5..0f00c7276da 100644 --- a/spec/rubocop/cop/layout/argument_alignment_spec.rb +++ b/spec/rubocop/cop/layout/argument_alignment_spec.rb @@ -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