3
3
module RuboCop
4
4
module Cop
5
5
module RSpecRails
6
- # Enforces use of ` ActiveSupport::Testing:: TimeHelpers` instead of ` Timecop` .
6
+ # Enforces use of ActiveSupport TimeHelpers instead of Timecop.
7
7
#
8
8
# ## Migration
9
9
# `Timecop.freeze` should be replaced with `freeze_time` when used
@@ -96,17 +96,25 @@ class Timecop < ::RuboCop::Cop::Base
96
96
extend AutoCorrector
97
97
98
98
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`'
100
101
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 } "
104
110
MSG = 'Use `ActiveSupport::Testing::TimeHelpers` instead of `Timecop`'
105
111
112
+ # @!method timecop_const?(node)
106
113
def_node_matcher :timecop_const? , <<~PATTERN
107
114
(const {nil? cbase} :Timecop)
108
115
PATTERN
109
116
117
+ # @!method timecop_send(node)
110
118
def_node_matcher :timecop_send , <<~PATTERN
111
119
(send
112
120
#timecop_const? ${:freeze :return :scale :travel}
@@ -138,7 +146,9 @@ def on_timecop_send(node, message, arguments)
138
146
139
147
def on_timecop_freeze ( node , arguments )
140
148
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 |
142
152
autocorrect_freeze ( corrector , node , arguments )
143
153
end
144
154
else
@@ -147,7 +157,9 @@ def on_timecop_freeze(node, arguments)
147
157
end
148
158
149
159
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 |
151
163
autocorrect_return ( corrector , node , arguments )
152
164
end
153
165
end
@@ -163,17 +175,19 @@ def on_timecop_travel(node, _arguments)
163
175
def autocorrect_freeze ( corrector , node , arguments )
164
176
return unless arguments . empty?
165
177
166
- corrector . replace ( receiver_and_message_range ( node ) , preferred_freeze_replacement )
178
+ corrector . replace ( receiver_and_message_range ( node ) ,
179
+ preferred_freeze_replacement )
167
180
end
168
181
169
182
def autocorrect_return ( corrector , node , _arguments )
170
183
return if given_block? ( node )
171
184
172
- corrector . replace ( receiver_and_message_range ( node ) , preferred_return_replacement )
185
+ corrector . replace ( receiver_and_message_range ( node ) ,
186
+ preferred_return_replacement )
173
187
end
174
188
175
189
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
177
191
end
178
192
179
193
def receiver_and_message_range ( node )
0 commit comments