Skip to content

Commit

Permalink
[Fix #11641] Fix a false negative for Layout/ExtraSpacing when ther…
Browse files Browse the repository at this point in the history
…e are many comments with extra spaces
  • Loading branch information
nobuyo authored and bbatsov committed Mar 18, 2023
1 parent 8b7afcd commit 62575cc
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#11641](https://github.com/rubocop/rubocop/issues/11641): Fix a false negative for `Layout/ExtraSpacing` when there are many comments with extra spaces. ([@nobuyo][])
7 changes: 6 additions & 1 deletion lib/rubocop/cop/layout/extra_spacing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,19 @@ def on_new_investigation

private

def aligned_locations(locs)
def aligned_locations(locs) # rubocop:disable Metrics/AbcSize
return [] if locs.empty?

aligned = Set[locs.first.line, locs.last.line]
locs.each_cons(3) do |before, loc, after|
col = loc.column
aligned << loc.line if col == before.column || col == after.column
end

# if locs.size > 2 and the size of variable `aligned`
# has not increased from its initial value, there are not aligned lines.
return [] if locs.size > 2 && aligned.size == 2

aligned
end

Expand Down
40 changes: 40 additions & 0 deletions spec/rubocop/cop/layout/extra_spacing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -592,4 +592,44 @@ def method(arg = 1)
RUBY
end
end

context 'when multiple comments have extra spaces' do
it 'registers offenses for all comments' do
expect_offense(<<~RUBY)
class Foo
def require(p) # rubocop:disable Naming/MethodParameterName
^ Unnecessary spacing detected.
end
def load(p) # rubocop:disable Naming/MethodParameterName
^ Unnecessary spacing detected.
end
def join(*ps) # rubocop:disable Naming/MethodParameterName
^ Unnecessary spacing detected.
end
def exist?(*ps) # rubocop:disable Naming/MethodParameterName
^ Unnecessary spacing detected.
end
end
RUBY

expect_correction(<<~RUBY)
class Foo
def require(p) # rubocop:disable Naming/MethodParameterName
end
def load(p) # rubocop:disable Naming/MethodParameterName
end
def join(*ps) # rubocop:disable Naming/MethodParameterName
end
def exist?(*ps) # rubocop:disable Naming/MethodParameterName
end
end
RUBY
end
end
end

0 comments on commit 62575cc

Please sign in to comment.