From aa125efe354c86bd92e1870301984c5b54774c5d Mon Sep 17 00:00:00 2001 From: ydah <13041216+ydah@users.noreply.github.com> Date: Tue, 28 Feb 2023 22:20:41 +0900 Subject: [PATCH] Fix an incorrect autocorrect for `RSpec/VerifiedDoubleReference` when namespaced class Fix: https://github.com/rubocop/rubocop-rspec/issues/1588 --- CHANGELOG.md | 1 + lib/rubocop/cop/rspec/verified_double_reference.rb | 4 ++-- .../cop/rspec/verified_double_reference_spec.rb | 12 ++++++++++++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a6136a8ea..85aea954d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ - Fix a false positive for `RSpec/PendingWithoutReason` when `skip` is passed a block inside example. ([@ydah], [@pirj]) - Rename `RSpec/PendingBlockInsideExample` cop to `RSpec/SkipBlockInsideExample`. ([@pirj]) - Deprecate `send_pattern`/`block_pattern`/`numblock_pattern` helpers in favour of using node pattern explicitly. ([@pirj], [@ydah]) +- Fix an incorrect autocorrect for `RSpec/VerifiedDoubleReference` when namespaced class. ([@ydah]) ## 2.18.1 (2023-01-19) diff --git a/lib/rubocop/cop/rspec/verified_double_reference.rb b/lib/rubocop/cop/rspec/verified_double_reference.rb index 2ab71c469..8c1694017 100644 --- a/lib/rubocop/cop/rspec/verified_double_reference.rb +++ b/lib/rubocop/cop/rspec/verified_double_reference.rb @@ -79,7 +79,7 @@ def on_send(node) expression = class_reference.source_range add_offense(expression, message: message) do |corrector| - violation = class_reference.children.last.to_s + violation = class_reference.source corrector.replace(expression, correct_style(violation)) opposite_style_detected @@ -102,7 +102,7 @@ def correct_style(violation) if style == :string "'#{violation}'" else - violation + violation.gsub(/^['"]|['"]$/, '') end end end diff --git a/spec/rubocop/cop/rspec/verified_double_reference_spec.rb b/spec/rubocop/cop/rspec/verified_double_reference_spec.rb index 1f7625c09..03d0f2c10 100644 --- a/spec/rubocop/cop/rspec/verified_double_reference_spec.rb +++ b/spec/rubocop/cop/rspec/verified_double_reference_spec.rb @@ -27,10 +27,16 @@ expect_offense(<<~RUBY, verified_double: verified_double) %{verified_double}('ClassName') _{verified_double} ^^^^^^^^^^^ Use a constant class reference for verified doubles. + %{verified_double}('Foo::Bar::Baz') + _{verified_double} ^^^^^^^^^^^^^^^ Use a constant class reference for verified doubles. + %{verified_double}('::Foo::Bar') + _{verified_double} ^^^^^^^^^^^^ Use a constant class reference for verified doubles. RUBY expect_correction(<<~RUBY) #{verified_double}(ClassName) + #{verified_double}(Foo::Bar::Baz) + #{verified_double}(::Foo::Bar) RUBY end @@ -52,10 +58,16 @@ expect_offense(<<~RUBY, verified_double: verified_double) %{verified_double}(ClassName) _{verified_double} ^^^^^^^^^ Use a string class reference for verified doubles. + %{verified_double}(Foo::Bar::Baz) + _{verified_double} ^^^^^^^^^^^^^ Use a string class reference for verified doubles. + %{verified_double}(::Foo::Bar) + _{verified_double} ^^^^^^^^^^ Use a string class reference for verified doubles. RUBY expect_correction(<<~RUBY) #{verified_double}('ClassName') + #{verified_double}('Foo::Bar::Baz') + #{verified_double}('::Foo::Bar') RUBY end