Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a false positive for Layout/SpaceAroundOperators #8906

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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_<type>` 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

Expand Down
2 changes: 2 additions & 0 deletions docs/modules/ROOT/pages/cops_layout.adoc
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion lib/rubocop/cop/layout/space_around_operators.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions spec/rubocop/cop/layout/space_around_operators_spec.rb
Expand Up @@ -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
Expand Down