Skip to content

Commit 153c881

Browse files
committed
[Fix rubocop#848] Fix a false positive for Rails/FreezeTime
Fixes rubocop#848. This PR fixes a false positive for `Rails/FreezeTime` when using `travel_to` with an argument of `Time.new(...).in_time_zone`.
1 parent 0c27719 commit 153c881

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#848](https://github.com/rubocop/rubocop-rails/issues/848): Fix a false positive for `Rails/FreezeTime` when using `travel_to` with an argument of `Time.new(...).in_time_zone`. ([@koic][])

lib/rubocop/cop/rails/freeze_time.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class FreezeTime < Base
2929

3030
MSG = 'Use `freeze_time` instead of `travel_to`.'
3131
NOW_METHODS = %i[now new current].freeze
32-
CONV_METHODS = %i[to_time in_time_zone].freeze
32+
CONVERT_METHODS = %i[to_time in_time_zone].freeze
3333
RESTRICT_ON_SEND = %i[travel_to].freeze
3434

3535
# @!method time_now?(node)
@@ -63,9 +63,11 @@ def current_time?(node, method_name)
6363
end
6464

6565
def current_time_with_convert?(node, method_name)
66-
return false unless CONV_METHODS.include?(method_name)
66+
return false unless CONVERT_METHODS.include?(method_name)
67+
68+
child_node, child_method_name, time_argument = *node.children
69+
return if time_argument
6770

68-
child_node, child_method_name = *node.children
6971
current_time?(child_node, child_method_name)
7072
end
7173
end

spec/rubocop/cop/rails/freeze_time_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@
9898
RUBY
9999
end
100100

101+
it 'does not register an offense when using `travel_to` with an argument of `Time.new(...).in_time_zone`' do
102+
expect_no_offenses(<<~RUBY)
103+
travel_to(Time.new(2019, 4, 3, 12, 30).in_time_zone)
104+
RUBY
105+
end
106+
101107
it 'does not register an offense when using `travel_to` without argument' do
102108
expect_no_offenses(<<~RUBY)
103109
travel_to

0 commit comments

Comments
 (0)