Skip to content

Commit

Permalink
[Fix rubocop#5680] Support Layout/ElseAlignment in do/end blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
YukiJikumaru committed Mar 15, 2018
1 parent 841569b commit 96e63b7
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

### Bug fixes

* [#5680](https://github.com/bbatsov/rubocop/issues/5680): Fix Layout/ElseAlignment for rescue/else/ensure inside do/end blocks. ([@YukiJikumaru][])
* [#5642](https://github.com/bbatsov/rubocop/pull/5642): Fix Style/Documentation :nodoc: for compact-style nested modules/classes. ([@ojab][])
* [#5648](https://github.com/bbatsov/rubocop/issues/5648): Suggest valid memoized instance variable for predicate method. ([@satyap][])
* [#5623](https://github.com/bbatsov/rubocop/pull/5623): Fix `Bundler/OrderedGems` when a group includes duplicate gems. ([@colorbox][])
Expand Down Expand Up @@ -3253,3 +3254,4 @@
[@htwroclau]: https://github.com/htwroclau
[@hamada14]: https://github.com/hamada14
[@anthony-robin]: https://github.com/anthony-robin
[@YukiJikumaru]: https://github.com/YukiJikumaru
24 changes: 14 additions & 10 deletions lib/rubocop/cop/layout/else_alignment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def on_if(node, base = nil)
return if ignored_node?(node)
return unless node.else? && begins_its_line?(node.loc.else)

check_alignment(base_range(node, base), node.loc.else)
check_alignment(base_range_of_if(node, base), node.loc.else)

return unless node.elsif_conditional?

Expand All @@ -50,14 +50,7 @@ def on_if(node, base = nil)
def on_rescue(node)
return unless node.loc.respond_to?(:else) && node.loc.else

parent = node.parent
parent = parent.parent if parent.ensure_type?
base = case parent.type
when :def, :defs then base_for_method_definition(parent)
when :kwbegin then parent.loc.begin
else node.loc.keyword
end
check_alignment(base, node.loc.else)
check_alignment(base_range_of_rescue(node), node.loc.else)
end

def on_case(node)
Expand All @@ -77,7 +70,7 @@ def check_nested(node, base)
ignore_node(node)
end

def base_range(node, base)
def base_range_of_if(node, base)
if base
base.source_range
else
Expand All @@ -86,6 +79,17 @@ def base_range(node, base)
end
end

def base_range_of_rescue(node)
parent = node.parent
parent = parent.parent if parent.ensure_type?
case parent.type
when :def, :defs then base_for_method_definition(parent)
when :kwbegin then parent.loc.begin
when :block then parent.send_node.source_range
else node.loc.keyword
end
end

def base_for_method_definition(node)
parent = node.parent
if parent && parent.send_type?
Expand Down
31 changes: 31 additions & 0 deletions spec/rubocop/cop/layout/else_alignment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -535,4 +535,35 @@ def my_func
RUBY
end
end

context '>= Ruby 2.5 ensure/rescue/else in Block Argument', :ruby25 do
it 'accepts a correctly aligned else' do
expect_no_offenses(<<-RUBY.strip_indent)
array_like.each do |n|
puts 'do something error prone'
rescue SomeException
puts 'error handling'
rescue
puts 'error handling'
else
puts 'normal handling'
end
RUBY
end

it 'registers an offense for misaligned else' do
expect_offense(<<-RUBY.strip_indent)
array_like.each do |n|
puts 'do something error prone'
rescue SomeException
puts 'error handling'
rescue
puts 'error handling'
else
^^^^ Align `else` with `array_like.each`.
puts 'normal handling'
end
RUBY
end
end
end

0 comments on commit 96e63b7

Please sign in to comment.