Skip to content

Commit

Permalink
Obsolete StringAsInstanceDoubleConstant in favor of VerifiedDoubleRef…
Browse files Browse the repository at this point in the history
…erence for constants only
  • Loading branch information
corsonknowles committed Oct 13, 2024
1 parent a95f2f0 commit d1e49c8
Show file tree
Hide file tree
Showing 11 changed files with 30 additions and 260 deletions.
5 changes: 0 additions & 5 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
13 changes: 1 addition & 12 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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: "<<next>>"
Reference: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/VerifiedDoubleReference

RSpec/VerifiedDoubles:
Expand Down
3 changes: 3 additions & 0 deletions config/obsoletion.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@ extracted:
RSpec/Rails/*: rubocop-rspec_rails
RSpec/FactoryBot/*: rubocop-factory_bot
RSpec/Capybara/*: rubocop-capybara

renamed:
RSpec/StringAsInstanceDoubleConstant: RSpec/VerifiedDoubleReference
1 change: 0 additions & 1 deletion docs/modules/ROOT/pages/cops.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
73 changes: 6 additions & 67 deletions docs/modules/ROOT/pages/cops_rspec.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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
|===
Expand Down Expand Up @@ -5910,20 +5875,19 @@ let(:userFood_2) { 'fettuccine' }
| Yes
| Always (Unsafe)
| 2.10.0
| 2.12
| <<next>>
|===
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
Expand All @@ -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]
----
Expand All @@ -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
Expand Down
45 changes: 0 additions & 45 deletions lib/rubocop/cop/rspec/string_as_instance_double_constant.rb

This file was deleted.

67 changes: 14 additions & 53 deletions lib/rubocop/cop/rspec/verified_double_reference.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -23,28 +23,17 @@ 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
# instance_double(@klass, method_name: 'returned_value')
# end
class VerifiedDoubleReference < Base
extend AutoCorrector
include ConfigurableEnforcedStyle

MSG = 'Use a %<style>s class reference for verified doubles.'
MSG = 'Use a constant class reference for verified doubles. ' \
'String references are not verifying unless the class is loaded.'

RESTRICT_ON_SEND = Set[
:class_double,
Expand All @@ -57,53 +46,25 @@ class VerifiedDoubleReference < Base
:stub_model
].freeze

REFERENCE_TYPE_STYLES = {
str: :string,
const: :constant
}.freeze

# @!method verified_double(node)
def_node_matcher :verified_double, <<~PATTERN
(send
nil?
RESTRICT_ON_SEND
$_class_reference
$str
...)
PATTERN

def on_send(node)
verified_double(node) do |class_reference|
break correct_style_detected unless opposing_style?(class_reference)

message = format(MSG, style: style)
expression = class_reference.source_range

add_offense(expression, message: message) do |corrector|
offense = class_reference.source
corrector.replace(expression, correct_style(offense))

opposite_style_detected
verified_double(node) do |string_argument_node|
add_offense(string_argument_node) do |corrector|
autocorrect(corrector, string_argument_node)
end
end
end

private

def opposing_style?(class_reference)
class_reference_style = REFERENCE_TYPE_STYLES[class_reference.type]

# Only enforce supported styles
return false unless class_reference_style

class_reference_style != style
end

def correct_style(offense)
if style == :string
"'#{offense}'"
else
offense.gsub(/^['"]|['"]$/, '')
end
def autocorrect(corrector, node)
corrector.replace(node, node.value)
end
end
end
Expand Down
1 change: 0 additions & 1 deletion lib/rubocop/cop/rspec_cops.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@
require_relative 'rspec/sort_metadata'
require_relative 'rspec/spec_file_path_format'
require_relative 'rspec/spec_file_path_suffix'
require_relative 'rspec/string_as_instance_double_constant'
require_relative 'rspec/stubbed_mock'
require_relative 'rspec/subject_declaration'
require_relative 'rspec/subject_stub'
Expand Down
33 changes: 0 additions & 33 deletions spec/rubocop/cop/rspec/string_as_instance_double_constant_spec.rb

This file was deleted.

Loading

0 comments on commit d1e49c8

Please sign in to comment.