Skip to content

Commit

Permalink
Fix a false negative for RSpec/ExcessiveDocstringSpacing when finds…
Browse files Browse the repository at this point in the history
… description with em space

This PR is fix a false negative for `RSpec/ExcessiveDocstringSpacing` when finds description with em space.

The motivation for this PR is to be able to detect and autocorrect the em space
that are sometimes left behind because they cannot be detected by
the current `RSpec/ExcessiveDocstringSpacing`.
  • Loading branch information
ydah committed Jun 1, 2023
1 parent cd6a30e commit 408e69b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Master (Unreleased)

- Fix a false negative for `RSpec/ExcessiveDocstringSpacing` when finds description with em space. ([@ydah])

## 2.22.0 (2023-05-06)

- Extract factory_bot cops to a separate repository, [`rubocop-factory_bot`](https://github.com/rubocop/rubocop-factory_bot). The `rubocop-factory_bot` repository is a dependency of `rubocop-rspec` and the factory_bot cops are aliased (`RSpec/FactoryBot/Foo` == `FactoryBot/Foo`) until v3.0 is released, so the change will be invisible to users until then. ([@ydah])
Expand Down
15 changes: 11 additions & 4 deletions lib/rubocop/cop/rspec/excessive_docstring_spacing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,21 @@ def on_send(node)

# @param text [String]
def excessive_whitespace?(text)
return true if text.start_with?(' ') || text.end_with?(' ')

text.match?(/[^\n ] +[^ ]/)
text.match?(/
# Leading space
\A[[:blank:]]
|
# Trailing space
[[:blank:]]\z
|
# Two or more consecutive spaces, except if they are leading spaces
[^[[:space:]]][[:blank:]]{2,}[^[[:blank:]]]
/x)
end

# @param text [String]
def strip_excessive_whitespace(text)
text.strip.gsub(/ +/, ' ')
text.strip.gsub(/[[:blank:]]{2,}/, ' ')
end

# @param node [RuboCop::AST::Node]
Expand Down
26 changes: 26 additions & 0 deletions spec/rubocop/cop/rspec/excessive_docstring_spacing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,19 @@
RUBY
end

it 'finds description with leading em space' do
expect_offense(<<-RUBY)
describe '  #mymethod' do
^^^^^^^^^^^ Excessive whitespace.
end
RUBY

expect_correction(<<-RUBY)
describe '#mymethod' do
end
RUBY
end

it 'finds interpolated description with leading whitespace' do
expect_offense(<<-'RUBY')
describe " ##{:stuff}" do
Expand All @@ -52,6 +65,19 @@
RUBY
end

it 'finds description with trailing em space' do
expect_offense(<<-RUBY)
describe '#mymethod  ' do
^^^^^^^^^^^ Excessive whitespace.
end
RUBY

expect_correction(<<-RUBY)
describe '#mymethod' do
end
RUBY
end

it 'finds interpolated description with trailing whitespace' do
expect_offense(<<-'RUBY')
describe "##{:stuff} " do
Expand Down

0 comments on commit 408e69b

Please sign in to comment.