Skip to content

Commit

Permalink
[Fix rubocop#6500] Add offense to use in_time_zone instead of depreca…
Browse files Browse the repository at this point in the history
…ted to_time_in_current_zone
  • Loading branch information
nadiyaka authored and bbatsov committed Dec 14, 2018
1 parent ab49526 commit ef95bfd
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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][])
Expand Down Expand Up @@ -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
25 changes: 22 additions & 3 deletions lib/rubocop/cop/rails/date.rb
Expand Up @@ -26,7 +26,6 @@ module Rails
# Date.yesterday
# Date.today
# date.to_time
# date.to_time_in_current_zone
#
# # good
# Time.zone.today
Expand All @@ -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
Expand All @@ -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 = '`%<deprecated>s` is deprecated. ' \
'Use `%<relevant>s` instead.'.freeze

def on_const(node)
mod, klass = *node.children
# we should only check core Date class (`Date` or `::Date`)
Expand All @@ -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)

Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions manual/cops_rails.md
Expand Up @@ -388,7 +388,6 @@ Date.current
Date.yesterday
Date.today
date.to_time
date.to_time_in_current_zone

# good
Time.zone.today
Expand All @@ -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
Expand Down
14 changes: 12 additions & 2 deletions spec/rubocop/cop/rails/date_spec.rb
Expand Up @@ -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
Expand All @@ -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

0 comments on commit ef95bfd

Please sign in to comment.