sevencop 0.25.0 → 0.26.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +1 -0
- data/config/default.yml +7 -0
- data/lib/rubocop/cop/sevencop/rails_date_and_time_calculation.rb +779 -0
- data/lib/sevencop/version.rb +1 -1
- data/lib/sevencop.rb +1 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7ae1550d29e9e7aeb18b9f63d2e8f167ef5a7d871f2e953551e7be6c86038430
|
4
|
+
data.tar.gz: fba303001375952d8f8f1e3de021a9a5b155adc79440bb526342391c81be2a9b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a32371dd923b0922ae7072b11d6917cf876eec46061a3fe0a7229a5bac2713d77b66f75ef7259f15838173d20dbd7f039dcb4af0ce0c456216091906eda7f6c4
|
7
|
+
data.tar.gz: a5cece615fd407987d77a5e7d5db08ef78c816a07c3330da17fdd5d778552dfd4ff1d249d044b95540ce06a04b873623853b42a5e466bbe4d389fb284784f75a
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -38,6 +38,7 @@ Note that all cops are `Enabled: false` by default.
|
|
38
38
|
- [Sevencop/MethodDefinitionOrdered](lib/rubocop/cop/sevencop/method_definition_ordered.rb)
|
39
39
|
- [Sevencop/RailsActionName](lib/rubocop/cop/sevencop/rails_action_name.rb)
|
40
40
|
- [Sevencop/RailsBelongsToOptional](lib/rubocop/cop/sevencop/rails_belongs_to_optional.rb)
|
41
|
+
- [Sevencop/RailsDateAndTimeCalculation](lib/rubocop/cop/sevencop/rails_date_and_time_calculation.rb)
|
41
42
|
- [Sevencop/RailsInferredSpecType](lib/rubocop/cop/sevencop/rails_inferred_spec_type.rb)
|
42
43
|
- [Sevencop/RailsOrderField](lib/rubocop/cop/sevencop/rails_order_field.rb)
|
43
44
|
- [Sevencop/RailsUniquenessValidatorExplicitCaseSensitivity](lib/rubocop/cop/sevencop/rails_uniqueness_validator_explicit_case_sensitivity.rb)
|
data/config/default.yml
CHANGED
@@ -91,6 +91,13 @@ Sevencop/RailsBelongsToOptional:
|
|
91
91
|
Safe: false
|
92
92
|
VersionAdded: '0.5'
|
93
93
|
|
94
|
+
Sevencop/RailsDateAndTimeCalculation:
|
95
|
+
Description: |
|
96
|
+
Prefer ActiveSupport date and time helper.
|
97
|
+
Enabled: false
|
98
|
+
Safe: false
|
99
|
+
VersionAdded: '0.26'
|
100
|
+
|
94
101
|
Sevencop/RailsInferredSpecType:
|
95
102
|
Description: |
|
96
103
|
Identifies redundant spec type.
|
@@ -0,0 +1,779 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'set'
|
4
|
+
|
5
|
+
module RuboCop
|
6
|
+
module Cop
|
7
|
+
module Sevencop
|
8
|
+
# Prefer ActiveSupport date and time helper.
|
9
|
+
#
|
10
|
+
# @safety
|
11
|
+
# This cop is unsafe becuase it considers that `n.days` is a Duration,
|
12
|
+
# and `date` in `date == Date.current` is a Date, but there is no guarantee.
|
13
|
+
#
|
14
|
+
# @example
|
15
|
+
# # bad
|
16
|
+
# Time.zone.now
|
17
|
+
#
|
18
|
+
# # good
|
19
|
+
# Time.current
|
20
|
+
#
|
21
|
+
# # bad
|
22
|
+
# Time.zone.today
|
23
|
+
#
|
24
|
+
# # good
|
25
|
+
# Date.current
|
26
|
+
#
|
27
|
+
# # bad
|
28
|
+
# Time.current.to_date
|
29
|
+
#
|
30
|
+
# # good
|
31
|
+
# Date.current
|
32
|
+
#
|
33
|
+
# # bad
|
34
|
+
# Date.current.tomorrow
|
35
|
+
#
|
36
|
+
# # good
|
37
|
+
# Date.tomorrow
|
38
|
+
#
|
39
|
+
# # bad
|
40
|
+
# Date.current.yesterday
|
41
|
+
#
|
42
|
+
# # good
|
43
|
+
# Date.yesterday
|
44
|
+
#
|
45
|
+
# # bad
|
46
|
+
# date == Date.current
|
47
|
+
# Date.current == date
|
48
|
+
#
|
49
|
+
# # good
|
50
|
+
# date.today?
|
51
|
+
#
|
52
|
+
# # bad
|
53
|
+
# date == Date.tomorrow
|
54
|
+
# Date.tomorrow == date
|
55
|
+
#
|
56
|
+
# # good
|
57
|
+
# date.tomorrow?
|
58
|
+
#
|
59
|
+
# # bad
|
60
|
+
# date == Date.yesterday
|
61
|
+
# Date.yesterday == date
|
62
|
+
#
|
63
|
+
# # good
|
64
|
+
# date.yesterday?
|
65
|
+
#
|
66
|
+
# # bad
|
67
|
+
# Time.current - n.days
|
68
|
+
# Time.zone.now - n.days
|
69
|
+
#
|
70
|
+
# # good
|
71
|
+
# n.days.ago
|
72
|
+
#
|
73
|
+
# # bad
|
74
|
+
# Time.current + n.days
|
75
|
+
#
|
76
|
+
# # good
|
77
|
+
# n.days.since
|
78
|
+
#
|
79
|
+
# # bad
|
80
|
+
# Date.current - 1
|
81
|
+
# Date.current - 1.day
|
82
|
+
#
|
83
|
+
# # good
|
84
|
+
# Date.yesterday
|
85
|
+
#
|
86
|
+
# # bad
|
87
|
+
# Date.current + 1
|
88
|
+
# Date.current + 1.day
|
89
|
+
#
|
90
|
+
# # good
|
91
|
+
# Date.tomorrow
|
92
|
+
#
|
93
|
+
# # bad
|
94
|
+
# time.after?(Time.current)
|
95
|
+
# time > Time.current
|
96
|
+
# Time.current < time
|
97
|
+
# Time.current.before?(time)
|
98
|
+
#
|
99
|
+
# # good
|
100
|
+
# time.future?
|
101
|
+
#
|
102
|
+
# # bad
|
103
|
+
# time.before?(Time.current)
|
104
|
+
# time < Time.current
|
105
|
+
# Time.current > time
|
106
|
+
# Time.current.after?(time)
|
107
|
+
#
|
108
|
+
# # good
|
109
|
+
# time.past?
|
110
|
+
class RailsDateAndTimeCalculation < Base
|
111
|
+
extend AutoCorrector
|
112
|
+
|
113
|
+
CALCULATION_METHOD_NAMES = ::Set.new(
|
114
|
+
%i[
|
115
|
+
-
|
116
|
+
+
|
117
|
+
]
|
118
|
+
).freeze
|
119
|
+
|
120
|
+
COMPARISON_METHOD_NAMES = ::Set.new(
|
121
|
+
%i[
|
122
|
+
<
|
123
|
+
>
|
124
|
+
after?
|
125
|
+
before?
|
126
|
+
]
|
127
|
+
).freeze
|
128
|
+
|
129
|
+
DURATION_METHOD_NAMES = ::Set.new(
|
130
|
+
%i[
|
131
|
+
day
|
132
|
+
days
|
133
|
+
fortnight
|
134
|
+
fortnights
|
135
|
+
hour
|
136
|
+
hours
|
137
|
+
minute
|
138
|
+
minutes
|
139
|
+
month
|
140
|
+
months
|
141
|
+
second
|
142
|
+
seconds
|
143
|
+
week
|
144
|
+
weeks
|
145
|
+
year
|
146
|
+
years
|
147
|
+
]
|
148
|
+
).freeze
|
149
|
+
|
150
|
+
MSG = 'Prefer ActiveSupport date and time helper.'
|
151
|
+
|
152
|
+
RESTRICT_ON_SEND = [
|
153
|
+
*CALCULATION_METHOD_NAMES,
|
154
|
+
*COMPARISON_METHOD_NAMES,
|
155
|
+
:==,
|
156
|
+
:now,
|
157
|
+
:to_date,
|
158
|
+
:today,
|
159
|
+
:tomorrow,
|
160
|
+
:yesterday
|
161
|
+
].freeze
|
162
|
+
|
163
|
+
# @param node [RuboCop::AST::SendNode]
|
164
|
+
# @return [void]
|
165
|
+
def on_send(node)
|
166
|
+
case node.method_name
|
167
|
+
when :-
|
168
|
+
check_subtraction(node)
|
169
|
+
when :+
|
170
|
+
check_addition(node)
|
171
|
+
when :==
|
172
|
+
check_equality(node)
|
173
|
+
when :<, :before?
|
174
|
+
check_less_than(node)
|
175
|
+
when :>, :after?
|
176
|
+
check_greater_than(node)
|
177
|
+
when :now
|
178
|
+
check_now(node)
|
179
|
+
when :to_date
|
180
|
+
check_to_date(node)
|
181
|
+
when :today
|
182
|
+
check_today(node)
|
183
|
+
when :tomorrow
|
184
|
+
check_tomorrow(node)
|
185
|
+
when :yesterday
|
186
|
+
check_yesterday(node)
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
private
|
191
|
+
|
192
|
+
# @!method comparison_to_time_current?(node)
|
193
|
+
# @param node [RuboCop::AST::Node]
|
194
|
+
# @return [Boolean]
|
195
|
+
def_node_matcher :comparison_to_time_current?, <<~PATTERN
|
196
|
+
{
|
197
|
+
#comparison_for_future? |
|
198
|
+
#comparison_for_past?
|
199
|
+
}
|
200
|
+
PATTERN
|
201
|
+
|
202
|
+
# @!method comparison_for_future?(node)
|
203
|
+
# @param node [RuboCop::AST::Node]
|
204
|
+
# @return [Boolean]
|
205
|
+
def_node_matcher :comparison_for_future?, <<~PATTERN
|
206
|
+
{
|
207
|
+
#comparison_for_future_with_time_current_left? |
|
208
|
+
#comparison_for_future_with_time_current_right?
|
209
|
+
}
|
210
|
+
PATTERN
|
211
|
+
|
212
|
+
# @!method comparison_for_future_with_time_current_left?(node)
|
213
|
+
# @param node [RuboCop::AST::Node]
|
214
|
+
# @return [Boolean]
|
215
|
+
def_node_matcher :comparison_for_future_with_time_current_left?, <<~PATTERN
|
216
|
+
(send
|
217
|
+
#time_current?
|
218
|
+
{:< | :before?}
|
219
|
+
_
|
220
|
+
)
|
221
|
+
PATTERN
|
222
|
+
|
223
|
+
# @!method comparison_for_future_with_time_current_right?(node)
|
224
|
+
# @param node [RuboCop::AST::Node]
|
225
|
+
# @return [Boolean]
|
226
|
+
def_node_matcher :comparison_for_future_with_time_current_right?, <<~PATTERN
|
227
|
+
(send
|
228
|
+
_
|
229
|
+
{:> | :after?}
|
230
|
+
#time_current?
|
231
|
+
)
|
232
|
+
PATTERN
|
233
|
+
|
234
|
+
# @!method comparison_for_past?(node)
|
235
|
+
# @param node [RuboCop::AST::Node]
|
236
|
+
# @return [Boolean]
|
237
|
+
def_node_matcher :comparison_for_past?, <<~PATTERN
|
238
|
+
{
|
239
|
+
#comparison_for_past_with_time_current_left? |
|
240
|
+
#comparison_for_past_with_time_current_right?
|
241
|
+
}
|
242
|
+
PATTERN
|
243
|
+
|
244
|
+
# @!method comparison_for_past_with_time_current_left?(node)
|
245
|
+
# @param node [RuboCop::AST::Node]
|
246
|
+
# @return [Boolean]
|
247
|
+
def_node_matcher :comparison_for_past_with_time_current_left?, <<~PATTERN
|
248
|
+
(send
|
249
|
+
#time_current?
|
250
|
+
{:> | :after?}
|
251
|
+
_
|
252
|
+
)
|
253
|
+
PATTERN
|
254
|
+
|
255
|
+
# @!method comparison_for_past_with_time_current_right?(node)
|
256
|
+
# @param node [RuboCop::AST::Node]
|
257
|
+
# @return [Boolean]
|
258
|
+
def_node_matcher :comparison_for_past_with_time_current_right?, <<~PATTERN
|
259
|
+
(send
|
260
|
+
_
|
261
|
+
{:< | :before?}
|
262
|
+
#time_current?
|
263
|
+
)
|
264
|
+
PATTERN
|
265
|
+
|
266
|
+
# @!method date_current?(node)
|
267
|
+
# @param node [RuboCop::AST::Node]
|
268
|
+
# @return [Booelan]
|
269
|
+
def_node_matcher :date_current?, <<~PATTERN
|
270
|
+
(send
|
271
|
+
(const
|
272
|
+
{nil? | cbase}
|
273
|
+
:Date
|
274
|
+
)
|
275
|
+
:current
|
276
|
+
)
|
277
|
+
PATTERN
|
278
|
+
|
279
|
+
# @!method date_current_minus_one_day?(node)
|
280
|
+
# @param node [RuboCop::AST::Node]
|
281
|
+
# @return [Boolean]
|
282
|
+
def_node_matcher :date_current_minus_one_day?, <<~PATTERN
|
283
|
+
(send
|
284
|
+
#date_current?
|
285
|
+
:-
|
286
|
+
#one_day_on_date_calculation?
|
287
|
+
)
|
288
|
+
PATTERN
|
289
|
+
|
290
|
+
# @!method date_current_plus_one_day?(node)
|
291
|
+
# @param node [RuboCop::AST::Node]
|
292
|
+
# @return [Boolean]
|
293
|
+
def_node_matcher :date_current_plus_one_day?, <<~PATTERN
|
294
|
+
(send
|
295
|
+
#date_current?
|
296
|
+
:+
|
297
|
+
#one_day_on_date_calculation?
|
298
|
+
)
|
299
|
+
PATTERN
|
300
|
+
|
301
|
+
# @!method date_current_with?(node, method_name)
|
302
|
+
# @param node [RuboCop::AST::Node]
|
303
|
+
# @param method_name [Symbol]
|
304
|
+
# @return [Boolean]
|
305
|
+
def_node_matcher :date_current_with?, <<~PATTERN
|
306
|
+
(send
|
307
|
+
#date_current?
|
308
|
+
%1
|
309
|
+
)
|
310
|
+
PATTERN
|
311
|
+
|
312
|
+
# @!method date_with?(node, method_name)
|
313
|
+
# @param node [RuboCop::AST::Node]
|
314
|
+
# @param method_name [Symbol]
|
315
|
+
# @return [Boolean]
|
316
|
+
def_node_matcher :date_with?, <<~PATTERN
|
317
|
+
(send
|
318
|
+
(const
|
319
|
+
{nil? | cbase}
|
320
|
+
:Date
|
321
|
+
)
|
322
|
+
%1
|
323
|
+
)
|
324
|
+
PATTERN
|
325
|
+
|
326
|
+
# @!method duration?(node)
|
327
|
+
# @param node [RuboCop::AST::Node]
|
328
|
+
# @return [Boolean]
|
329
|
+
def_node_matcher :duration?, <<~PATTERN
|
330
|
+
(send
|
331
|
+
_
|
332
|
+
DURATION_METHOD_NAMES
|
333
|
+
)
|
334
|
+
PATTERN
|
335
|
+
|
336
|
+
# @!method duration_calculation_to_time_current?(node)
|
337
|
+
# @param node [RuboCop::AST::Node]
|
338
|
+
# @return [Boolean]
|
339
|
+
def_node_matcher :duration_calculation_to_time_current?, <<~PATTERN
|
340
|
+
(send
|
341
|
+
#time_current?
|
342
|
+
_
|
343
|
+
#duration?
|
344
|
+
)
|
345
|
+
PATTERN
|
346
|
+
|
347
|
+
# @!method equals_to_date_with_left?(node, method_name)
|
348
|
+
# @param node [RuboCop::AST::Node]
|
349
|
+
# @param method_name [Symbol]
|
350
|
+
# @return [Boolean]
|
351
|
+
def_node_matcher :equals_to_date_with_left?, <<~PATTERN
|
352
|
+
(send
|
353
|
+
#date_with?(%1)
|
354
|
+
:==
|
355
|
+
_
|
356
|
+
)
|
357
|
+
PATTERN
|
358
|
+
|
359
|
+
# @!method equals_to_date_with_right?(node, method_name)
|
360
|
+
# @param node [RuboCop::AST::Node]
|
361
|
+
# @param method_name [Symbol]
|
362
|
+
# @return [Boolean]
|
363
|
+
def_node_matcher :equals_to_date_with_right?, <<~PATTERN
|
364
|
+
(send
|
365
|
+
_
|
366
|
+
:==
|
367
|
+
#date_with?(%1)
|
368
|
+
)
|
369
|
+
PATTERN
|
370
|
+
|
371
|
+
# @!method one_day_on_date_calculation?(node)
|
372
|
+
# @param node [RuboCop::AST::Node]
|
373
|
+
# @return [Boolean]
|
374
|
+
def_node_matcher :one_day_on_date_calculation?, <<~PATTERN
|
375
|
+
{
|
376
|
+
(int 1) |
|
377
|
+
(send (int 1) :day)
|
378
|
+
}
|
379
|
+
PATTERN
|
380
|
+
|
381
|
+
# @!method time_current?(node)
|
382
|
+
# @param node [RuboCop::AST::Node]
|
383
|
+
# @return [Boolean]
|
384
|
+
def_node_matcher :time_current?, <<~PATTERN
|
385
|
+
(send
|
386
|
+
(const
|
387
|
+
{nil? | cbase}
|
388
|
+
:Time
|
389
|
+
)
|
390
|
+
:current
|
391
|
+
)
|
392
|
+
PATTERN
|
393
|
+
|
394
|
+
# @!method time_current_to_date?(node)
|
395
|
+
# @param node [RuboCop::AST::Node]
|
396
|
+
# @return [Boolean]
|
397
|
+
def_node_matcher :time_current_to_date?, <<~PATTERN
|
398
|
+
(send
|
399
|
+
#time_current?
|
400
|
+
:to_date
|
401
|
+
)
|
402
|
+
PATTERN
|
403
|
+
|
404
|
+
# @!method time_zone_with?(node, method_name)
|
405
|
+
# @param node [RuboCop::AST::Node]
|
406
|
+
# @param method_name [Symbol]
|
407
|
+
# @return [Boolean]
|
408
|
+
def_node_matcher :time_zone_with?, <<~PATTERN
|
409
|
+
(send
|
410
|
+
(send
|
411
|
+
(const
|
412
|
+
{nil? | cbase}
|
413
|
+
:Time
|
414
|
+
)
|
415
|
+
:zone
|
416
|
+
)
|
417
|
+
%1
|
418
|
+
)
|
419
|
+
PATTERN
|
420
|
+
|
421
|
+
# @param corrector [RuboCop::Cop::Corrector]
|
422
|
+
# @param node [RuboCop::AST::SendNode]
|
423
|
+
# @return [void]
|
424
|
+
def autocorrect_to_comparison_helper_for_time_current(
|
425
|
+
corrector,
|
426
|
+
node
|
427
|
+
)
|
428
|
+
corrector.replace(
|
429
|
+
node,
|
430
|
+
format_to_method_call(
|
431
|
+
method: helper_method_name_for_comparison(node),
|
432
|
+
receiver: find_comparison_subject_to_time_current(node)
|
433
|
+
)
|
434
|
+
)
|
435
|
+
end
|
436
|
+
|
437
|
+
# @param corrector [RuboCop::Cop::Corrector]
|
438
|
+
# @param helper_method_name [Symbol]
|
439
|
+
# @param node [RuboCop::AST::SendNode]
|
440
|
+
# @return [void]
|
441
|
+
def autocorrect_to_duration_helper(
|
442
|
+
corrector,
|
443
|
+
node,
|
444
|
+
helper_method_name:
|
445
|
+
)
|
446
|
+
corrector.replace(
|
447
|
+
node,
|
448
|
+
format_to_method_call(
|
449
|
+
method: helper_method_name,
|
450
|
+
receiver: node.first_argument
|
451
|
+
)
|
452
|
+
)
|
453
|
+
end
|
454
|
+
|
455
|
+
# @param corrector [RuboCop::Cop::Corrector]
|
456
|
+
# @param date_method_name [Symbol]
|
457
|
+
# @param helper_method_name [Symbol]
|
458
|
+
# @param node [RuboCop::AST::SendNode]
|
459
|
+
# @return [void]
|
460
|
+
def autocorrect_to_equality_helper_for_date(
|
461
|
+
corrector,
|
462
|
+
node,
|
463
|
+
date_method_name:,
|
464
|
+
helper_method_name:
|
465
|
+
)
|
466
|
+
corrector.replace(
|
467
|
+
node,
|
468
|
+
format_to_method_call(
|
469
|
+
method: helper_method_name,
|
470
|
+
receiver: find_equality_subject_to_date_with(node, date_method_name)
|
471
|
+
)
|
472
|
+
)
|
473
|
+
end
|
474
|
+
|
475
|
+
# @param node [RuboCop::AST::SendNode]
|
476
|
+
# @return [void]
|
477
|
+
def check_addition(node)
|
478
|
+
check_addition_to_date_current(node)
|
479
|
+
check_addition_to_time_current(node)
|
480
|
+
end
|
481
|
+
|
482
|
+
# @param node [RuboCop::AST::SendNode]
|
483
|
+
# @return [void]
|
484
|
+
def check_addition_to_date_current(node)
|
485
|
+
return unless date_current_plus_one_day?(node)
|
486
|
+
|
487
|
+
add_offense(node) do |corrector|
|
488
|
+
replace_keeping_cbase(
|
489
|
+
corrector: corrector,
|
490
|
+
node: node,
|
491
|
+
with: 'Date.tomorrow'
|
492
|
+
)
|
493
|
+
end
|
494
|
+
end
|
495
|
+
|
496
|
+
# @param node [RuboCop::AST::SendNode]
|
497
|
+
# @return [void]
|
498
|
+
def check_addition_to_time_current(node)
|
499
|
+
return unless duration_calculation_to_time_current?(node)
|
500
|
+
|
501
|
+
add_offense(node) do |corrector|
|
502
|
+
autocorrect_to_duration_helper(
|
503
|
+
corrector,
|
504
|
+
node,
|
505
|
+
helper_method_name: :since
|
506
|
+
)
|
507
|
+
end
|
508
|
+
end
|
509
|
+
|
510
|
+
# @param node [RuboCop::AST::SendNode]
|
511
|
+
# @return [void]
|
512
|
+
def check_equality(node)
|
513
|
+
check_equality_to_date_current(node)
|
514
|
+
check_equality_to_date_tomorrow(node)
|
515
|
+
check_equality_to_date_yesterday(node)
|
516
|
+
end
|
517
|
+
|
518
|
+
# @param node [RuboCop::AST::SendNode]
|
519
|
+
# @return [void]
|
520
|
+
def check_equality_to_date_current(node)
|
521
|
+
return unless equals_to_date_with?(node, :current)
|
522
|
+
|
523
|
+
add_offense(node) do |corrector|
|
524
|
+
autocorrect_to_equality_helper_for_date(
|
525
|
+
corrector,
|
526
|
+
node,
|
527
|
+
date_method_name: :current,
|
528
|
+
helper_method_name: :today?
|
529
|
+
)
|
530
|
+
end
|
531
|
+
end
|
532
|
+
|
533
|
+
# @param node [RuboCop::AST::SendNode]
|
534
|
+
# @return [void]
|
535
|
+
def check_equality_to_date_tomorrow(node)
|
536
|
+
return unless equals_to_date_with?(node, :tomorrow)
|
537
|
+
|
538
|
+
add_offense(node) do |corrector|
|
539
|
+
autocorrect_to_equality_helper_for_date(
|
540
|
+
corrector,
|
541
|
+
node,
|
542
|
+
date_method_name: :tomorrow,
|
543
|
+
helper_method_name: :tomorrow?
|
544
|
+
)
|
545
|
+
end
|
546
|
+
end
|
547
|
+
|
548
|
+
# @param node [RuboCop::AST::SendNode]
|
549
|
+
# @return [void]
|
550
|
+
def check_equality_to_date_yesterday(node)
|
551
|
+
return unless equals_to_date_with?(node, :yesterday)
|
552
|
+
|
553
|
+
add_offense(node) do |corrector|
|
554
|
+
autocorrect_to_equality_helper_for_date(
|
555
|
+
corrector,
|
556
|
+
node,
|
557
|
+
date_method_name: :yesterday,
|
558
|
+
helper_method_name: :yesterday?
|
559
|
+
)
|
560
|
+
end
|
561
|
+
end
|
562
|
+
|
563
|
+
# @param node [RuboCop::AST::SendNode]
|
564
|
+
# @return [void]
|
565
|
+
def check_greater_than(node)
|
566
|
+
return unless comparison_to_time_current?(node)
|
567
|
+
|
568
|
+
add_offense(node) do |corrector|
|
569
|
+
autocorrect_to_comparison_helper_for_time_current(
|
570
|
+
corrector,
|
571
|
+
node
|
572
|
+
)
|
573
|
+
end
|
574
|
+
end
|
575
|
+
alias check_less_than check_greater_than
|
576
|
+
|
577
|
+
# @param node [RuboCop::AST::SendNode]
|
578
|
+
# @return [void]
|
579
|
+
def check_now(node)
|
580
|
+
return unless time_zone_with?(node, :now)
|
581
|
+
|
582
|
+
add_offense(node) do |corrector|
|
583
|
+
replace_keeping_cbase(
|
584
|
+
corrector: corrector,
|
585
|
+
node: node,
|
586
|
+
with: 'Time.current'
|
587
|
+
)
|
588
|
+
end
|
589
|
+
end
|
590
|
+
|
591
|
+
# @param node [RuboCop::AST::SendNode]
|
592
|
+
# @return [void]
|
593
|
+
def check_subtraction(node)
|
594
|
+
check_subtraction_to_date_current(node)
|
595
|
+
check_subtraction_to_time_current(node)
|
596
|
+
end
|
597
|
+
|
598
|
+
# @param node [RuboCop::AST::SendNode]
|
599
|
+
# @return [void]
|
600
|
+
def check_subtraction_to_date_current(node)
|
601
|
+
return unless date_current_minus_one_day?(node)
|
602
|
+
|
603
|
+
add_offense(node) do |corrector|
|
604
|
+
replace_keeping_cbase(
|
605
|
+
corrector: corrector,
|
606
|
+
node: node,
|
607
|
+
with: 'Date.yesterday'
|
608
|
+
)
|
609
|
+
end
|
610
|
+
end
|
611
|
+
|
612
|
+
# @param node [RuboCop::AST::SendNode]
|
613
|
+
# @return [void]
|
614
|
+
def check_subtraction_to_time_current(node)
|
615
|
+
return unless duration_calculation_to_time_current?(node)
|
616
|
+
|
617
|
+
add_offense(node) do |corrector|
|
618
|
+
autocorrect_to_duration_helper(
|
619
|
+
corrector,
|
620
|
+
node,
|
621
|
+
helper_method_name: :ago
|
622
|
+
)
|
623
|
+
end
|
624
|
+
end
|
625
|
+
|
626
|
+
# @param node [RuboCop::AST::SendNode]
|
627
|
+
# @return [void]
|
628
|
+
def check_to_date(node)
|
629
|
+
return unless time_current_to_date?(node)
|
630
|
+
|
631
|
+
add_offense(node) do |corrector|
|
632
|
+
replace_keeping_cbase(
|
633
|
+
corrector: corrector,
|
634
|
+
node: node,
|
635
|
+
with: 'Date.current'
|
636
|
+
)
|
637
|
+
end
|
638
|
+
end
|
639
|
+
|
640
|
+
# @param node [RuboCop::AST::SendNode]
|
641
|
+
# @return [void]
|
642
|
+
def check_today(node)
|
643
|
+
check_today_to_date_current(node)
|
644
|
+
check_today_to_time_zone(node)
|
645
|
+
end
|
646
|
+
|
647
|
+
# @param node [RuboCop::AST::SendNode]
|
648
|
+
# @return [void]
|
649
|
+
def check_today_to_date_current(node)
|
650
|
+
return unless date_current_with?(node, :today)
|
651
|
+
|
652
|
+
add_offense(node) do |corrector|
|
653
|
+
replace_keeping_cbase(
|
654
|
+
corrector: corrector,
|
655
|
+
node: node,
|
656
|
+
with: 'Date.current'
|
657
|
+
)
|
658
|
+
end
|
659
|
+
end
|
660
|
+
|
661
|
+
# @param node [RuboCop::AST::SendNode]
|
662
|
+
# @return [void]
|
663
|
+
def check_today_to_time_zone(node)
|
664
|
+
return unless time_zone_with?(node, :today)
|
665
|
+
|
666
|
+
add_offense(node) do |corrector|
|
667
|
+
replace_keeping_cbase(
|
668
|
+
corrector: corrector,
|
669
|
+
node: node,
|
670
|
+
with: 'Date.current'
|
671
|
+
)
|
672
|
+
end
|
673
|
+
end
|
674
|
+
|
675
|
+
# @param node [RuboCop::AST::SendNode]
|
676
|
+
# @return [void]
|
677
|
+
def check_tomorrow(node)
|
678
|
+
return unless date_current_with?(node, :tomorrow)
|
679
|
+
|
680
|
+
add_offense(node) do |corrector|
|
681
|
+
replace_keeping_cbase(
|
682
|
+
corrector: corrector,
|
683
|
+
node: node,
|
684
|
+
with: 'Date.tomorrow'
|
685
|
+
)
|
686
|
+
end
|
687
|
+
end
|
688
|
+
|
689
|
+
# @param node [RuboCop::AST::SendNode]
|
690
|
+
# @return [void]
|
691
|
+
def check_yesterday(node)
|
692
|
+
return unless date_current_with?(node, :yesterday)
|
693
|
+
|
694
|
+
add_offense(node) do |corrector|
|
695
|
+
replace_keeping_cbase(
|
696
|
+
corrector: corrector,
|
697
|
+
node: node,
|
698
|
+
with: 'Date.yesterday'
|
699
|
+
)
|
700
|
+
end
|
701
|
+
end
|
702
|
+
|
703
|
+
# @param node [RuboCop::AST::SendNode]
|
704
|
+
# @param method_name [Symbol]
|
705
|
+
# @return [Boolean]
|
706
|
+
def equals_to_date_with?(
|
707
|
+
node,
|
708
|
+
method_name
|
709
|
+
)
|
710
|
+
equals_to_date_with_left?(node, method_name) ||
|
711
|
+
equals_to_date_with_right?(node, method_name)
|
712
|
+
end
|
713
|
+
|
714
|
+
# @param node [RuboCop::AST::SendNode]
|
715
|
+
# @return [RuboCop::AST::Node]
|
716
|
+
def find_comparison_subject_to_time_current(node)
|
717
|
+
if time_current?(node.receiver)
|
718
|
+
node.first_argument
|
719
|
+
else
|
720
|
+
node.receiver
|
721
|
+
end
|
722
|
+
end
|
723
|
+
|
724
|
+
# @param node [RuboCop::AST::SendNode]
|
725
|
+
# @param method_name [Symbol]
|
726
|
+
# @return [RuboCop::AST::Node]
|
727
|
+
def find_equality_subject_to_date_with(
|
728
|
+
node,
|
729
|
+
method_name
|
730
|
+
)
|
731
|
+
if date_with?(node.receiver, method_name)
|
732
|
+
node.first_argument
|
733
|
+
else
|
734
|
+
node.receiver
|
735
|
+
end
|
736
|
+
end
|
737
|
+
|
738
|
+
# @param receiver [RuboCop::AST::Node]
|
739
|
+
# @param method [String]
|
740
|
+
# @return [String]
|
741
|
+
def format_to_method_call(
|
742
|
+
method:,
|
743
|
+
receiver:
|
744
|
+
)
|
745
|
+
receiver_source = receiver.source
|
746
|
+
receiver_source = "(#{receiver_source})" if receiver.operator_method?
|
747
|
+
[receiver_source, method].join('.')
|
748
|
+
end
|
749
|
+
|
750
|
+
# @param node [RuboCop::AST::SendNode]
|
751
|
+
# @return [Symbol]
|
752
|
+
def helper_method_name_for_comparison(node)
|
753
|
+
if comparison_for_future?(node)
|
754
|
+
:future?
|
755
|
+
else
|
756
|
+
:past?
|
757
|
+
end
|
758
|
+
end
|
759
|
+
|
760
|
+
# @param corrector [RuboCop::Cop::Corrector]
|
761
|
+
# @param node [RuboCop::AST::SendNode]
|
762
|
+
# @param with [String]
|
763
|
+
# @return [void]
|
764
|
+
def replace_keeping_cbase(
|
765
|
+
corrector:,
|
766
|
+
node:,
|
767
|
+
with:
|
768
|
+
)
|
769
|
+
corrector.replace(
|
770
|
+
node.location.expression.with(
|
771
|
+
begin_pos: node.each_descendant(:const).first.location.name.begin_pos
|
772
|
+
),
|
773
|
+
with
|
774
|
+
)
|
775
|
+
end
|
776
|
+
end
|
777
|
+
end
|
778
|
+
end
|
779
|
+
end
|
data/lib/sevencop/version.rb
CHANGED
data/lib/sevencop.rb
CHANGED
@@ -14,6 +14,7 @@ require_relative 'rubocop/cop/sevencop/method_definition_keyword_argument_ordere
|
|
14
14
|
require_relative 'rubocop/cop/sevencop/method_definition_ordered'
|
15
15
|
require_relative 'rubocop/cop/sevencop/rails_action_name'
|
16
16
|
require_relative 'rubocop/cop/sevencop/rails_belongs_to_optional'
|
17
|
+
require_relative 'rubocop/cop/sevencop/rails_date_and_time_calculation'
|
17
18
|
require_relative 'rubocop/cop/sevencop/rails_inferred_spec_type'
|
18
19
|
require_relative 'rubocop/cop/sevencop/rails_order_field'
|
19
20
|
require_relative 'rubocop/cop/sevencop/rails_uniqueness_validator_explicit_case_sensitivity'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sevencop
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.26.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryo Nakamura
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-11-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -66,6 +66,7 @@ files:
|
|
66
66
|
- lib/rubocop/cop/sevencop/method_definition_ordered.rb
|
67
67
|
- lib/rubocop/cop/sevencop/rails_action_name.rb
|
68
68
|
- lib/rubocop/cop/sevencop/rails_belongs_to_optional.rb
|
69
|
+
- lib/rubocop/cop/sevencop/rails_date_and_time_calculation.rb
|
69
70
|
- lib/rubocop/cop/sevencop/rails_inferred_spec_type.rb
|
70
71
|
- lib/rubocop/cop/sevencop/rails_order_field.rb
|
71
72
|
- lib/rubocop/cop/sevencop/rails_uniqueness_validator_explicit_case_sensitivity.rb
|