Skip to content

Commit

Permalink
[Fix #10364] Fix an infinite loop error for Style/HashAlignment
Browse files Browse the repository at this point in the history
Fixes #10364.

This PR fixes an infinite loop error for `Layout/HashAlignment` when
`EnforcedStyle: with_fixed_indentation` is specified for `Layout/ArgumentAlignment`.
The issue context is that the first keyword argument and the argument before it
are on the same line.
  • Loading branch information
koic authored and bbatsov committed Feb 3, 2022
1 parent 7e09781 commit 38b8c78
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/fix_an_error_for_style_hash_alignment.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#10364](https://github.com/rubocop/rubocop/issues/10364): Fix an infinite loop error for `Layout/HashAlignment` when `EnforcedStyle: with_fixed_indentation` is specified for `Layout/ArgumentAlignment`. ([@koic][])
7 changes: 6 additions & 1 deletion lib/rubocop/cop/layout/hash_alignment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,16 @@ def autocorrect_incompatible_with_other_cops?(node)
node.pairs.any? &&
node.parent&.call_type?

left_sibling = argument_before_hash(node)
parent_loc = node.parent.loc
selector = parent_loc.selector || parent_loc.expression
selector = left_sibling || parent_loc.selector || parent_loc.expression
same_line?(selector, node.pairs.first)
end

def argument_before_hash(hash_node)
hash_node.left_sibling.respond_to?(:loc) ? hash_node.left_sibling : nil
end

def reset!
self.offenses_by = {}
self.column_deltas = Hash.new { |hash, key| hash[key] = {} }
Expand Down
10 changes: 10 additions & 0 deletions spec/rubocop/cli/autocorrect_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1919,6 +1919,11 @@ def do_even_more_stuff
do_something.(foo: bar, baz: qux,
quux: corge)
do_something(
arg, foo: bar,
baz: qux
)
RUBY

create_file('.rubocop.yml', <<~YAML)
Expand All @@ -1945,6 +1950,11 @@ def do_even_more_stuff
do_something.(foo: bar, baz: qux,
quux: corge)
do_something(
arg, foo: bar,
baz: qux
)
RUBY
end

Expand Down

0 comments on commit 38b8c78

Please sign in to comment.