From eef962596c7553b49202014a14335734ab337f3f Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Thu, 13 May 2021 07:14:03 +0900 Subject: [PATCH] [Fix #70] Fix a false positive for `Rails/TimeZone` Fixes #70. This PR fixes a false positive for `Rails/TimeZone` when setting `EnforcedStyle: strict` and using `Time.current`. --- CHANGELOG.md | 1 + docs/modules/ROOT/pages/cops_rails.adoc | 32 ++++++++++-------------- lib/rubocop/cop/rails/time_zone.rb | 30 ++++++++-------------- spec/rubocop/cop/rails/time_zone_spec.rb | 26 +++++-------------- 4 files changed, 31 insertions(+), 58 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7e00bd03ab..94e43feed5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ * [#482](https://github.com/rubocop/rubocop-rails/pull/482): Fix a false positive for `Rails/RelativeDateConstant` when assigning (hashes/arrays/etc)-containing procs to a constant. ([@jdelStrother][]) * [#419](https://github.com/rubocop/rubocop-rails/issues/419): Fix an error for `Rails/UniqueValidationWithoutIndex` when using a unique index and `check_constraint` that has `nil` first argument. ([@koic][]) +* [#70](https://github.com/rubocop/rubocop-rails/issues/70): Fix a false positive for `Rails/TimeZone` when setting `EnforcedStyle: strict` and using `Time.current`. ([@koic][]) ### Changes diff --git a/docs/modules/ROOT/pages/cops_rails.adoc b/docs/modules/ROOT/pages/cops_rails.adoc index 92fb801408..18b1b00653 100644 --- a/docs/modules/ROOT/pages/cops_rails.adoc +++ b/docs/modules/ROOT/pages/cops_rails.adoc @@ -4366,50 +4366,44 @@ This cop checks for the use of Time methods without zone. Built on top of Ruby on Rails style guide (https://rails.rubystyle.guide#time) and the article http://danilenko.org/2012/7/6/rails_timezones/ -Two styles are supported for this cop. When EnforcedStyle is 'strict' -then only use of Time.zone is allowed. +Two styles are supported for this cop. When `EnforcedStyle` is 'strict' +then only use of `Time.zone` is allowed. When EnforcedStyle is 'flexible' then it's also allowed -to use Time.in_time_zone. +to use `Time#in_time_zone`. === Examples -==== EnforcedStyle: strict - [source,ruby] ---- -# `strict` means that `Time` should be used with `zone`. - # bad Time.now Time.parse('2015-03-02T19:05:37') -# bad -Time.current -Time.at(timestamp).in_time_zone - # good +Time.current Time.zone.now Time.zone.parse('2015-03-02T19:05:37') Time.zone.parse('2015-03-02T19:05:37Z') # Respect ISO 8601 format with timezone specifier. ---- -==== EnforcedStyle: flexible (default) +==== EnforcedStyle: strict [source,ruby] ---- -# `flexible` allows usage of `in_time_zone` instead of `zone`. +# `strict` means that `Time` should be used with `zone`. # bad -Time.now -Time.parse('2015-03-02T19:05:37') +Time.at(timestamp).in_time_zone +---- -# good -Time.zone.now -Time.zone.parse('2015-03-02T19:05:37') +==== EnforcedStyle: flexible (default) + +[source,ruby] +---- +# `flexible` allows usage of `in_time_zone` instead of `zone`. # good -Time.current Time.at(timestamp).in_time_zone ---- diff --git a/lib/rubocop/cop/rails/time_zone.rb b/lib/rubocop/cop/rails/time_zone.rb index 4182aa562d..90ef9d5ff9 100644 --- a/lib/rubocop/cop/rails/time_zone.rb +++ b/lib/rubocop/cop/rails/time_zone.rb @@ -8,41 +8,33 @@ module Rails # Built on top of Ruby on Rails style guide (https://rails.rubystyle.guide#time) # and the article http://danilenko.org/2012/7/6/rails_timezones/ # - # Two styles are supported for this cop. When EnforcedStyle is 'strict' - # then only use of Time.zone is allowed. + # Two styles are supported for this cop. When `EnforcedStyle` is 'strict' + # then only use of `Time.zone` is allowed. # # When EnforcedStyle is 'flexible' then it's also allowed - # to use Time.in_time_zone. - # - # @example EnforcedStyle: strict - # # `strict` means that `Time` should be used with `zone`. + # to use `Time#in_time_zone`. # + # @example # # bad # Time.now # Time.parse('2015-03-02T19:05:37') # - # # bad - # Time.current - # Time.at(timestamp).in_time_zone - # # # good + # Time.current # Time.zone.now # Time.zone.parse('2015-03-02T19:05:37') # Time.zone.parse('2015-03-02T19:05:37Z') # Respect ISO 8601 format with timezone specifier. # - # @example EnforcedStyle: flexible (default) - # # `flexible` allows usage of `in_time_zone` instead of `zone`. + # @example EnforcedStyle: strict + # # `strict` means that `Time` should be used with `zone`. # # # bad - # Time.now - # Time.parse('2015-03-02T19:05:37') + # Time.at(timestamp).in_time_zone # - # # good - # Time.zone.now - # Time.zone.parse('2015-03-02T19:05:37') + # @example EnforcedStyle: flexible (default) + # # `flexible` allows usage of `in_time_zone` instead of `zone`. # # # good - # Time.current # Time.at(timestamp).in_time_zone class TimeZone < Base include ConfigurableEnforcedStyle @@ -59,7 +51,7 @@ class TimeZone < Base GOOD_METHODS = %i[zone zone_default find_zone find_zone!].freeze - DANGEROUS_METHODS = %i[now local new parse at current].freeze + DANGEROUS_METHODS = %i[now local new parse at].freeze ACCEPTED_METHODS = %i[in_time_zone utc getlocal xmlschema iso8601 jisx0301 rfc3339 httpdate to_i to_f].freeze diff --git a/spec/rubocop/cop/rails/time_zone_spec.rb b/spec/rubocop/cop/rails/time_zone_spec.rb index 3d41e7ed32..4b1d642b79 100644 --- a/spec/rubocop/cop/rails/time_zone_spec.rb +++ b/spec/rubocop/cop/rails/time_zone_spec.rb @@ -11,13 +11,6 @@ RUBY end - it 'registers an offense for Time.current' do - expect_offense(<<~RUBY) - Time.current - ^^^^^^^ Do not use `Time.current` without zone. Use `Time.zone.now` instead. - RUBY - end - it 'registers an offense for Time.new without argument' do expect_offense(<<~RUBY) Time.new @@ -104,19 +97,6 @@ RUBY end end - - describe '.current' do - it 'corrects the error' do - expect_offense(<<~RUBY) - Time.current - ^^^^^^^ Do not use `Time.current` without zone. Use `Time.zone.now` instead. - RUBY - - expect_correction(<<~RUBY) - Time.zone.now - RUBY - end - end end it 'registers an offense for Time.parse' do @@ -179,6 +159,12 @@ RUBY end + it 'accepts Time.current' do + expect_no_offenses(<<~RUBY) + Time.current + RUBY + end + it 'accepts Time.zone.now' do expect_no_offenses('Time.zone.now') end