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

Layout/RescueEnsureAlignment continuation of #6254 #6771 #6818

Closed
wants to merge 2 commits into from
Closed
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
12 changes: 9 additions & 3 deletions lib/rubocop/cop/layout/rescue_ensure_alignment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ def alignment_source(node, starting_loc)
node.loc.begin
when :def, :defs, :class, :module
node.loc.name
when :lvasgn
node.child_nodes.first.loc.begin
else
# It is a wrapper with access modifier.
node.child_nodes.first.loc.name
Expand All @@ -114,11 +116,15 @@ def alignment_source(node, starting_loc)
def alignment_node(node)
ancestor_node = ancestor_node(node)

return ancestor_node if ancestor_node.nil? ||
ancestor_node.kwbegin_type?
return if ancestor_node.nil?

assignment_node = assignment_node(ancestor_node)
return assignment_node if same_line?(ancestor_node, assignment_node)

if assignment_node && same_line?(ancestor_node, assignment_node)
return assignment_node
end

return ancestor_node if ancestor_node.kwbegin_type?

access_modifier_node = access_modifier_node(ancestor_node)
return access_modifier_node unless access_modifier_node.nil?
Expand Down
22 changes: 22 additions & 0 deletions spec/rubocop/cop/layout/rescue_ensure_alignment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,28 @@ def method2
RUBY
end

context 'rescue with assigned begin' do
it 'accepts variable-aligned rescue in or-assigned begin-end block' do
expect_no_offenses(<<-RUBY.strip_indent)
@bar ||= begin
expensive_method
rescue StandardError
fall_back
end
RUBY
end

it 'accepts variable-aligned rescue in assigned begin-end block' do
expect_no_offenses(<<-RUBY.strip_indent)
@bar = begin
expensive_method
rescue StandardError
fall_back
end
RUBY
end
end

context '>= Ruby 2.5', :ruby25 do
it 'accepts aligned rescue in do-end block' do
expect_no_offenses(<<-RUBY.strip_indent)
Expand Down