Skip to content

Commit 783e2ed

Browse files
authored
Merge pull request rubocop#766 from koic/fix_an_incorrect_autocorrect_for_rails_freeze_time
[Fix rubocop#764] Fix an incorrect autocorrect for `Rails/FreezeTime`
2 parents d42f944 + b270514 commit 783e2ed

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
@@ -47,7 +47,11 @@ def on_send(node)
4747
return unless child_node
4848
return unless current_time?(child_node, method_name) || current_time_with_convert?(child_node, method_name)
4949

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

5357
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)