Skip to content

Commit 6bca2ce

Browse files
authored
Merge pull request #999 from r7kamura/date-autocorrect
Add autocorrection for `Rails/Date`
2 parents 6f1a411 + 9068775 commit 6bca2ce

File tree

4 files changed

+50
-2
lines changed

4 files changed

+50
-2
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#999](https://github.com/rubocop/rubocop-rails/pull/999): Add autocorrection for `Rails/Date`. ([@r7kamura][])

config/default.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ Rails/Date:
301301
Checks the correct usage of date aware methods,
302302
such as Date.today, Date.current etc.
303303
Enabled: true
304+
SafeAutoCorrect: false
304305
VersionAdded: '0.30'
305306
VersionChanged: '2.11'
306307
# The value `strict` disallows usage of `Date.today`, `Date.current`,

lib/rubocop/cop/rails/date.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ module Rails
2222
# And you can set a warning for `to_time` with `AllowToTime: false`.
2323
# `AllowToTime` is `true` by default to prevent false positive on `DateTime` object.
2424
#
25+
# @safety
26+
# This cop's autocorrection is unsafe because it may change handling time.
27+
#
2528
# @example EnforcedStyle: flexible (default)
2629
# # bad
2730
# Date.today
@@ -51,6 +54,8 @@ module Rails
5154
# # bad
5255
# date.to_time
5356
class Date < Base
57+
extend AutoCorrector
58+
5459
include ConfigurableEnforcedStyle
5560

5661
MSG = 'Do not use `Date.%<method_called>s` without zone. Use `Time.zone.%<day>s` instead.'
@@ -92,7 +97,9 @@ def check_deprecated_methods(node)
9297

9398
message = format(DEPRECATED_MSG, deprecated: method[:deprecated], relevant: method[:relevant])
9499

95-
add_offense(node.loc.selector, message: message)
100+
add_offense(node.loc.selector, message: message) do |corrector|
101+
corrector.replace(node.loc.selector, method[:relevant].to_s)
102+
end
96103
end
97104
end
98105

@@ -108,7 +115,9 @@ def check_date_node(node)
108115

109116
message = format(MSG, method_called: method_name, day: day)
110117

111-
add_offense(node.loc.selector, message: message)
118+
add_offense(node.loc.selector, message: message) do |corrector|
119+
corrector.replace(node.receiver.loc.name, 'Time.zone')
120+
end
112121
end
113122

114123
def extract_method_chain(node)

spec/rubocop/cop/rails/date_spec.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@
5454
date.to_time_in_current_zone
5555
^^^^^^^^^^^^^^^^^^^^^^^ `to_time_in_current_zone` is deprecated. Use `in_time_zone` instead.
5656
RUBY
57+
58+
expect_correction(<<~RUBY)
59+
date.in_time_zone
60+
RUBY
5761
end
5862

5963
context 'when using safe navigation operator' do
@@ -62,6 +66,10 @@
6266
date&.to_time_in_current_zone
6367
^^^^^^^^^^^^^^^^^^^^^^^ `to_time_in_current_zone` is deprecated. Use `in_time_zone` instead.
6468
RUBY
69+
70+
expect_correction(<<~RUBY)
71+
date&.in_time_zone
72+
RUBY
6573
end
6674
end
6775

@@ -137,6 +145,8 @@
137145
date.to_time
138146
^^^^^^^ Do not use `to_time` on Date objects, because they know nothing about the time zone in use.
139147
RUBY
148+
149+
expect_no_corrections
140150
end
141151

142152
context 'when using safe navigation operator' do
@@ -145,6 +155,8 @@
145155
date&.to_time
146156
^^^^^^^ Do not use `to_time` on Date objects, because they know nothing about the time zone in use.
147157
RUBY
158+
159+
expect_no_corrections
148160
end
149161
end
150162

@@ -162,6 +174,8 @@
162174
"2016-07-12 14:36:31".to_time(:utc)
163175
^^^^^^^ Do not use `to_time` on Date objects, because they know nothing about the time zone in use.
164176
RUBY
177+
178+
expect_no_corrections
165179
end
166180
end
167181

@@ -189,6 +203,10 @@
189203
"2016-07-12 14:36:31".to_time_in_current_zone
190204
^^^^^^^^^^^^^^^^^^^^^^^ `to_time_in_current_zone` is deprecated. Use `in_time_zone` instead.
191205
RUBY
206+
207+
expect_correction(<<~RUBY)
208+
"2016-07-12 14:36:31".in_time_zone
209+
RUBY
192210
end
193211
end
194212

@@ -206,6 +224,21 @@
206224
Date.today
207225
^^^^^ Do not use `Date.today` without zone. Use `Time.zone.today` instead.
208226
RUBY
227+
228+
expect_correction(<<~RUBY)
229+
Time.zone.today
230+
RUBY
231+
end
232+
233+
it 'registers an offense for ::Date.today' do
234+
expect_offense(<<~RUBY)
235+
::Date.today
236+
^^^^^ Do not use `Date.today` without zone. Use `Time.zone.today` instead.
237+
RUBY
238+
239+
expect_correction(<<~RUBY)
240+
::Time.zone.today
241+
RUBY
209242
end
210243

211244
RuboCop::Cop::Rails::TimeZone::ACCEPTED_METHODS.each do |a_method|
@@ -219,6 +252,10 @@
219252
"2016-07-12 14:36:31".to_time_in_current_zone
220253
^^^^^^^^^^^^^^^^^^^^^^^ `to_time_in_current_zone` is deprecated. Use `in_time_zone` instead.
221254
RUBY
255+
256+
expect_correction(<<~RUBY)
257+
"2016-07-12 14:36:31".in_time_zone
258+
RUBY
222259
end
223260
end
224261
end

0 commit comments

Comments
 (0)