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 #764] Fix an incorrect autocorrect for Rails/FreezeTime #766

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@koic what about block type?

travel_to(Time.current) do 
  do_something
end

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
Original file line number Diff line number Diff line change
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