Skip to content

Commit

Permalink
[Fix rubocop#12324] Fix an error for Layout/RescueEnsureAlignment
Browse files Browse the repository at this point in the history
Fixes rubocop#12324.

This PR fixes an error for `Layout/RescueEnsureAlignment`
when using `rescue` in `do`...`end` block assigned to object attribute.
  • Loading branch information
koic committed Oct 31, 2023
1 parent fe802d9 commit 4b46872
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
1 change: 1 addition & 0 deletions changelog/fix_error_for_layout_rescue_ensure_alignment.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12324](https://github.com/rubocop/rubocop/issues/12324): Fix an error for `Layout/RescueEnsureAlignment` when using `rescue` in `do`...`end` block assigned to object attribute. ([@koic][])
4 changes: 2 additions & 2 deletions lib/rubocop/cop/layout/rescue_ensure_alignment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ def alignment_source(node, starting_loc)
mlhs_node, = *node
mlhs_node.source_range
else
# It is a wrapper with access modifier.
node.child_nodes.first.loc.name
# It is a wrapper with receiver of object attribute or access modifier.
node.receiver&.source_range || node.child_nodes.first.loc.name
end

range_between(starting_loc.begin_pos, ending_loc.end_pos).source
Expand Down
17 changes: 17 additions & 0 deletions spec/rubocop/cop/layout/rescue_ensure_alignment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,23 @@ def foo
end
end

context 'rescue in do-end block assigned to object attribute' do
it 'registers an offense' do
expect_offense(<<~RUBY)
obj.attr = do_something do
rescue StandardError
^^^^^^ `rescue` at 2, 2 is not aligned with `obj` at 1, 0.
end
RUBY

expect_correction(<<~RUBY)
obj.attr = do_something do
rescue StandardError
end
RUBY
end
end

context 'rescue in do-end block on multi-assignment' do
it 'registers an offense' do
expect_offense(<<~RUBY)
Expand Down

0 comments on commit 4b46872

Please sign in to comment.