Skip to content

Commit 1aa987f

Browse files
committedJan 10, 2025
Obsolete StringAsInstanceDoubleConstant in favor of VerifiedDoubleReference for constants only
1 parent e44a27b commit 1aa987f

11 files changed

+33
-268
lines changed
 

‎.rubocop.yml

-5
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,3 @@ Style/StringChars: {Enabled: true}
250250
Style/SuperWithArgsParentheses: {Enabled: true}
251251
Style/SwapValues: {Enabled: true}
252252
Style/YAMLFileRead: {Enabled: true}
253-
254-
# Enable our own pending cops.
255-
#
256-
RSpec/StringAsInstanceDoubleConstant:
257-
Enabled: true

‎CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
- Fix `RSpec/VoidExpect` to only operate inside an example block. ([@corsonknowles])
1515
- Change `RSpec/ContextWording` cop to always report an offense when both `Prefixes` and `AllowedPatterns` are empty. ([@ydah])
1616
- Add support for `and` and `or` compound matchers to `RSpec/ChangeByZero` cop. ([@ydah])
17+
- Replace `RSpec/StringAsInstanceDoubleConstant` with `RSpecVerifiedDoubleReference` configured to only support constant class references. ([@corsonknowles])
1718

1819
## 3.1.0 (2024-10-01)
1920

‎config/default.yml

+1-12
Original file line numberDiff line numberDiff line change
@@ -930,13 +930,6 @@ RSpec/SpecFilePathSuffix:
930930
- "**/spec/**/*"
931931
Reference: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/SpecFilePathSuffix
932932

933-
RSpec/StringAsInstanceDoubleConstant:
934-
Description: Do not use a string as `instance_double` constant.
935-
Enabled: pending
936-
Safe: false
937-
VersionAdded: '3.1'
938-
Reference: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/StringAsInstanceDoubleConstant
939-
940933
RSpec/StubbedMock:
941934
Description: Checks that message expectations do not have a configured response.
942935
Enabled: true
@@ -995,12 +988,8 @@ RSpec/VerifiedDoubleReference:
995988
Description: Checks for consistent verified double reference style.
996989
Enabled: true
997990
SafeAutoCorrect: false
998-
EnforcedStyle: constant
999-
SupportedStyles:
1000-
- constant
1001-
- string
1002991
VersionAdded: 2.10.0
1003-
VersionChanged: '2.12'
992+
VersionChanged: "<<next>>"
1004993
Reference: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/VerifiedDoubleReference
1005994

1006995
RSpec/VerifiedDoubles:

‎config/obsoletion.yml

+5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ changed_parameters:
1212
parameters: IgnoredPatterns
1313
alternative: AllowedPatterns
1414
severity: warning
15+
- cops: RSpec/VerifiedDoubleReference
16+
parameters: EnforcedStyle
17+
reason: String references are not verifying unless the class is loaded.
1518

1619
split:
1720
RSpec/FilePath:
@@ -23,6 +26,8 @@ removed:
2326
RSpec/Capybara/FeatureMethods:
2427
reason: >
2528
this cop has migrated to `RSpec/Dialect`. Please use `RSpec/Dialect` instead
29+
RSpec/StringAsInstanceDoubleConstant:
30+
reason: Please use `RSpec/VerifiedDoubleReference` instead
2631

