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