Skip to content

Commit

Permalink
Fix a false negative for Layout/EmptyComment
Browse files Browse the repository at this point in the history
This PR fixes a false negative for `Layout/EmptyComment`
when using an empty comment next to code after comment line.
  • Loading branch information
koic authored and bbatsov committed May 20, 2024
1 parent 08a7244 commit d42a182
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/fix_false_negative_for_layout_empty_comment.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12914](https://github.com/rubocop/rubocop/pull/12914): Fix a false negative for `Layout/EmptyComment` when using an empty comment next to code after comment line. ([@koic][])
4 changes: 3 additions & 1 deletion lib/rubocop/cop/layout/empty_comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ def autocorrect(corrector, node)
end

def concat_consecutive_comments(comments)
consecutive_comments = comments.chunk_while { |i, j| i.loc.line.succ == j.loc.line }
consecutive_comments = comments.chunk_while do |i, j|
i.loc.line.succ == j.loc.line && i.loc.column == j.loc.column
end

consecutive_comments.map do |chunk|
joined_text = chunk.map { |c| comment_text(c) }.join
Expand Down
17 changes: 17 additions & 0 deletions spec/rubocop/cop/layout/empty_comment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,23 @@ def foo
RUBY
end

it 'registers an offense and corrects when using an empty comment next to code after comment line' do
expect_offense(<<~RUBY)
# comment
def foo #
^ Source code comment is empty.
something
end
RUBY

expect_correction(<<~RUBY)
# comment
def foo
something
end
RUBY
end

it 'does not register an offense when using comment text' do
expect_no_offenses(<<~RUBY)
# Description of `Foo` class.
Expand Down

0 comments on commit d42a182

Please sign in to comment.