Skip to content

Commit

Permalink
Update Layout/SpaceBeforeComment to handle heredocs.
Browse files Browse the repository at this point in the history
  • Loading branch information
dvandersluis committed Aug 7, 2021
1 parent 122829b commit d03ef0c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/fix_update_layoutspacebeforecomment_to.md
@@ -0,0 +1 @@
* [#9984](https://github.com/rubocop/rubocop/pull/9984): Fix false negatives involving heredocs for `Layout/SpaceBeforeComma`, `Layout/SpaceBeforeComment`, `Layout/SpaceBeforeSemicolon` and `Layout/SpaceInsideParens`. ([@dvandersluis][])
2 changes: 1 addition & 1 deletion lib/rubocop/cop/layout/space_before_comment.rb
Expand Up @@ -18,7 +18,7 @@ class SpaceBeforeComment < Base
MSG = 'Put a space before an end-of-line comment.'

def on_new_investigation
processed_source.tokens.each_cons(2) do |token1, token2|
processed_source.sorted_tokens.each_cons(2) do |token1, token2|
next unless token2.comment?
next unless token1.line == token2.line
next unless token1.pos.end == token2.pos.begin
Expand Down
15 changes: 15 additions & 0 deletions spec/rubocop/cop/layout/space_before_comment_spec.rb
Expand Up @@ -27,4 +27,19 @@
=end
RUBY
end

it 'registers an offense and corrects after a heredoc' do
expect_offense(<<~RUBY)
<<~STR# my string
^^^^^^^^^^^ Put a space before an end-of-line comment.
text
STR
RUBY

expect_correction(<<~RUBY)
<<~STR # my string
text
STR
RUBY
end
end

0 comments on commit d03ef0c

Please sign in to comment.