Skip to content

Commit

Permalink
[Fix rubocop#848] Fix a false positive for Rails/FreezeTime
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
koic committed Nov 1, 2022
1 parent 0c27719 commit 153c881
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
1 change: 1 addition & 0 deletions changelog/fix_false_positive_for_rails_freeze_time.md
@@ -0,0 +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][])
8 changes: 5 additions & 3 deletions lib/rubocop/cop/rails/freeze_time.rb
Expand Up @@ -29,7 +29,7 @@ class FreezeTime < Base

MSG = 'Use `freeze_time` instead of `travel_to`.'
NOW_METHODS = %i[now new current].freeze
CONV_METHODS = %i[to_time in_time_zone].freeze
CONVERT_METHODS = %i[to_time in_time_zone].freeze
RESTRICT_ON_SEND = %i[travel_to].freeze

# @!method time_now?(node)
Expand Down Expand Up @@ -63,9 +63,11 @@ def current_time?(node, method_name)
end

def current_time_with_convert?(node, method_name)
return false unless CONV_METHODS.include?(method_name)
return false unless CONVERT_METHODS.include?(method_name)

child_node, child_method_name, time_argument = *node.children
return if time_argument

child_node, child_method_name = *node.children
current_time?(child_node, child_method_name)
end
end
Expand Down
6 changes: 6 additions & 0 deletions spec/rubocop/cop/rails/freeze_time_spec.rb
Expand Up @@ -98,6 +98,12 @@
RUBY
end

it 'does not register an offense when using `travel_to` with an argument of `Time.new(...).in_time_zone`' do
expect_no_offenses(<<~RUBY)
travel_to(Time.new(2019, 4, 3, 12, 30).in_time_zone)
RUBY
end

it 'does not register an offense when using `travel_to` without argument' do
expect_no_offenses(<<~RUBY)
travel_to
Expand Down

0 comments on commit 153c881

Please sign in to comment.