Skip to content

Commit

Permalink
Merge pull request #12325 from koic/fix_error_for_layout_rescue_ensur…
Browse files Browse the repository at this point in the history
…e_alignment

[Fix #12324] Fix an error for `Layout/RescueEnsureAlignment`
  • Loading branch information
koic committed Oct 31, 2023
2 parents f981a1a + 4b46872 commit 3e39a76
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 3e39a76

Please sign in to comment.