Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix an error for Layout/RescueEnsureAlignment #10017

Merged
merged 1 commit into from Aug 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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