Skip to content

Commit

Permalink
Fix a false positive for Layout/SpaceAroundOperators
Browse files Browse the repository at this point in the history
This PR fixes a false positive for `Layout/SpaceAroundOperators`
when upward alignment. Two false positive examples are shown.

1. `output` and `logger` assignments are aligned with the same margin

```ruby
integer_message = 12345
output  = StringIO.new
logger  = Logger.new(output)
```

2. `expected` and `tagging` assignments are aligned with a blank line in between

```ruby
expected = posts(:welcome)

tagging  = Tagging.all.merge!(includes: :taggable).find(taggings(:welcome_general).id)
assert_no_queries { assert_equal expected, tagging.taggable }
```

cf: rails/rails#36943
  • Loading branch information
koic authored and bbatsov committed Oct 21, 2020
1 parent e0713ad commit 4673957
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 1 deletion.
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

0 comments on commit 4673957

Please sign in to comment.