From a4db148891334e84ee6e14592d3559812643fb90 Mon Sep 17 00:00:00 2001 From: Brian Graham Date: Mon, 14 Aug 2023 11:45:40 -0400 Subject: [PATCH] Fix infinite loop in ExcessiveDocstringSpacing --- CHANGELOG.md | 3 + .../cop/rspec/excessive_docstring_spacing.rb | 4 +- .../rspec/excessive_docstring_spacing_spec.rb | 121 +++++++++++++++++- 3 files changed, 125 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 83a3f6e72..7b0bd46b4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Master (Unreleased) +- Fix an infinite loop error when `RSpec/ExcessiveDocstringSpacing` finds a description with non-ASCII leading/trailing whitespace. ([@bcgraham]) + ## 2.23.2 (2023-08-09) - Fix an incorrect autocorrect for `RSpec/ReceiveMessages` when method is only non-word character. ([@marocchino]) @@ -797,6 +799,7 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features. [@aried3r]: https://github.com/aried3r [@baberthal]: https://github.com/baberthal [@backus]: https://github.com/backus +[@bcgraham]: https://github.com/bcgraham [@biinari]: https://github.com/biinari [@bmorrall]: https://github.com/bmorrall [@bquorning]: https://github.com/bquorning diff --git a/lib/rubocop/cop/rspec/excessive_docstring_spacing.rb b/lib/rubocop/cop/rspec/excessive_docstring_spacing.rb index 4d4127679..ac52f2209 100644 --- a/lib/rubocop/cop/rspec/excessive_docstring_spacing.rb +++ b/lib/rubocop/cop/rspec/excessive_docstring_spacing.rb @@ -66,7 +66,9 @@ def excessive_whitespace?(text) # @param text [String] def strip_excessive_whitespace(text) - text.strip.gsub(/[[:blank:]]{2,}/, ' ') + text + .gsub(/[[:blank:]]{2,}/, ' ') + .gsub(/\A[[:blank:]]|[[:blank:]]\z/, '') 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 dc5ca5ef4..cec7e1c54 100644 --- a/spec/rubocop/cop/rspec/excessive_docstring_spacing_spec.rb +++ b/spec/rubocop/cop/rspec/excessive_docstring_spacing_spec.rb @@ -28,7 +28,7 @@ it 'finds description with leading em space' do expect_offense(<<-RUBY) - describe '  #mymethod' do + describe '\u3000\u3000#mymethod' do ^^^^^^^^^^^ Excessive whitespace. end RUBY @@ -39,6 +39,19 @@ RUBY end + it 'finds description with leading non-breaking space' do + expect_offense(<<-RUBY) + describe '\u00a0#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 @@ -67,7 +80,33 @@ it 'finds description with trailing em space' do expect_offense(<<-RUBY) - describe '#mymethod  ' do + describe '#mymethod\u3000\u3000' do + ^^^^^^^^^^^ Excessive whitespace. + end + RUBY + + expect_correction(<<-RUBY) + describe '#mymethod' do + end + RUBY + end + + it 'finds description with trailing non-breaking space' do + expect_offense(<<-RUBY) + describe '#mymethod\u00a0' do + ^^^^^^^^^^ Excessive whitespace. + end + RUBY + + expect_correction(<<-RUBY) + describe '#mymethod' do + end + RUBY + end + + it 'finds description with leading and trailing non-breaking space' do + expect_offense(<<-RUBY) + describe '\u00a0#mymethod\u00a0' do ^^^^^^^^^^^ Excessive whitespace. end RUBY @@ -205,6 +244,19 @@ RUBY end + it 'finds description with leading non-breaking space' do + expect_offense(<<-RUBY) + context '\u00a0#mymethod' do + ^^^^^^^^^^ Excessive whitespace. + end + RUBY + + expect_correction(<<-RUBY) + context '#mymethod' do + end + RUBY + end + it 'finds interpolated description with leading whitespace' do expect_offense(<<-'RUBY') context " when doing something #{:stuff}" do @@ -231,6 +283,32 @@ RUBY end + it 'finds description with trailing non-breaking space' do + expect_offense(<<-RUBY) + context '#mymethod\u00a0' do + ^^^^^^^^^^ Excessive whitespace. + end + RUBY + + expect_correction(<<-RUBY) + context '#mymethod' do + end + RUBY + end + + it 'finds description with leading and trailing non-breaking space' do + expect_offense(<<-RUBY) + context '\u00a0#mymethod\u00a0' do + ^^^^^^^^^^^ Excessive whitespace. + end + RUBY + + expect_correction(<<-RUBY) + context '#mymethod' do + end + RUBY + end + it 'finds interpolated description with trailing whitespace' do expect_offense(<<-'RUBY') context "when doing #{:stuff} " do @@ -372,6 +450,19 @@ RUBY end + it 'finds description with leading non-breaking space' do + expect_offense(<<-RUBY) + it '\u00a0does something' do + ^^^^^^^^^^^^^^^ Excessive whitespace. + end + RUBY + + expect_correction(<<-RUBY) + it 'does something' do + end + RUBY + end + it 'finds interpolated description with leading whitespace' do expect_offense(<<-'RUBY') it " does something #{:stuff}" do @@ -398,6 +489,32 @@ RUBY end + it 'finds description with trailing non-breaking space' do + expect_offense(<<-RUBY) + it 'does something\u00a0' do + ^^^^^^^^^^^^^^^ Excessive whitespace. + end + RUBY + + expect_correction(<<-RUBY) + it 'does something' do + end + RUBY + end + + it 'finds description with leading and trailing non-breaking space' do + expect_offense(<<-RUBY) + it '\u00a0does something\u00a0' do + ^^^^^^^^^^^^^^^^ Excessive whitespace. + end + RUBY + + expect_correction(<<-RUBY) + it 'does something' do + end + RUBY + end + it 'finds interpolated description with trailing whitespace' do expect_offense(<<-'RUBY') it "does something #{:stuff} " do