Skip to content

Commit

Permalink
Merge pull request #13782 from Earlopain/else-alignment-numblock
Browse files Browse the repository at this point in the history
Fix an error `Layout/ElseAlignment` when `else` is part of a numblock
  • Loading branch information
koic authored Feb 1, 2025
2 parents 1127fc0 + bf06024 commit 8a48e61
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/fix_else_alignment_numblock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#13782](https://github.com/rubocop/rubocop/pull/13782): Fix an error `Layout/ElseAlignment` when `else` is part of a numblock. ([@earlopain][])
2 changes: 1 addition & 1 deletion lib/rubocop/cop/layout/else_alignment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def base_range_of_rescue(node)
case parent.type
when :def, :defs then base_for_method_definition(parent)
when :kwbegin then parent.loc.begin
when :block
when :block, :numblock
assignment_node = assignment_node(parent)
if same_line?(parent, assignment_node)
assignment_node.source_range
Expand Down
15 changes: 15 additions & 0 deletions spec/rubocop/cop/layout/else_alignment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,21 @@ def my_func
RUBY
end

it 'accepts a correctly aligned else from numblock' do
expect_no_offenses(<<~RUBY)
array_like.each do
_1
puts 'do something error prone'
rescue SomeException
puts 'error handling'
rescue
puts 'error handling'
else
puts 'normal handling'
end
RUBY
end

it 'accepts a correctly aligned else with assignment' do
expect_no_offenses(<<~RUBY)
result = array_like.each do |n|
Expand Down

0 comments on commit 8a48e61

Please sign in to comment.