Class: RuboCop::Cop::Rails::FreezeTime
- Inherits:
-
Base
- Object
- Base
- RuboCop::Cop::Rails::FreezeTime
show all
- Extended by:
- AutoCorrector, TargetRailsVersion
- Defined in:
- lib/rubocop/cop/rails/freeze_time.rb
Overview
Identifies usages of ‘travel_to` with an argument of the current time and change them to use `freeze_time` instead.
Constant Summary
collapse
- MSG =
'Use `freeze_time` instead of `travel_to`.'
- NOW_METHODS =
%i[now new current].freeze
- CONVERT_METHODS =
%i[to_time in_time_zone].freeze
- RESTRICT_ON_SEND =
%i[travel_to].freeze
Instance Method Summary
collapse
minimum_target_rails_version, support_target_rails_version?
Instance Method Details
#on_send(node) ⇒ Object
48
49
50
51
52
53
54
55
56
57
58
|
# File 'lib/rubocop/cop/rails/freeze_time.rb', line 48
def on_send(node)
child_node, method_name, time_argument = *node.first_argument&.children
return if time_argument || !child_node
return unless current_time?(child_node, method_name) || current_time_with_convert?(child_node, method_name)
add_offense(node) do |corrector|
last_argument = node.last_argument
freeze_time_method = last_argument.block_pass_type? ? "freeze_time(#{last_argument.source})" : 'freeze_time'
corrector.replace(node, freeze_time_method)
end
end
|
#time_now?(node) ⇒ Object
39
40
41
|
# File 'lib/rubocop/cop/rails/freeze_time.rb', line 39
def_node_matcher :time_now?, <<~PATTERN
(const {nil? cbase} {:Time :DateTime})
PATTERN
|
#zoned_time_now?(node) ⇒ Object
44
45
46
|
# File 'lib/rubocop/cop/rails/freeze_time.rb', line 44
def_node_matcher :zoned_time_now?, <<~PATTERN
(send (const {nil? cbase} :Time) :zone)
PATTERN
|