2732
extracted:
2833
RSpec/Rails/*: rubocop-rspec_rails

‎docs/modules/ROOT/pages/cops.adoc

-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@
101101
* xref:cops_rspec.adoc#rspecsortmetadata[RSpec/SortMetadata]
102102
* xref:cops_rspec.adoc#rspecspecfilepathformat[RSpec/SpecFilePathFormat]
103103
* xref:cops_rspec.adoc#rspecspecfilepathsuffix[RSpec/SpecFilePathSuffix]
104-
* xref:cops_rspec.adoc#rspecstringasinstancedoubleconstant[RSpec/StringAsInstanceDoubleConstant]
105104
* xref:cops_rspec.adoc#rspecstubbedmock[RSpec/StubbedMock]
106105
* xref:cops_rspec.adoc#rspecsubjectdeclaration[RSpec/SubjectDeclaration]
107106
* xref:cops_rspec.adoc#rspecsubjectstub[RSpec/SubjectStub]

‎docs/modules/ROOT/pages/cops_rspec.adoc

+8-75
Original file line numberDiff line numberDiff line change
@@ -5909,45 +5909,6 @@ spec/models/user.rb # shared_examples_for 'foo'
59095909
59105910
* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/SpecFilePathSuffix
59115911
5912-
[#rspecstringasinstancedoubleconstant]
5913-
== RSpec/StringAsInstanceDoubleConstant
5914-
5915-
|===
5916-
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
5917-
5918-
| Pending
5919-
| No
5920-
| Always (Unsafe)
5921-
| 3.1
5922-
| -
5923-
|===
5924-
5925-
Do not use a string as `instance_double` constant.
5926-
5927-
[#safety-rspecstringasinstancedoubleconstant]
5928-
=== Safety
5929-
5930-
This cop is unsafe because the correction requires loading the class.
5931-
Loading before stubbing causes RSpec to only allow instance methods
5932-
to be stubbed.
5933-
5934-
[#examples-rspecstringasinstancedoubleconstant]
5935-
=== Examples
5936-
5937-
[source,ruby]
5938-
----
5939-
# bad
5940-
instance_double('User', name: 'John')
5941-
5942-
# good
5943-
instance_double(User, name: 'John')
5944-
----
5945-
5946-
[#references-rspecstringasinstancedoubleconstant]
5947-
=== References
5948-
5949-
* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/StringAsInstanceDoubleConstant
5950-
59515912
[#rspecstubbedmock]
59525913
== RSpec/StubbedMock
59535914
@@ -6359,22 +6320,21 @@ let(:userFood_2) { 'fettuccine' }
63596320
| Yes
63606321
| Always (Unsafe)
63616322
| 2.10.0
6362-
| 2.12
6323+
| <<next>>
63636324
|===
63646325
63656326
Checks for consistent verified double reference style.
63666327
6367-
Only investigates references that are one of the supported styles.
6328+
[#safety-rspecverifieddoublereference]
6329+
=== Safety
63686330
6369-
This cop can be configured in your configuration using the
6370-
`EnforcedStyle` option and supports `--auto-gen-config`.
6331+
This cop is unsafe because the correction requires loading the class.
6332+
Loading before stubbing causes RSpec to only allow instance methods
6333+
to be stubbed.
63716334
63726335
[#examples-rspecverifieddoublereference]
63736336
=== Examples
63746337
6375-
[#_enforcedstyle_-constant_-_default_-rspecverifieddoublereference]
6376-
==== `EnforcedStyle: constant` (default)
6377-
63786338
[source,ruby]
63796339
----
63806340
# bad
@@ -6388,24 +6348,8 @@ let(:foo) do
63886348
end
63896349
----
63906350
6391-
[#_enforcedstyle_-string_-rspecverifieddoublereference]
6392-
==== `EnforcedStyle: string`
6393-
6394-
[source,ruby]
6395-
----
6396-
# bad
6397-
let(:foo) do
6398-
instance_double(ClassName, method_name: 'returned_value')
6399-
end
6400-
6401-
# good
6402-
let(:foo) do
6403-
instance_double('ClassName', method_name: 'returned_value')
6404-
end
6405-
----
6406-
6407-
[#reference-is-not-in-the-supported-style-list_-no-enforcement-rspecverifieddoublereference]
6408-
==== Reference is not in the supported style list. No enforcement
6351+
[#reference-is-any-dynamic-variable_-no-enforcement-rspecverifieddoublereference]
6352+
==== Reference is any dynamic variable. No enforcement
64096353
64106354
[source,ruby]
64116355
----
@@ -6415,17 +6359,6 @@ let(:foo) do
64156359
end
64166360
----
64176361
6418-
[#configurable-attributes-rspecverifieddoublereference]
6419-
=== Configurable attributes
6420-
6421-
|===
6422-
| Name | Default value | Configurable values
6423-
6424-
| EnforcedStyle
6425-
| `constant`
6426-
| `constant`, `string`
6427-
|===
6428-
64296362
[#references-rspecverifieddoublereference]
64306363
=== References
64316364

‎lib/rubocop/cop/rspec/string_as_instance_double_constant.rb

-45
This file was deleted.

‎lib/rubocop/cop/rspec/verified_double_reference.rb

+14-53
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ module Cop
55
module RSpec
66
# Checks for consistent verified double reference style.
77
#
8-
# Only investigates references that are one of the supported styles.
9-
#
108
# @see https://rspec.info/features/3-12/rspec-mocks/verifying-doubles
119
#
12-
# This cop can be configured in your configuration using the
13-
# `EnforcedStyle` option and supports `--auto-gen-config`.
10+
# @safety
11+
# This cop is unsafe because the correction requires loading the class.
12+
# Loading before stubbing causes RSpec to only allow instance methods
13+
# to be stubbed.
1414
#
15-
# @example `EnforcedStyle: constant` (default)
15+
# @example
1616
# # bad
1717
# let(:foo) do
1818
# instance_double('ClassName', method_name: 'returned_value')
@@ -23,28 +23,17 @@ module RSpec
2323
# instance_double(ClassName, method_name: 'returned_value')
2424
# end
2525
#
26-
# @example `EnforcedStyle: string`
27-
# # bad
28-
# let(:foo) do
29-
# instance_double(ClassName, method_name: 'returned_value')
30-
# end
31-
#
32-
# # good
33-
# let(:foo) do
34-
# instance_double('ClassName', method_name: 'returned_value')
35-
# end
36-
#
37-
# @example Reference is not in the supported style list. No enforcement
26+
# @example Reference is any dynamic variable. No enforcement
3827
#
3928
# # good
4029
# let(:foo) do
4130
# instance_double(@klass, method_name: 'returned_value')
4231
# end
4332
class VerifiedDoubleReference < Base
4433
extend AutoCorrector
45-
include ConfigurableEnforcedStyle
4634

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

4938
RESTRICT_ON_SEND = Set[
5039
:class_double,
@@ -57,53 +46,25 @@ class VerifiedDoubleReference < Base
5746
:stub_model
5847
].freeze
5948

60-
REFERENCE_TYPE_STYLES = {
61-
str: :string,
62-
const: :constant
63-
}.freeze
64-
6549
# @!method verified_double(node)
6650
def_node_matcher :verified_double, <<~PATTERN
6751
(send
6852
nil?
6953
RESTRICT_ON_SEND
70-
$_class_reference
54+
$str
7155
...)
7256
PATTERN
7357

7458
def on_send(node)
75-
verified_double(node) do |class_reference|
76-
break correct_style_detected unless opposing_style?(class_reference)
77-
78-
message = format(MSG, style: style)
79-
expression = class_reference.source_range
80-
81-
add_offense(expression, message: message) do |corrector|
82-
offense = class_reference.source
83-
corrector.replace(expression, correct_style(offense))
84-
85-
opposite_style_detected
59+
verified_double(node) do |string_argument_node|
60+
add_offense(string_argument_node) do |corrector|
61+
autocorrect(corrector, string_argument_node)
8662
end
8763
end
8864
end
8965

90-
private
91-
92-
def opposing_style?(class_reference)
93-
class_reference_style = REFERENCE_TYPE_STYLES[class_reference.type]
94-
95-
# Only enforce supported styles
96-
return false unless class_reference_style
97-
98-
class_reference_style != style
99-
end
100-
101-
def correct_style(offense)
102-
if style == :string
103-
"'#{offense}'"
104-
else
105-
offense.gsub(/^['"]|['"]$/, '')
106-
end
66+
def autocorrect(corrector, node)
67+
corrector.replace(node, node.value)
10768
end
10869
end
10970
end

‎lib/rubocop/cop/rspec_cops.rb

-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@
9999
require_relative 'rspec/sort_metadata'
100100
require_relative 'rspec/spec_file_path_format'
101101
require_relative 'rspec/spec_file_path_suffix'
102-
require_relative 'rspec/string_as_instance_double_constant'
103102
require_relative 'rspec/stubbed_mock'
104103
require_relative 'rspec/subject_declaration'
105104
require_relative 'rspec/subject_stub'

‎spec/rubocop/cop/rspec/string_as_instance_double_constant_spec.rb

-33
This file was deleted.

0 commit comments

Comments
 (0)