diff --git a/CHANGELOG.md b/CHANGELOG.md index ea46bf39db15..91c6c5cb9c7a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ ### Bug fixes +* [#6500](https://github.com/rubocop-hq/rubocop/issues/6500): Add offense to use `in_time_zone` instead of deprecated `to_time_in_current_zone`. ([@nadiyaka][]) * [#6554](https://github.com/rubocop-hq/rubocop/issues/6554): Prevent Layout/RescueEnsureAlignment cop from breaking on block assignment when assignment is on a separate line. ([@timmcanty][]) * [#6343](https://github.com/rubocop-hq/rubocop/pull/6343): Optimise `--auto-gen-config` when `Metrics/LineLength` cop is disabled. ([@tom-lord][]) * [#6389](https://github.com/rubocop-hq/rubocop/pull/6389): Fix false negative for `Style/TrailingCommaInHashLitera`/`Style/TrailingCommaInArrayLiteral` when there is a comment in the last line. ([@bayandin][]) @@ -3691,3 +3692,4 @@ [@timmcanty]: https://github.com/timmcanty [@tom-lord]: https://github.com/tom-lord [@bayandin]: https://github.com/bayandin +[@nadiyaka]: https://github.com/nadiyaka diff --git a/lib/rubocop/cop/rails/date.rb b/lib/rubocop/cop/rails/date.rb index e07ae7183cb4..c51d81c05ca7 100644 --- a/lib/rubocop/cop/rails/date.rb +++ b/lib/rubocop/cop/rails/date.rb @@ -26,7 +26,6 @@ module Rails # Date.yesterday # Date.today # date.to_time - # date.to_time_in_current_zone # # # good # Time.zone.today @@ -42,7 +41,7 @@ module Rails # Time.zone.today - 1.day # Date.current # Date.yesterday - # date.to_time_in_current_zone + # date.in_time_zone # class Date < Cop include ConfigurableEnforcedStyle @@ -55,6 +54,13 @@ class Date < Cop BAD_DAYS = %i[today current yesterday tomorrow].freeze + DEPRECATED_METHODS = [ + { deprecated: 'to_time_in_current_zone', relevant: 'in_time_zone' } + ].freeze + + DEPRECATED_MSG = '`%s` is deprecated. ' \ + 'Use `%s` instead.'.freeze + def on_const(node) mod, klass = *node.children # we should only check core Date class (`Date` or `::Date`) @@ -68,12 +74,25 @@ def on_send(node) return if safe_chain?(node) || safe_to_time?(node) + check_deprecated_methods(node) + add_offense(node, location: :selector, message: format(MSG_SEND, method: node.method_name)) end private + def check_deprecated_methods(node) + DEPRECATED_METHODS.each do |relevant:, deprecated:| + next unless node.method_name == deprecated.to_sym + + add_offense(node, location: :selector, + message: format(DEPRECATED_MSG, + deprecated: deprecated, + relevant: relevant)) + end + end + def check_date_node(node) chain = extract_method_chain(node) @@ -124,7 +143,7 @@ def bad_days end def bad_methods - style == :strict ? %i[to_time to_time_in_current_zone] : [:to_time] + %i[to_time to_time_in_current_zone] end def good_methods diff --git a/manual/cops_rails.md b/manual/cops_rails.md index 2b173969a801..c19f9bd6def9 100644 --- a/manual/cops_rails.md +++ b/manual/cops_rails.md @@ -388,7 +388,6 @@ Date.current Date.yesterday Date.today date.to_time -date.to_time_in_current_zone # good Time.zone.today @@ -406,7 +405,7 @@ Time.zone.today Time.zone.today - 1.day Date.current Date.yesterday -date.to_time_in_current_zone +date.in_time_zone ``` ### Configurable attributes diff --git a/spec/rubocop/cop/rails/date_spec.rb b/spec/rubocop/cop/rails/date_spec.rb index 1f31d54cbdf6..117668f77561 100644 --- a/spec/rubocop/cop/rails/date_spec.rb +++ b/spec/rubocop/cop/rails/date_spec.rb @@ -74,6 +74,13 @@ expect(cop.offenses.size).to eq(1) end end + + it 'registers an offense for #to_time_in_current_zone' do + expect_offense(<<-RUBY.strip_indent) + "2016-07-12 14:36:31".to_time_in_current_zone + ^^^^^^^^^^^^^^^^^^^^^^^ `to_time_in_current_zone` is deprecated. Use `in_time_zone` instead. + RUBY + end end context 'when EnforcedStyle is "flexible"' do @@ -98,8 +105,11 @@ end end - it 'accepts #to_time_in_current_zone' do - expect_no_offenses('date.to_time_in_current_zone') + it 'registers an offense for #to_time_in_current_zone' do + expect_offense(<<-RUBY.strip_indent) + "2016-07-12 14:36:31".to_time_in_current_zone + ^^^^^^^^^^^^^^^^^^^^^^^ `to_time_in_current_zone` is deprecated. Use `in_time_zone` instead. + RUBY end end end