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 layout false negatives around heredocs #9984

Merged
merged 3 commits into from Aug 7, 2021
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/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][])
koic marked this conversation as resolved.
Show resolved Hide resolved
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
10 changes: 5 additions & 5 deletions lib/rubocop/cop/layout/space_inside_parens.rb
Expand Up @@ -43,12 +43,12 @@ class SpaceInsideParens < Base
MSG_SPACE = 'No space inside parentheses detected.'

def on_new_investigation
@processed_source = processed_source
tokens = processed_source.sorted_tokens

if style == :space
process_with_space_style(processed_source)
process_with_space_style(tokens)
else
each_extraneous_space(processed_source.tokens) do |range|
each_extraneous_space(tokens) do |range|
add_offense(range) do |corrector|
corrector.remove(range)
end
Expand All @@ -58,8 +58,8 @@ def on_new_investigation

private

def process_with_space_style(processed_source)
processed_source.tokens.each_cons(2) do |token1, token2|
def process_with_space_style(tokens)
tokens.each_cons(2) do |token1, token2|
each_extraneous_space_in_empty_parens(token1, token2) do |range|
add_offense(range) do |corrector|
corrector.remove(range)
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/mixin/space_before_punctuation.rb
Expand Up @@ -10,7 +10,7 @@ module SpaceBeforePunctuation
MSG = 'Space found before %<token>s.'

def on_new_investigation
each_missing_space(processed_source.tokens) do |token, pos_before|
each_missing_space(processed_source.sorted_tokens) do |token, pos_before|
add_offense(pos_before, message: format(MSG, token: kind(token))) do |corrector|
PunctuationCorrector.remove_space(corrector, pos_before)
end
Expand Down
2 changes: 1 addition & 1 deletion rubocop.gemspec
Expand Up @@ -35,7 +35,7 @@ Gem::Specification.new do |s|
s.add_runtime_dependency('rainbow', '>= 2.2.2', '< 4.0')
s.add_runtime_dependency('regexp_parser', '>= 1.8', '< 3.0')
s.add_runtime_dependency('rexml')
s.add_runtime_dependency('rubocop-ast', '>= 1.8.0', '< 2.0')
s.add_runtime_dependency('rubocop-ast', '>= 1.9.0', '< 2.0')
s.add_runtime_dependency('ruby-progressbar', '~> 1.7')
s.add_runtime_dependency('unicode-display_width', '>= 1.4.0', '< 3.0')

Expand Down
17 changes: 17 additions & 0 deletions spec/rubocop/cop/layout/space_before_comma_spec.rb
Expand Up @@ -50,4 +50,21 @@
each { |s, t| a(1, formats[0, 1])}
RUBY
end

context 'heredocs' do
it 'registers an offense and corrects' do
expect_offense(<<~RUBY)
a(<<~STR , 2)
^ Space found before comma.
text
STR
RUBY

expect_correction(<<~RUBY)
a(<<~STR, 2)
text
STR
RUBY
end
end
end
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
17 changes: 17 additions & 0 deletions spec/rubocop/cop/layout/space_before_semicolon_spec.rb
Expand Up @@ -64,4 +64,21 @@
end
end
end

context 'heredocs' do
it 'registers an offense and corrects' do
expect_offense(<<~RUBY)
<<~STR ; x = 1
^ Space found before semicolon.
text
STR
RUBY

expect_correction(<<~RUBY)
<<~STR; x = 1
text
STR
RUBY
end
end
end
32 changes: 32 additions & 0 deletions spec/rubocop/cop/layout/space_inside_parens_spec.rb
Expand Up @@ -21,6 +21,22 @@
RUBY
end

it 'registers an offense for space around heredoc start' do
expect_offense(<<~'RUBY')
f( <<~HEREDOC )
^ Space inside parentheses detected.
^ Space inside parentheses detected.
This is my text
HEREDOC
RUBY

expect_correction(<<~RUBY)
f(<<~HEREDOC)
This is my text
HEREDOC
RUBY
end

it 'accepts parentheses in block parameter list' do
expect_no_offenses(<<~RUBY)
list.inject(Tms.new) { |sum, (label, item)|
Expand Down Expand Up @@ -93,6 +109,22 @@
RUBY
end

it 'registers an offense for no space around heredoc start' do
expect_offense(<<~RUBY)
f(<<~HEREDOC)
^ No space inside parentheses detected.
^ No space inside parentheses detected.
This is my text
HEREDOC
RUBY

expect_correction(<<~RUBY)
f( <<~HEREDOC )
This is my text
HEREDOC
RUBY
end

it 'accepts empty parentheses without spaces' do
expect_no_offenses('f()')
end
Expand Down