Skip to content

Commit

Permalink
[Fix rubocop#9953] Fix a false auto-correction behavior for `Layout/E…
Browse files Browse the repository at this point in the history
…ndAlignment`

Fixes rubocop#9953.

Fix an infinite loop error and a false auto-correction behavior for `Layout/EndAlignment`
when using a conditional statement in a method argument.
  • Loading branch information
koic committed Jul 30, 2021
1 parent 39fcf1c commit 2b8b517
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
@@ -0,0 +1 @@
* [#9953](https://github.com/rubocop/rubocop/issues/9953): Fix an infinite loop error and a false auto-correction behavior for `Layout/EndAlignment` when using a conditional statement in a method argument. ([@koic][])
3 changes: 2 additions & 1 deletion lib/rubocop/cop/layout/end_alignment.rb
Expand Up @@ -165,7 +165,8 @@ def alignment_node(node)
end

def alignment_node_for_variable_style(node)
return node.parent if node.case_type? && node.argument?
return node.parent if node.case_type? && node.argument? &&
node.loc.line == node.parent.loc.line

assignment = assignment_or_operator_method(node)

Expand Down
25 changes: 25 additions & 0 deletions spec/rubocop/cop/layout/end_alignment_spec.rb
Expand Up @@ -339,6 +339,31 @@ module Test
end
RUBY
end

it 'register an offense when using a conditional statement in a method argument and `end` is not aligned' do
expect_offense(<<~RUBY)
format(
case condition
when foo
bar
else
baz
end, qux
^^^ `end` at 7, 0 is not aligned with `case` at 2, 2.
)
RUBY

expect_correction(<<~RUBY)
format(
case condition
when foo
bar
else
baz
end, qux
)
RUBY
end
end

context 'correct + opposite' do
Expand Down

0 comments on commit 2b8b517

Please sign in to comment.