Skip to content

Commit

Permalink
[Fix rubocop#6433] 2.5 assigned block rescue alignment
Browse files Browse the repository at this point in the history
Fix Ruby 2.5 Layout/RescueEnsureAlignment error on assigned blocks.
  • Loading branch information
gmcgibbon committed Nov 29, 2018
1 parent aa2c86f commit 3df6685
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

### Bug fixes

* [#6433](https://github.com/rubocop-hq/rubocop/issues/6433): Fix Ruby 2.5 `Layout/RescueEnsureAlignment` error on assigned blocks. ([@gmcgibbon][])
* [#6405](https://github.com/rubocop-hq/rubocop/issues/6405): Fix a false positive for `Lint/UselessAssignment` when using a variable in a module name. ([@itsWill][])
* [#5934](https://github.com/rubocop-hq/rubocop/issues/5934): Handle the combination of `--auto-gen-config` and `--config FILE` correctly. ([@jonas054][])
* [#5970](https://github.com/rubocop-hq/rubocop/issues/5970): Make running `--auto-gen-config` in a subdirectory work. ([@jonas054][])
Expand Down Expand Up @@ -3666,3 +3667,4 @@
[@itsWill]: https://github.com/itsWill
[@xlts]: https://github.com/xlts
[@takaram]: https://github.com/takaram
[@gmcgibbon]: https://github.com/gmcgibbon
19 changes: 16 additions & 3 deletions lib/rubocop/cop/layout/rescue_ensure_alignment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ class RescueEnsureAlignment < Cop
'aligned with `%<beginning>s` at ' \
'%<begin_loc_line>d, %<begin_loc_column>d.'.freeze
ANCESTOR_TYPES = %i[kwbegin def defs class module].freeze
RUBY_2_5_ANCESTOR_TYPES = (ANCESTOR_TYPES + [:block]).freeze
RUBY_2_5_ANCESTOR_TYPES = (ANCESTOR_TYPES + %i[block]).freeze
ANCESTOR_TYPES_WITH_ACCESS_MODIFIERS = %i[def defs].freeze
ASSIGNMENT_TYPES = %i[lvasgn].freeze

def on_resbody(node)
check(node) unless modifier?(node)
Expand Down Expand Up @@ -115,10 +116,13 @@ def alignment_node(node)
ancestor_node = ancestor_node(node)
return nil if ancestor_node.nil?

assignment_node = assignment_node(ancestor_node)
return assignment_node unless assignment_node.nil?

access_modifier_node = access_modifier_node(ancestor_node)
return ancestor_node if access_modifier_node.nil?
return access_modifier_node unless access_modifier_node.nil?

access_modifier_node
ancestor_node
end

def ancestor_node(node)
Expand All @@ -132,6 +136,15 @@ def ancestor_node(node)
node.each_ancestor(*ancestor_types).first
end

def assignment_node(node)
assignment_node = node.ancestors.first
return nil unless
assignment_node &&
ASSIGNMENT_TYPES.include?(assignment_node.type)

assignment_node
end

def access_modifier_node(node)
return nil unless
ANCESTOR_TYPES_WITH_ACCESS_MODIFIERS.include?(node.type)
Expand Down
10 changes: 10 additions & 0 deletions spec/rubocop/cop/layout/rescue_ensure_alignment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,16 @@ def method2
RUBY
end

it 'accepts aligned rescue in assigned do-end block' do
expect_no_offenses(<<-RUBY.strip_indent)
result = [1, 2, 3].map do |el|
el.to_s
rescue StandardError => _exception
next
end
RUBY
end

it 'accepts aligned rescue in do-end block in a method' do
expect_no_offenses(<<-RUBY.strip_indent)
def foo
Expand Down

0 comments on commit 3df6685

Please sign in to comment.