Skip to content

Commit

Permalink
Fix an error for Layout/RescueEnsureAlignment
Browse files Browse the repository at this point in the history
Follow up to #9974 (comment).

This PR fixes an error for `Layout/RescueEnsureAlignment`
when using zsuper with block.
  • Loading branch information
koic authored and bbatsov committed Aug 17, 2021
1 parent 4a52615 commit 016f38e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
1 change: 1 addition & 0 deletions changelog/fix_error_for_rescue_ensure_alignment.md
@@ -0,0 +1 @@
* [#10017](https://github.com/rubocop/rubocop/pull/10017): Fixan error for `Layout/RescueEnsureAlignment` when using zsuper with block. ([@koic][])
9 changes: 5 additions & 4 deletions lib/rubocop/cop/layout/rescue_ensure_alignment.rb
Expand Up @@ -137,17 +137,18 @@ def aligned_with_line_break_method?(ancestor_node, node)
send_node_loc = ancestor_node.send_node.loc
do_keyword_line = ancestor_node.loc.begin.line
rescue_keyword_column = node.loc.keyword.column
selector = send_node_loc.selector
selector = send_node_loc.respond_to?(:selector) ? send_node_loc.selector : send_node_loc

if send_node_loc.respond_to?(:dot) && (dot = send_node_loc.dot) &&
aligned_with_leading_dot?(do_keyword_line, dot, rescue_keyword_column)
if aligned_with_leading_dot?(do_keyword_line, send_node_loc, rescue_keyword_column)
return true
end

do_keyword_line == selector.line && rescue_keyword_column == selector.column
end

def aligned_with_leading_dot?(do_keyword_line, dot, rescue_keyword_column)
def aligned_with_leading_dot?(do_keyword_line, send_node_loc, rescue_keyword_column)
return false unless send_node_loc.respond_to?(:dot) && (dot = send_node_loc.dot)

do_keyword_line == dot.line && rescue_keyword_column == dot.column
end

Expand Down
31 changes: 31 additions & 0 deletions spec/rubocop/cop/layout/rescue_ensure_alignment_spec.rb
Expand Up @@ -769,6 +769,37 @@ def foo
end
end

context 'when using zsuper with block' do
it 'registers and corrects an offense and corrects when incorrect alignment' do
expect_offense(<<~RUBY)
super do
nil
ensure
^^^^^^ `ensure` at 3, 4 is not aligned with `super do` at 1, 0.
nil
end
RUBY

expect_correction(<<~RUBY)
super do
nil
ensure
nil
end
RUBY
end

it 'does not register an offense when correct alignment' do
expect_no_offenses(<<~RUBY)
super do
nil
ensure
nil
end
RUBY
end
end

describe 'excluded file', :config do
let(:config) do
RuboCop::Config.new('Layout/RescueEnsureAlignment' =>
Expand Down

0 comments on commit 016f38e

Please sign in to comment.