From 1ca14c974b3f9c93ac7f3279a773b7379226b97a Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Wed, 30 Jun 2021 04:07:56 +0900 Subject: [PATCH] Fix an incorrect auto-correct for `Layout/HashAlignment` Fixes https://github.com/testdouble/standard/issues/306. This PR fixes an incorrect auto-correct for `Layout/HashAlignment` when setting `EnforcedStyle: with_fixed_indentation` of `Layout/ArgumentAlignment` and using misaligned keyword arguments. --- ...fix_incorrect_autocorrect_for_layout_hash_alignment.md | 1 + lib/rubocop/cop/layout/hash_alignment.rb | 2 +- spec/rubocop/cli/autocorrect_spec.rb | 8 ++++++++ 3 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 changelog/fix_incorrect_autocorrect_for_layout_hash_alignment.md diff --git a/changelog/fix_incorrect_autocorrect_for_layout_hash_alignment.md b/changelog/fix_incorrect_autocorrect_for_layout_hash_alignment.md new file mode 100644 index 00000000000..590f35975ef --- /dev/null +++ b/changelog/fix_incorrect_autocorrect_for_layout_hash_alignment.md @@ -0,0 +1 @@ +* [#9897](https://github.com/rubocop/rubocop/pull/9897): Fix an incorrect auto-correct for `Layout/HashAlignment` when setting `EnforcedStyle: with_fixed_indentation` of `Layout/ArgumentAlignment` and using misaligned keyword arguments. ([@koic][]) diff --git a/lib/rubocop/cop/layout/hash_alignment.rb b/lib/rubocop/cop/layout/hash_alignment.rb index 33870a0b77f..9ff70a1d94e 100644 --- a/lib/rubocop/cop/layout/hash_alignment.rb +++ b/lib/rubocop/cop/layout/hash_alignment.rb @@ -220,7 +220,7 @@ def on_hash(node) def autocorrect_incompatible_with_other_cops?(node) enforce_first_argument_with_fixed_indentation? && node.pairs.any? && - node.parent&.call_type? && node.parent.loc.line == node.pairs.first.loc.line + node.parent&.call_type? && node.parent.loc.selector.line == node.pairs.first.loc.line end def reset! diff --git a/spec/rubocop/cli/autocorrect_spec.rb b/spec/rubocop/cli/autocorrect_spec.rb index b4416c266ac..fc35d83767c 100644 --- a/spec/rubocop/cli/autocorrect_spec.rb +++ b/spec/rubocop/cli/autocorrect_spec.rb @@ -1849,6 +1849,10 @@ def do_even_more_stuff self&.update(foo: bar, baz: boo, pony: party) + + foo + .do_something(foo: bar, + baz: qux) RUBY create_file('.rubocop.yml', <<~YAML) @@ -1868,6 +1872,10 @@ def do_even_more_stuff self&.update(foo: bar, baz: boo, pony: party) + + foo + .do_something(foo: bar, + baz: qux) RUBY end