Skip to content

Commit

Permalink
Merge pull request #766 from koic/fix_an_incorrect_autocorrect_for_ra…
Browse files Browse the repository at this point in the history
…ils_freeze_time

[Fix #764] Fix an incorrect autocorrect for `Rails/FreezeTime`
  • Loading branch information
koic committed Sep 11, 2022
2 parents d42f944 + b270514 commit 783e2ed
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 @@ -47,7 +47,11 @@ def on_send(node)
return unless child_node
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 783e2ed

Please sign in to comment.