forked from rubocop/rubocop-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime_zone.rb
249 lines (207 loc) · 7.29 KB
/
time_zone.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# frozen_string_literal: true
module RuboCop
module Cop
module Rails
# This cop checks for the use of Time methods without zone.
#
# Built on top of Ruby on Rails style guide (https://rails.rubystyle.guide#time)
# and the article http://danilenko.org/2012/7/6/rails_timezones/
#
# Two styles are supported for this cop. When EnforcedStyle is 'strict'
# then only use of Time.zone is allowed.
#
# When EnforcedStyle is 'flexible' then it's also allowed
# to use Time.in_time_zone.
#
# @example EnforcedStyle: strict
# # `strict` means that `Time` should be used with `zone`.
#
# # bad
# Time.now
# Time.parse('2015-03-02 19:05:37')
#
# # bad
# Time.current
# Time.at(timestamp).in_time_zone
#
# # good
# Time.zone.now
# Time.zone.parse('2015-03-02 19:05:37')
#
# @example EnforcedStyle: flexible (default)
# # `flexible` allows usage of `in_time_zone` instead of `zone`.
#
# # bad
# Time.now
# Time.parse('2015-03-02 19:05:37')
#
# # good
# Time.zone.now
# Time.zone.parse('2015-03-02 19:05:37')
#
# # good
# Time.current
# Time.at(timestamp).in_time_zone
class TimeZone < Cop
include ConfigurableEnforcedStyle
MSG = 'Do not use `%<current>s` without zone. Use `%<prefer>s` ' \
'instead.'
MSG_ACCEPTABLE = 'Do not use `%<current>s` without zone. ' \
'Use one of %<prefer>s instead.'
MSG_LOCALTIME = 'Do not use `Time.localtime` without ' \
'offset or zone.'
GOOD_METHODS = %i[zone zone_default find_zone find_zone!].freeze
DANGEROUS_METHODS = %i[now local new parse at current].freeze
ACCEPTED_METHODS = %i[in_time_zone utc getlocal xmlschema iso8601
jisx0301 rfc3339 httpdate to_i to_f].freeze
def on_const(node)
mod, klass = *node
# we should only check core classes
# (`Time` or `::Time`)
return unless (mod.nil? || mod.cbase_type?) && method_send?(node)
check_time_node(klass, node.parent) if klass == :Time
end
def autocorrect(node)
lambda do |corrector|
# add `.zone`: `Time.at` => `Time.zone.at`
corrector.insert_after(node.children[0].source_range, '.zone')
case node.method_name
when :current
# replace `Time.zone.current` => `Time.zone.now`
corrector.replace(node.loc.selector, 'now')
when :new
autocorrect_time_new(node, corrector)
end
# prefer `Time` over `DateTime` class
if strict?
corrector.replace(node.children.first.source_range, 'Time')
end
remove_redundant_in_time_zone(corrector, node)
end
end
private
def autocorrect_time_new(node, corrector)
if node.arguments?
corrector.replace(node.loc.selector, 'local')
else
corrector.replace(node.loc.selector, 'now')
end
end
# remove redundant `.in_time_zone` from `Time.zone.now.in_time_zone`
def remove_redundant_in_time_zone(corrector, node)
time_methods_called = extract_method_chain(node)
return unless time_methods_called.include?(:in_time_zone) ||
time_methods_called.include?(:zone)
while node&.send_type?
if node.children.last == :in_time_zone
in_time_zone_with_dot =
node.loc.selector.adjust(begin_pos: -1)
corrector.remove(in_time_zone_with_dot)
end
node = node.parent
end
end
def check_time_node(klass, node)
chain = extract_method_chain(node)
return if not_danger_chain?(chain)
return check_localtime(node) if need_check_localtime?(chain)
method_name = (chain & DANGEROUS_METHODS).join('.')
return if offset_provided?(node)
message = build_message(klass, method_name, node)
add_offense(node, location: :selector, message: message)
end
def build_message(klass, method_name, node)
if flexible?
format(
MSG_ACCEPTABLE,
current: "#{klass}.#{method_name}",
prefer: acceptable_methods(klass, method_name, node).join(', ')
)
else
safe_method_name = safe_method(method_name, node)
format(MSG,
current: "#{klass}.#{method_name}",
prefer: "Time.zone.#{safe_method_name}")
end
end
def extract_method_chain(node)
chain = []
while !node.nil? && node.send_type?
chain << node.method_name if method_from_time_class?(node)
node = node.parent
end
chain
end
# Only add the method to the chain if the method being
# called is part of the time class.
def method_from_time_class?(node)
receiver, method_name, *_args = *node
if (receiver.is_a? RuboCop::AST::Node) && !receiver.cbase_type?
method_from_time_class?(receiver)
else
method_name == :Time
end
end
# checks that parent node of send_type
# and receiver is the given node
def method_send?(node)
return false unless node.parent&.send_type?
node.parent.receiver == node
end
def safe_method(method_name, node)
if %w[new current].include?(method_name)
node.arguments? ? 'local' : 'now'
else
method_name
end
end
def check_localtime(node)
selector_node = node
while node&.send_type?
break if node.method?(:localtime)
node = node.parent
end
return if node.arguments?
add_offense(selector_node,
location: :selector, message: MSG_LOCALTIME)
end
def not_danger_chain?(chain)
(chain & DANGEROUS_METHODS).empty? || !(chain & good_methods).empty?
end
def need_check_localtime?(chain)
flexible? && chain.include?(:localtime)
end
def flexible?
style == :flexible
end
def strict?
style == :strict
end
def good_methods
if strict?
GOOD_METHODS
else
GOOD_METHODS + [:current] + ACCEPTED_METHODS
end
end
def acceptable_methods(klass, method_name, node)
acceptable = [
"`Time.zone.#{safe_method(method_name, node)}`",
"`#{klass}.current`"
]
ACCEPTED_METHODS.each do |am|
acceptable << "`#{klass}.#{method_name}.#{am}`"
end
acceptable
end
# Time.new can be called with a time zone offset
# When it is, that should be considered safe
# Example:
# Time.new(1988, 3, 15, 3, 0, 0, "-05:00")
def offset_provided?(node)
node.arguments.size >= 7
end
end
end
end
end