Skip to content

Commit 408e69b

Browse files
committed
Fix a false negative for RSpec/ExcessiveDocstringSpacing when finds 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`.
1 parent cd6a30e commit 408e69b

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Master (Unreleased)
44

5+
- Fix a false negative for `RSpec/ExcessiveDocstringSpacing` when finds description with em space. ([@ydah])
6+
57
## 2.22.0 (2023-05-06)
68

79
- 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])

lib/rubocop/cop/rspec/excessive_docstring_spacing.rb

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,21 @@ def on_send(node)
5252

5353
# @param text [String]
5454
def excessive_whitespace?(text)
55-
return true if text.start_with?(' ') || text.end_with?(' ')
56-
57-
text.match?(/[^\n ] +[^ ]/)
55+
text.match?(/
56+
# Leading space
57+
\A[[:blank:]]
58+
|
59+
# Trailing space
60+
[[:blank:]]\z
61+
|
62+
# Two or more consecutive spaces, except if they are leading spaces
63+
[^[[:space:]]][[:blank:]]{2,}[^[[:blank:]]]
64+
/x)
5865
end
5966

6067
# @param text [String]
6168
def strip_excessive_whitespace(text)
62-
text.strip.gsub(/ +/, ' ')
69+
text.strip.gsub(/[[:blank:]]{2,}/, ' ')
6370
end
6471

6572
# @param node [RuboCop::AST::Node]

spec/rubocop/cop/rspec/excessive_docstring_spacing_spec.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,19 @@
2626
RUBY
2727
end
2828

29+
it 'finds description with leading em space' do
30+
expect_offense(<<-RUBY)
31+
describe '  #mymethod' do
32+
^^^^^^^^^^^ Excessive whitespace.
33+
end
34+
RUBY
35+
36+
expect_correction(<<-RUBY)
37+
describe '#mymethod' do
38+
end
39+
RUBY
40+
end
41+
2942
it 'finds interpolated description with leading whitespace' do
3043
expect_offense(<<-'RUBY')
3144
describe " ##{:stuff}" do
@@ -52,6 +65,19 @@
5265
RUBY
5366
end
5467

68+
it 'finds description with trailing em space' do
69+
expect_offense(<<-RUBY)
70+
describe '#mymethod  ' do
71+
^^^^^^^^^^^ Excessive whitespace.
72+
end
73+
RUBY
74+
75+
expect_correction(<<-RUBY)
76+
describe '#mymethod' do
77+
end
78+
RUBY
79+
end
80+
5581
it 'finds interpolated description with trailing whitespace' do
5682
expect_offense(<<-'RUBY')
5783
describe "##{:stuff} " do

0 commit comments

Comments
 (0)