From 614558ba9617dff45ab6f918dc4e9f9b58c33589 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Mon, 12 Sep 2022 00:46:31 +0900 Subject: [PATCH] [Fix #769] Fix a false positive for `Rails/FreezeTime` Fixes #769. This PR fixes a false positive for `Rails/FreezeTime` when using `travel_to` with an argument of `DateTime.new` with arguments. --- changelog/fix_a_false_positive_for_rails_freeze_time.md | 1 + lib/rubocop/cop/rails/freeze_time.rb | 4 ++-- spec/rubocop/cop/rails/freeze_time_spec.rb | 6 ++++++ 3 files changed, 9 insertions(+), 2 deletions(-) create mode 100644 changelog/fix_a_false_positive_for_rails_freeze_time.md diff --git a/changelog/fix_a_false_positive_for_rails_freeze_time.md b/changelog/fix_a_false_positive_for_rails_freeze_time.md new file mode 100644 index 0000000000..f742efec43 --- /dev/null +++ b/changelog/fix_a_false_positive_for_rails_freeze_time.md @@ -0,0 +1 @@ +* [#769](https://github.com/rubocop/rubocop-rails/issues/769): Fix a false positive for `Rails/FreezeTime` when using `travel_to` with an argument of `DateTime.new` with arguments. ([@koic][]) diff --git a/lib/rubocop/cop/rails/freeze_time.rb b/lib/rubocop/cop/rails/freeze_time.rb index 3a28b0f8b1..582219cf54 100644 --- a/lib/rubocop/cop/rails/freeze_time.rb +++ b/lib/rubocop/cop/rails/freeze_time.rb @@ -43,8 +43,8 @@ class FreezeTime < Base PATTERN def on_send(node) - child_node, method_name = *node.first_argument.children - return unless child_node + child_node, method_name, time_argument = *node.first_argument.children + return if time_argument || !child_node return unless current_time?(child_node, method_name) || current_time_with_convert?(child_node, method_name) add_offense(node) do |corrector| diff --git a/spec/rubocop/cop/rails/freeze_time_spec.rb b/spec/rubocop/cop/rails/freeze_time_spec.rb index 5c791d8456..99c75d768f 100644 --- a/spec/rubocop/cop/rails/freeze_time_spec.rb +++ b/spec/rubocop/cop/rails/freeze_time_spec.rb @@ -91,4 +91,10 @@ travel_to(current) RUBY end + + it 'does not register an offense when using `travel_to` with an argument of `DateTime.new` with arguments' do + expect_no_offenses(<<~RUBY) + travel_to(DateTime.new(2022, 5, 3, 12, 0, 0)) + RUBY + end end