diff --git a/CHANGELOG.md b/CHANGELOG.md index 6ce615d6e01..103fc587a8c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ * [#8781](https://github.com/rubocop-hq/rubocop/issues/8781): Fix handling of comments in `Style/SafeNavigation` autocorrection. ([@dvandersluis][]) * [#8907](https://github.com/rubocop-hq/rubocop/pull/8907): Fix an incorrect auto-correct for `Layout/ClassStructure` when heredoc constant is defined after public method. ([@koic][]) * [#8889](https://github.com/rubocop-hq/rubocop/pull/8889): Cops can use new `after_` callbacks (only for nodes that may have children nodes, like `:send` and unlike `:sym`). ([@marcandre][]) +* [#8906](https://github.com/rubocop-hq/rubocop/pull/8906): Fix a false positive for `Layout/SpaceAroundOperators` when upward alignment. ([@koic][]) ### Changes diff --git a/docs/modules/ROOT/pages/cops_layout.adoc b/docs/modules/ROOT/pages/cops_layout.adoc index fd34e085b79..587b0ddb80f 100644 --- a/docs/modules/ROOT/pages/cops_layout.adoc +++ b/docs/modules/ROOT/pages/cops_layout.adoc @@ -5276,6 +5276,8 @@ RuboCop::Cop::Cop Checks that operators have space around them, except for ** which should or shouldn't have surrounding space depending on configuration. +It allows vertical alignment consisting of one or more whitespace +around operators. This cop has `AllowForAlignment` option. When `true`, allows most uses of extra spacing if the intent is to align with an operator on diff --git a/lib/rubocop/cop/layout/space_around_operators.rb b/lib/rubocop/cop/layout/space_around_operators.rb index 0273120ca62..1427df51ff5 100644 --- a/lib/rubocop/cop/layout/space_around_operators.rb +++ b/lib/rubocop/cop/layout/space_around_operators.rb @@ -5,6 +5,8 @@ module Cop module Layout # Checks that operators have space around them, except for ** which # should or shouldn't have surrounding space depending on configuration. + # It allows vertical alignment consisting of one or more whitespace + # around operators. # # This cop has `AllowForAlignment` option. When `true`, allows most # uses of extra spacing if the intent is to align with an operator on @@ -207,7 +209,8 @@ def excess_leading_space?(type, operator, with_space) token = Token.new(operator, nil, operator.source) align_preceding = aligned_with_preceding_assignment(token) - return align_preceding == :no unless align_preceding == :none + return false if align_preceding == :yes || + aligned_with_subsequent_assignment(token) == :none aligned_with_subsequent_assignment(token) != :yes end diff --git a/spec/rubocop/cop/layout/space_around_operators_spec.rb b/spec/rubocop/cop/layout/space_around_operators_spec.rb index a9e35c0f43c..2718609accc 100644 --- a/spec/rubocop/cop/layout/space_around_operators_spec.rb +++ b/spec/rubocop/cop/layout/space_around_operators_spec.rb @@ -129,6 +129,23 @@ def self.===(other); end expect_no_offenses('x = 0') end + it 'accepts an assignment with the same alignment margins' do + expect_no_offenses(<<~RUBY) + @integer_message = 12345 + @output = StringIO.new + @logger = Logger.new(@output) + RUBY + end + + it 'accepts an assignment with a blank line' do + expect_no_offenses(<<~RUBY) + expected = posts(:welcome) + + tagging = Tagging.all.merge!(includes: :taggable).find(taggings(:welcome_general).id) + assert_no_queries { assert_equal expected, tagging.taggable } + RUBY + end + it 'accepts an assignment by `for` statement' do expect_no_offenses(<<~RUBY) for a in [] do; end