Skip to content

Commit 9c18a2b

Browse files
committed
Appease RuboCop
1 parent 29fd700 commit 9c18a2b

File tree

3 files changed

+171
-130
lines changed

3 files changed

+171
-130
lines changed

config/default.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ RSpecRails/NegationBeValid:
7878
Reference: https://www.rubydoc.info/gems/rubocop-rspec_rails/RuboCop/Cop/RSpecRails/NegationBeValid
7979

8080
RSpecRails/Timecop:
81-
Description: Enforces use of `ActiveSupport::Testing::TimeHelpers` instead of `Timecop`.
81+
Description: Enforces use of ActiveSupport TimeHelpers instead of Timecop.
8282
Enabled: pending
8383
VersionAdded: "<<next>>"
8484
SafeAutoCorrect: false

lib/rubocop/cop/rspec_rails/timecop.rb

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
module RuboCop
44
module Cop
55
module RSpecRails
6-
# Enforces use of `ActiveSupport::Testing::TimeHelpers` instead of `Timecop`.
6+
# Enforces use of ActiveSupport TimeHelpers instead of Timecop.
77
#
88
# ## Migration
99
# `Timecop.freeze` should be replaced with `freeze_time` when used
@@ -96,17 +96,25 @@ class Timecop < ::RuboCop::Cop::Base
9696
extend AutoCorrector
9797

9898
FREEZE_MESSAGE = 'Use `%<replacement>s` instead of `Timecop.freeze`'
99-
FREEZE_WITH_ARGUMENTS_MESSAGE = 'Use `travel` or `travel_to` instead of `Timecop.freeze`'
99+
FREEZE_WITH_ARGUMENTS_MESSAGE =
100+
'Use `travel` or `travel_to` instead of `Timecop.freeze`'
100101
RETURN_MESSAGE = 'Use `%<replacement>s` instead of `Timecop.return`'
101-
FLOW_ADDENDUM = 'If you need time to keep flowing, simulate it by travelling again.'
102-
TRAVEL_MESSAGE = "Use `travel` or `travel_to` instead of `Timecop.travel`. #{FLOW_ADDENDUM}"
103-
SCALE_MESSAGE = "Use `travel` or `travel_to` instead of `Timecop.scale`. #{FLOW_ADDENDUM}"
102+
FLOW_ADDENDUM =
103+
'If you need time to keep flowing, simulate it by travelling again.'
104+
TRAVEL_MESSAGE =
105+
'Use `travel` or `travel_to` instead of `Timecop.travel`. ' \
106+
"#{FLOW_ADDENDUM}"
107+
SCALE_MESSAGE =
108+
'Use `travel` or `travel_to` instead of `Timecop.scale`. ' \
109+
"#{FLOW_ADDENDUM}"
104110
MSG = 'Use `ActiveSupport::Testing::TimeHelpers` instead of `Timecop`'
105111

112+
# @!method timecop_const?(node)
106113
def_node_matcher :timecop_const?, <<~PATTERN
107114
(const {nil? cbase} :Timecop)
108115
PATTERN
109116

117+
# @!method timecop_send(node)
110118
def_node_matcher :timecop_send, <<~PATTERN
111119
(send
112120
#timecop_const? ${:freeze :return :scale :travel}
@@ -138,7 +146,9 @@ def on_timecop_send(node, message, arguments)
138146

139147
def on_timecop_freeze(node, arguments)
140148
if arguments.empty?
141-
add_offense(node, message: format(FREEZE_MESSAGE, replacement: preferred_freeze_replacement)) do |corrector|
149+
message =
150+
format(FREEZE_MESSAGE, replacement: preferred_freeze_replacement)
151+
add_offense(node, message: message) do |corrector|
142152
autocorrect_freeze(corrector, node, arguments)
143153
end
144154
else
@@ -147,7 +157,9 @@ def on_timecop_freeze(node, arguments)
147157
end
148158

149159
def on_timecop_return(node, arguments)
150-
add_offense(node, message: format(RETURN_MESSAGE, replacement: preferred_return_replacement)) do |corrector|
160+
message =
161+
format(RETURN_MESSAGE, replacement: preferred_return_replacement)
162+
add_offense(node, message: message) do |corrector|
151163
autocorrect_return(corrector, node, arguments)
152164
end
153165
end
@@ -163,17 +175,19 @@ def on_timecop_travel(node, _arguments)
163175
def autocorrect_freeze(corrector, node, arguments)
164176
return unless arguments.empty?
165177

166-
corrector.replace(receiver_and_message_range(node), preferred_freeze_replacement)
178+
corrector.replace(receiver_and_message_range(node),
179+
preferred_freeze_replacement)
167180
end
168181

169182
def autocorrect_return(corrector, node, _arguments)
170183
return if given_block?(node)
171184

172-
corrector.replace(receiver_and_message_range(node), preferred_return_replacement)
185+
corrector.replace(receiver_and_message_range(node),
186+
preferred_return_replacement)
173187
end
174188

175189
def given_block?(node)
176-
node.send_type? && node.parent && node.parent.block_type? && node.parent.send_node == node
190+
node.parent&.block_type? && node.parent.send_node == node
177191
end
178192

179193
def receiver_and_message_range(node)

0 commit comments

Comments
 (0)