Skip to content

Commit b270514

Browse files
committed
[Fix rubocop#764] Fix an incorrect autocorrect for Rails/FreezeTime
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.
1 parent 2f68e9f commit b270514

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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][])

lib/rubocop/cop/rails/freeze_time.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ def on_send(node)
4646
child_node, method_name = *node.first_argument.children
4747
return unless current_time?(child_node, method_name) || current_time_with_convert?(child_node, method_name)
4848

49-
add_offense(node) { |corrector| corrector.replace(node, 'freeze_time') }
49+
add_offense(node) do |corrector|
50+
last_argument = node.last_argument
51+
freeze_time_method = last_argument.block_pass_type? ? "freeze_time(#{last_argument.source})" : 'freeze_time'
52+
corrector.replace(node, freeze_time_method)
53+
end
5054
end
5155

5256
private

spec/rubocop/cop/rails/freeze_time_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,21 @@
5656
RUBY
5757
end
5858

59+
it 'registers an offense when using `travel_to` with an argument of the current time and proc argument' do
60+
expect_offense(<<~RUBY)
61+
around do |example|
62+
travel_to(Time.current, &example)
63+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `freeze_time` instead of `travel_to`.
64+
end
65+
RUBY
66+
67+
expect_correction(<<~RUBY)
68+
around do |example|
69+
freeze_time(&example)
70+
end
71+
RUBY
72+
end
73+
5974
it 'does not register an offense when using `freeze_time`' do
6075
expect_no_offenses(<<~RUBY)
6176
freeze_time

0 commit comments

Comments
 (0)