From 408e69be903f1114e44e2f1ea8ef70d8c02cf279 Mon Sep 17 00:00:00 2001 From: ydah <13041216+ydah@users.noreply.github.com> Date: Thu, 1 Jun 2023 21:30:16 +0900 Subject: [PATCH] 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`. --- CHANGELOG.md | 2 ++ .../cop/rspec/excessive_docstring_spacing.rb | 15 ++++++++--- .../rspec/excessive_docstring_spacing_spec.rb | 26 +++++++++++++++++++ 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5493f0cf3..4e8ceabe7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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]) diff --git a/lib/rubocop/cop/rspec/excessive_docstring_spacing.rb b/lib/rubocop/cop/rspec/excessive_docstring_spacing.rb index b4f48c370..4d4127679 100644 --- a/lib/rubocop/cop/rspec/excessive_docstring_spacing.rb +++ b/lib/rubocop/cop/rspec/excessive_docstring_spacing.rb @@ -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] diff --git a/spec/rubocop/cop/rspec/excessive_docstring_spacing_spec.rb b/spec/rubocop/cop/rspec/excessive_docstring_spacing_spec.rb index 58c0ecd8e..dc5ca5ef4 100644 --- a/spec/rubocop/cop/rspec/excessive_docstring_spacing_spec.rb +++ b/spec/rubocop/cop/rspec/excessive_docstring_spacing_spec.rb @@ -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 @@ -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