From c8ab8d951770a46c07470fb2d505347759703a3f Mon Sep 17 00:00:00 2001 From: Marco Costa Date: Wed, 16 Jan 2019 13:20:16 -0500 Subject: [PATCH 1/2] [Fix #6254] Fix RescueEnsureAlignment for non-local assignments --- CHANGELOG.md | 2 + .../cop/layout/rescue_ensure_alignment.rb | 12 +++- .../layout/rescue_ensure_alignment_spec.rb | 66 ++++++++++++++++++- 3 files changed, 78 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b0ed6e02041..31ee110286f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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][]) @@ -3758,3 +3759,4 @@ [@Intrepidd]: https://github.com/Intrepidd [@Ruffeng]: https://github.com/Ruffeng [@roooodcastro]: https://github.com/roooodcastro +[@marcotc]: https://github.com/marcotc diff --git a/lib/rubocop/cop/layout/rescue_ensure_alignment.rb b/lib/rubocop/cop/layout/rescue_ensure_alignment.rb index 1b981e3b931..cf34a26df17 100644 --- a/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +++ b/lib/rubocop/cop/layout/rescue_ensure_alignment.rb @@ -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) diff --git a/spec/rubocop/cop/layout/rescue_ensure_alignment_spec.rb b/spec/rubocop/cop/layout/rescue_ensure_alignment_spec.rb index 4c042370c41..e104c8b4788 100644 --- a/spec/rubocop/cop/layout/rescue_ensure_alignment_spec.rb +++ b/spec/rubocop/cop/layout/rescue_ensure_alignment_spec.rb @@ -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 @@ -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 = From f79c30fea8c74bf901705d467f277e8190d23e20 Mon Sep 17 00:00:00 2001 From: Marco Costa Date: Fri, 18 Jan 2019 13:31:11 -0500 Subject: [PATCH 2/2] Ask if node is assignment, instead of creating new list --- lib/rubocop/cop/layout/rescue_ensure_alignment.rb | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/lib/rubocop/cop/layout/rescue_ensure_alignment.rb b/lib/rubocop/cop/layout/rescue_ensure_alignment.rb index cf34a26df17..7984c43eac0 100644 --- a/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +++ b/lib/rubocop/cop/layout/rescue_ensure_alignment.rb @@ -30,17 +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 - ivasgn - cvasgn - gvasgn - casgn - masgn - op_asgn - and_asgn - or_asgn - ].freeze def on_resbody(node) check(node) unless modifier?(node) @@ -151,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