Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix #848] Fix a false positive for Rails/FreezeTime #851

Merged
merged 1 commit into from
Nov 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/fix_false_positive_for_rails_freeze_time.md
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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