Skip to content

Commit

Permalink
[Fix rubocop#764] Fix an incorrect autocorrect for Rails/FreezeTime
Browse files Browse the repository at this point in the history
Fixes rubocop#764.

This PR fixes an incorrect autocorrect for `Rails/FreezeTime` when
using `travel_to` with an argument of the current time and proc argument.
  • Loading branch information
koic committed Sep 9, 2022
1 parent 2f68e9f commit b270514
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
@@ -0,0 +1 @@
* [#764](https://github.com/rubocop/rubocop-rails/issues/764): Fix an incorrect autocorrect for `Rails/FreezeTime` when using `travel_to` with an argument of the current time and proc argument. ([@koic][])
6 changes: 5 additions & 1 deletion lib/rubocop/cop/rails/freeze_time.rb
Expand Up @@ -46,7 +46,11 @@ def on_send(node)
child_node, method_name = *node.first_argument.children
return unless current_time?(child_node, method_name) || current_time_with_convert?(child_node, method_name)

add_offense(node) { |corrector| corrector.replace(node, 'freeze_time') }
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

private
Expand Down
15 changes: 15 additions & 0 deletions spec/rubocop/cop/rails/freeze_time_spec.rb
Expand Up @@ -56,6 +56,21 @@
RUBY
end

it 'registers an offense when using `travel_to` with an argument of the current time and proc argument' do
expect_offense(<<~RUBY)
around do |example|
travel_to(Time.current, &example)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `freeze_time` instead of `travel_to`.
end
RUBY

expect_correction(<<~RUBY)
around do |example|
freeze_time(&example)
end
RUBY
end

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

0 comments on commit b270514

Please sign in to comment.