From d1e49c8498930d79058726b11cd592a66c1aced2 Mon Sep 17 00:00:00 2001 From: Dave Corson-Knowles Date: Sun, 13 Oct 2024 03:07:16 -0700 Subject: [PATCH] Obsolete StringAsInstanceDoubleConstant in favor of VerifiedDoubleReference for constants only --- .rubocop.yml | 5 -- CHANGELOG.md | 2 + config/default.yml | 13 +--- config/obsoletion.yml | 3 + docs/modules/ROOT/pages/cops.adoc | 1 - docs/modules/ROOT/pages/cops_rspec.adoc | 73 ++----------------- .../string_as_instance_double_constant.rb | 45 ------------ .../cop/rspec/verified_double_reference.rb | 67 ++++------------- lib/rubocop/cop/rspec_cops.rb | 1 - ...string_as_instance_double_constant_spec.rb | 33 --------- .../rspec/verified_double_reference_spec.rb | 47 +----------- 11 files changed, 30 insertions(+), 260 deletions(-) delete mode 100644 lib/rubocop/cop/rspec/string_as_instance_double_constant.rb delete mode 100644 spec/rubocop/cop/rspec/string_as_instance_double_constant_spec.rb diff --git a/.rubocop.yml b/.rubocop.yml index 6fb427aef..6f7695352 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -250,8 +250,3 @@ Style/StringChars: {Enabled: true} Style/SuperWithArgsParentheses: {Enabled: true} Style/SwapValues: {Enabled: true} Style/YAMLFileRead: {Enabled: true} - -# Enable our own pending cops. -# -RSpec/StringAsInstanceDoubleConstant: - Enabled: true diff --git a/CHANGELOG.md b/CHANGELOG.md index 93499ceaf..5e4ce9307 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Master (Unreleased) +- Replace `RSpec/StringAsInstanceDoubleConstant` with `RSpecVerifiedDoubleReference` configured to only support constant class references. ([@corsonknowles]) + ## 3.1.0 (2024-10-01) - Add `RSpec/StringAsInstanceDoubleConstant` to check for and correct strings used as instance_doubles. ([@corsonknowles]) diff --git a/config/default.yml b/config/default.yml index c62b2f138..7b3d55b7e 100644 --- a/config/default.yml +++ b/config/default.yml @@ -929,13 +929,6 @@ RSpec/SpecFilePathSuffix: - "**/spec/**/*" Reference: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/SpecFilePathSuffix -RSpec/StringAsInstanceDoubleConstant: - Description: Do not use a string as `instance_double` constant. - Enabled: pending - Safe: false - VersionAdded: '3.1' - Reference: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/StringAsInstanceDoubleConstant - RSpec/StubbedMock: Description: Checks that message expectations do not have a configured response. Enabled: true @@ -994,12 +987,8 @@ RSpec/VerifiedDoubleReference: Description: Checks for consistent verified double reference style. Enabled: true SafeAutoCorrect: false - EnforcedStyle: constant - SupportedStyles: - - constant - - string VersionAdded: 2.10.0 - VersionChanged: '2.12' + VersionChanged: "<>" Reference: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/VerifiedDoubleReference RSpec/VerifiedDoubles: diff --git a/config/obsoletion.yml b/config/obsoletion.yml index 530bd0ed4..40c6628d4 100644 --- a/config/obsoletion.yml +++ b/config/obsoletion.yml @@ -28,3 +28,6 @@ extracted: RSpec/Rails/*: rubocop-rspec_rails RSpec/FactoryBot/*: rubocop-factory_bot RSpec/Capybara/*: rubocop-capybara + +renamed: + RSpec/StringAsInstanceDoubleConstant: RSpec/VerifiedDoubleReference diff --git a/docs/modules/ROOT/pages/cops.adoc b/docs/modules/ROOT/pages/cops.adoc index 116d2a714..16ddf0fd6 100644 --- a/docs/modules/ROOT/pages/cops.adoc +++ b/docs/modules/ROOT/pages/cops.adoc @@ -101,7 +101,6 @@ * xref:cops_rspec.adoc#rspecsortmetadata[RSpec/SortMetadata] * xref:cops_rspec.adoc#rspecspecfilepathformat[RSpec/SpecFilePathFormat] * xref:cops_rspec.adoc#rspecspecfilepathsuffix[RSpec/SpecFilePathSuffix] -* xref:cops_rspec.adoc#rspecstringasinstancedoubleconstant[RSpec/StringAsInstanceDoubleConstant] * xref:cops_rspec.adoc#rspecstubbedmock[RSpec/StubbedMock] * xref:cops_rspec.adoc#rspecsubjectdeclaration[RSpec/SubjectDeclaration] * xref:cops_rspec.adoc#rspecsubjectstub[RSpec/SubjectStub] diff --git a/docs/modules/ROOT/pages/cops_rspec.adoc b/docs/modules/ROOT/pages/cops_rspec.adoc index 2aff167af..2586cce28 100644 --- a/docs/modules/ROOT/pages/cops_rspec.adoc +++ b/docs/modules/ROOT/pages/cops_rspec.adoc @@ -5493,41 +5493,6 @@ spec/models/user.rb # shared_examples_for 'foo' * https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/SpecFilePathSuffix -== RSpec/StringAsInstanceDoubleConstant - -|=== -| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed - -| Pending -| No -| Always (Unsafe) -| 3.1 -| - -|=== - -Do not use a string as `instance_double` constant. - -=== Safety - -This cop is unsafe because the correction requires loading the class. -Loading before stubbing causes RSpec to only allow instance methods -to be stubbed. - -=== Examples - -[source,ruby] ----- -# bad -instance_double('User', name: 'John') - -# good -instance_double(User, name: 'John') ----- - -=== References - -* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/StringAsInstanceDoubleConstant - == RSpec/StubbedMock |=== @@ -5910,20 +5875,19 @@ let(:userFood_2) { 'fettuccine' } | Yes | Always (Unsafe) | 2.10.0 -| 2.12 +| <> |=== Checks for consistent verified double reference style. -Only investigates references that are one of the supported styles. +=== Safety -This cop can be configured in your configuration using the -`EnforcedStyle` option and supports `--auto-gen-config`. +This cop is unsafe because the correction requires loading the class. +Loading before stubbing causes RSpec to only allow instance methods +to be stubbed. === Examples -==== `EnforcedStyle: constant` (default) - [source,ruby] ---- # bad @@ -5937,22 +5901,7 @@ let(:foo) do end ---- -==== `EnforcedStyle: string` - -[source,ruby] ----- -# bad -let(:foo) do - instance_double(ClassName, method_name: 'returned_value') -end - -# good -let(:foo) do - instance_double('ClassName', method_name: 'returned_value') -end ----- - -==== Reference is not in the supported style list. No enforcement +==== Reference is any dynamic variable. No enforcement [source,ruby] ---- @@ -5962,16 +5911,6 @@ let(:foo) do end ---- -=== Configurable attributes - -|=== -| Name | Default value | Configurable values - -| EnforcedStyle -| `constant` -| `constant`, `string` -|=== - === References * https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/VerifiedDoubleReference diff --git a/lib/rubocop/cop/rspec/string_as_instance_double_constant.rb b/lib/rubocop/cop/rspec/string_as_instance_double_constant.rb deleted file mode 100644 index c7a346821..000000000 --- a/lib/rubocop/cop/rspec/string_as_instance_double_constant.rb +++ /dev/null @@ -1,45 +0,0 @@ -# frozen_string_literal: true - -module RuboCop - module Cop - module RSpec - # Do not use a string as `instance_double` constant. - # - # @safety - # This cop is unsafe because the correction requires loading the class. - # Loading before stubbing causes RSpec to only allow instance methods - # to be stubbed. - # - # @example - # # bad - # instance_double('User', name: 'John') - # - # # good - # instance_double(User, name: 'John') - # - class StringAsInstanceDoubleConstant < Base - extend AutoCorrector - - MSG = 'Do not use a string as `instance_double` constant.' - RESTRICT_ON_SEND = %i[instance_double].freeze - - # @!method stringified_instance_double_const?(node) - def_node_matcher :stringified_instance_double_const?, <<~PATTERN - (send nil? :instance_double $str ...) - PATTERN - - def on_send(node) - stringified_instance_double_const?(node) do |args_node| - add_offense(args_node) do |corrector| - autocorrect(corrector, args_node) - end - end - end - - def autocorrect(corrector, node) - corrector.replace(node, node.value) - end - end - end - end -end diff --git a/lib/rubocop/cop/rspec/verified_double_reference.rb b/lib/rubocop/cop/rspec/verified_double_reference.rb index b4f6e8d38..1a3621a26 100644 --- a/lib/rubocop/cop/rspec/verified_double_reference.rb +++ b/lib/rubocop/cop/rspec/verified_double_reference.rb @@ -5,14 +5,14 @@ module Cop module RSpec # Checks for consistent verified double reference style. # - # Only investigates references that are one of the supported styles. - # # @see https://rspec.info/features/3-12/rspec-mocks/verifying-doubles # - # This cop can be configured in your configuration using the - # `EnforcedStyle` option and supports `--auto-gen-config`. + # @safety + # This cop is unsafe because the correction requires loading the class. + # Loading before stubbing causes RSpec to only allow instance methods + # to be stubbed. # - # @example `EnforcedStyle: constant` (default) + # @example # # bad # let(:foo) do # instance_double('ClassName', method_name: 'returned_value') @@ -23,18 +23,7 @@ module RSpec # instance_double(ClassName, method_name: 'returned_value') # end # - # @example `EnforcedStyle: string` - # # bad - # let(:foo) do - # instance_double(ClassName, method_name: 'returned_value') - # end - # - # # good - # let(:foo) do - # instance_double('ClassName', method_name: 'returned_value') - # end - # - # @example Reference is not in the supported style list. No enforcement + # @example Reference is any dynamic variable. No enforcement # # # good # let(:foo) do @@ -42,9 +31,9 @@ module RSpec # end class VerifiedDoubleReference < Base extend AutoCorrector - include ConfigurableEnforcedStyle - MSG = 'Use a %