Skip to content

Commit

Permalink
[Fix #6254] Fix RescueEnsureAlignment for non-local assignments
Browse files Browse the repository at this point in the history
  • Loading branch information
marcotc committed Jan 16, 2019
1 parent 457cd4f commit c8ab8d9
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -14,6 +14,7 @@

### Bug fixes

* [#6254](https://github.com/rubocop-hq/rubocop/issues/6254): Fix `Layout/RescueEnsureAlignment` for non-local assignments. ([@marcotc][])
* [#6627](https://github.com/rubocop-hq/rubocop/pull/6627): Fix handling of hashes in trailing comma. ([@abrom][])
* [#6623](https://github.com/rubocop-hq/rubocop/pull/6623): Fix heredoc detection in trailing comma. ([@palkan][])
* [#6100](https://github.com/rubocop-hq/rubocop/issues/6100): Fix a false positive in `Naming/ConstantName` cop when rhs is a conditional expression. ([@tatsuyafw][])
Expand Down Expand Up @@ -3758,3 +3759,4 @@
[@Intrepidd]: https://github.com/Intrepidd
[@Ruffeng]: https://github.com/Ruffeng
[@roooodcastro]: https://github.com/roooodcastro
[@marcotc]: https://github.com/marcotc
12 changes: 11 additions & 1 deletion lib/rubocop/cop/layout/rescue_ensure_alignment.rb
Expand Up @@ -30,7 +30,17 @@ class RescueEnsureAlignment < Cop
ANCESTOR_TYPES = %i[kwbegin def defs class module].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
ASSIGNMENT_TYPES = %i[
lvasgn
ivasgn
cvasgn
gvasgn
casgn
masgn
op_asgn
and_asgn
or_asgn
].freeze

def on_resbody(node)
check(node) unless modifier?(node)
Expand Down
66 changes: 65 additions & 1 deletion spec/rubocop/cop/layout/rescue_ensure_alignment_spec.rb
Expand Up @@ -375,7 +375,7 @@ def method2
RUBY
end

it 'accepts aligned rescue in assigned do-end block' do
it 'accepts aligned rescue do-end block assigned to local variable' do
expect_no_offenses(<<-RUBY.strip_indent)
result = [1, 2, 3].map do |el|
el.to_s
Expand All @@ -385,6 +385,70 @@ def method2
RUBY
end

it 'accepts aligned rescue in do-end block assigned to instance variable' do
expect_no_offenses(<<-RUBY.strip_indent)
@instance = [].map do |_|
rescue StandardError => _
end
RUBY
end

it 'accepts aligned rescue in do-end block assigned to class variable' do
expect_no_offenses(<<-RUBY.strip_indent)
@@class = [].map do |_|
rescue StandardError => _
end
RUBY
end

it 'accepts aligned rescue in do-end block assigned to global variable' do
expect_no_offenses(<<-RUBY.strip_indent)
$global = [].map do |_|
rescue StandardError => _
end
RUBY
end

it 'accepts aligned rescue in do-end block assigned to class' do
expect_no_offenses(<<-RUBY.strip_indent)
CLASS = [].map do |_|
rescue StandardError => _
end
RUBY
end

it 'accepts aligned rescue in do-end block on multi-assignment' do
expect_no_offenses(<<-RUBY.strip_indent)
a, b = [].map do |_|
rescue StandardError => _
end
RUBY
end

it 'accepts aligned rescue in do-end block on operation assignment' do
expect_no_offenses(<<-RUBY.strip_indent)
a += [].map do |_|
rescue StandardError => _
end
RUBY
end

it 'accepts aligned rescue in do-end block on and-assignment' do
expect_no_offenses(<<-RUBY.strip_indent)
a &&= [].map do |_|
rescue StandardError => _
end
RUBY
end

it 'accepts aligned rescue in do-end block on or-assignment' do
expect_no_offenses(<<-RUBY.strip_indent)
a ||= [].map do |_|
rescue StandardError => _
end
RUBY
end

it 'accepts aligned rescue in assigned do-end block starting on newline' do
expect_no_offenses(<<-RUBY.strip_indent)
valid =
Expand Down

0 comments on commit c8ab8d9

Please sign in to comment.