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 41551a09d..4073488f5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ - Fix `RSpec/VoidExpect` to only operate inside an example block. ([@corsonknowles]) - Change `RSpec/ContextWording` cop to always report an offense when both `Prefixes` and `AllowedPatterns` are empty. ([@ydah]) - Add support for `and` and `or` compound matchers to `RSpec/ChangeByZero` cop. ([@ydah]) +- Replace `RSpec/StringAsInstanceDoubleConstant` with `RSpecVerifiedDoubleReference` configured to only support constant class references. ([@corsonknowles]) ## 3.1.0 (2024-10-01) 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..4be6e6067 100644 --- a/config/obsoletion.yml +++ b/config/obsoletion.yml @@ -6,6 +6,13 @@ # Cop parameters that have been changed # Can be treated as a warning instead of a failure with `severity: warning` +changed_enforced_styles: + - cops: RSpec/VerifiedDoubleReference + parameters: EnforcedStyle + value: string + reason: String references are not verifying unless the class is loaded. + severity: warning + changed_parameters: - cops: - RSpec/VariableName @@ -28,3 +35,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 20015bc0a..8a8a8b5fd 100644 --- a/docs/modules/ROOT/pages/cops_rspec.adoc +++ b/docs/modules/ROOT/pages/cops_rspec.adoc @@ -5907,45 +5907,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 @@ -6357,22 +6318,21 @@ 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-rspecverifieddoublereference] +=== 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 -[#_enforcedstyle_-constant_-_default_-rspecverifieddoublereference] -==== `EnforcedStyle: constant` (default) - [source,ruby] ---- # bad @@ -6386,24 +6346,8 @@ let(:foo) do end ---- -[#_enforcedstyle_-string_-rspecverifieddoublereference] -==== `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-rspecverifieddoublereference] -==== Reference is not in the supported style list. No enforcement +[#reference-is-any-dynamic-variable_-no-enforcement-rspecverifieddoublereference] +==== Reference is any dynamic variable. No enforcement [source,ruby] ---- @@ -6413,17 +6357,6 @@ let(:foo) do end ---- -[#configurable-attributes-rspecverifieddoublereference] -=== Configurable attributes - -|=== -| Name | Default value | Configurable values - -| EnforcedStyle -| `constant` -| `constant`, `string` -|=== - [#references-rspecverifieddoublereference] === References 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 %