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 6c09b7d72..a3bb9f216 100644 --- a/config/default.yml +++ b/config/default.yml @@ -930,13 +930,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 @@ -995,12 +988,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 5556dd086..ba3094c18 100644 --- a/docs/modules/ROOT/pages/cops_rspec.adoc +++ b/docs/modules/ROOT/pages/cops_rspec.adoc @@ -5904,45 +5904,6 @@ spec/models/user.rb # shared_examples_for 'foo' * https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/SpecFilePathSuffix -[#rspecstringasinstancedoubleconstant] -== 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-rspecstringasinstancedoubleconstant] -=== 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-rspecstringasinstancedoubleconstant] -=== Examples - -[source,ruby] ----- -# bad -instance_double('User', name: 'John') - -# good -instance_double(User, name: 'John') ----- - -[#references-rspecstringasinstancedoubleconstant] -=== References - -* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/StringAsInstanceDoubleConstant - [#rspecstubbedmock] == RSpec/StubbedMock @@ -6354,15 +6315,16 @@ 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-rspecverifieddoublereference] === Examples 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 %