Skip to content

Commit

Permalink
Merge pull request #6671 from marcotc/fix-raise
Browse files Browse the repository at this point in the history
[Fix #6254] Fix RescueEnsureAlignment for non-local assignments
  • Loading branch information
Drenmi committed Jan 22, 2019
2 parents 35251b4 + e947a03 commit 826ae83
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,7 @@

### Bug fixes

* [#6254](https://github.com/rubocop-hq/rubocop/issues/6254): Fix `Layout/RescueEnsureAlignment` for non-local assignments. ([@marcotc][])
* [#6678](https://github.com/rubocop-hq/rubocop/issues/6678): Fix `Lint/DisjunctiveAssignmentInConstructor` when it finds an empty constructor. ([@rmm5t][])
* Do not attempt to auto-correct mass assignment or optional assignment in `Rails/RelativeDateConstant`. ([@rrosenblum][])
* Fix auto-correction of `Style/WordArray` and `Style/SymbolArray` when all elements are on separate lines and there is a trailing comment after the closing bracket. ([@rrosenblum][])
Expand Down Expand Up @@ -3769,3 +3770,4 @@
[@Ruffeng]: https://github.com/Ruffeng
[@roooodcastro]: https://github.com/roooodcastro
[@rmm5t]: https://github.com/rmm5t
[@marcotc]: https://github.com/marcotc
4 changes: 1 addition & 3 deletions lib/rubocop/cop/layout/rescue_ensure_alignment.rb
Expand Up @@ -30,7 +30,6 @@ 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

def on_resbody(node)
check(node) unless modifier?(node)
Expand Down Expand Up @@ -141,8 +140,7 @@ def ancestor_node(node)
def assignment_node(node)
assignment_node = node.ancestors.first
return nil unless
assignment_node &&
ASSIGNMENT_TYPES.include?(assignment_node.type)
assignment_node && assignment_node.assignment?

assignment_node
end
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 826ae83

Please sign in to comment.