Skip to content

Commit

Permalink
[Fix rubocop#12307] Fix an infinite loop error for Layout/EndAlignment
Browse files Browse the repository at this point in the history
Fixes rubocop#12307.

This PR fixes an infinite loop error for `Layout/EndAlignment`
when `EnforcedStyleAlignWith: variable` and using a conditional statement in
a method argument on the same line and `end` with method call is not aligned.
  • Loading branch information
koic committed Oct 25, 2023
1 parent 21cad51 commit d05f4b1
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12307](https://github.com/rubocop/rubocop/issues/12307): Fix an infinite loop error for `Layout/EndAlignment` when `EnforcedStyleAlignWith: variable` and using a conditional statement in a method argument on the same line and `end` with method call is not aligned. ([@koic][])
8 changes: 7 additions & 1 deletion lib/rubocop/cop/layout/end_alignment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,13 @@ def alignment_node(node)
when :keyword
node
when :variable
alignment_node_for_variable_style(node)
align_to = alignment_node_for_variable_style(node)

while (parent = align_to.parent) && parent.send_type? && same_line?(align_to, parent)
align_to = parent
end

align_to
else
start_line_range(node)
end
Expand Down
15 changes: 15 additions & 0 deletions spec/rubocop/cop/layout/end_alignment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,21 @@ class << self
RUBY
end

it 'registers an offense when using a conditional statement in a method argument on the same line and `end` with method call is not aligned' do
expect_offense(<<~RUBY)
do_something case condition
when expr
end.method_call
^^^ `end` at 3, 13 is not aligned with `do_something case` at 1, 0.
RUBY

expect_correction(<<~RUBY)
do_something case condition
when expr
end.method_call
RUBY
end

it 'register an offense when using a pattern matching in a method argument and `end` is not aligned', :ruby27 do
expect_offense(<<~RUBY)
format(
Expand Down

0 comments on commit d05f4b1

Please sign in to comment.