From fb275119de364c60a800d018916ca048f29870b6 Mon Sep 17 00:00:00 2001 From: Daniel Vandersluis Date: Fri, 25 Sep 2020 02:40:15 -0400 Subject: [PATCH] Enable autocorrection for `Style/DateTime` (#8738) Co-authored-by: Bozhidar Batsov --- CHANGELOG.md | 1 + config/default.yml | 3 ++- docs/modules/ROOT/pages/cops_style.adoc | 4 ++-- lib/rubocop/cop/style/date_time.rb | 13 ++++++++++++- spec/rubocop/cop/style/date_time_spec.rb | 12 ++++++++++++ 5 files changed, 29 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0f04e919371..a53fc1330a4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### New features * [#8790](https://github.com/rubocop-hq/rubocop/pull/8790): Add `AllowedMethods` option to `Style/OptionalBooleanParameter` cop. ([@fatkodima][]) +* [#8738](https://github.com/rubocop-hq/rubocop/issues/8738): Add autocorrection to `Style/DateTime`. ([@dvandersluis][]) ### Bug fixes diff --git a/config/default.yml b/config/default.yml index 5f3adaee625..0d7b46acc3a 100644 --- a/config/default.yml +++ b/config/default.yml @@ -2863,7 +2863,8 @@ Style/DateTime: StyleGuide: '#date--time' Enabled: false VersionAdded: '0.51' - VersionChanged: '0.59' + VersionChanged: '0.92' + SafeAutoCorrect: false AllowCoercion: false Style/DefWithParentheses: diff --git a/docs/modules/ROOT/pages/cops_style.adoc b/docs/modules/ROOT/pages/cops_style.adoc index a668eaf9d10..e1ea36e09d0 100644 --- a/docs/modules/ROOT/pages/cops_style.adoc +++ b/docs/modules/ROOT/pages/cops_style.adoc @@ -1879,9 +1879,9 @@ an offense is reported. | Disabled | Yes -| No +| Yes (Unsafe) | 0.51 -| 0.59 +| 0.92 |=== This cop checks for consistent usage of the `DateTime` class over the diff --git a/lib/rubocop/cop/style/date_time.rb b/lib/rubocop/cop/style/date_time.rb index a3ff50cfa54..d7b9a6c0097 100644 --- a/lib/rubocop/cop/style/date_time.rb +++ b/lib/rubocop/cop/style/date_time.rb @@ -42,6 +42,8 @@ module Style # # good # something.to_time class DateTime < Base + extend AutoCorrector + CLASS_MSG = 'Prefer Time over DateTime.' COERCION_MSG = 'Do not use #to_datetime.' @@ -63,7 +65,10 @@ def on_send(node) return if historic_date?(node) message = to_datetime?(node) ? COERCION_MSG : CLASS_MSG - add_offense(node, message: message) + + add_offense(node, message: message) do |corrector| + autocorrect(corrector, node) + end end private @@ -71,6 +76,12 @@ def on_send(node) def disallow_coercion? !cop_config['AllowCoercion'] end + + def autocorrect(corrector, node) + return if to_datetime?(node) + + corrector.replace(node.receiver.loc.name, 'Time') + end end end end diff --git a/spec/rubocop/cop/style/date_time_spec.rb b/spec/rubocop/cop/style/date_time_spec.rb index 02249c8f679..85dc2893eac 100644 --- a/spec/rubocop/cop/style/date_time_spec.rb +++ b/spec/rubocop/cop/style/date_time_spec.rb @@ -8,6 +8,10 @@ DateTime.now ^^^^^^^^^^^^ Prefer Time over DateTime. RUBY + + expect_correction(<<~RUBY) + Time.now + RUBY end it 'registers an offense when using ::DateTime for current time' do @@ -15,6 +19,10 @@ ::DateTime.now ^^^^^^^^^^^^^^ Prefer Time over DateTime. RUBY + + expect_correction(<<~RUBY) + ::Time.now + RUBY end it 'registers an offense when using DateTime for modern date' do @@ -22,6 +30,10 @@ DateTime.iso8601('2016-06-29') ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer Time over DateTime. RUBY + + expect_correction(<<~RUBY) + Time.iso8601('2016-06-29') + RUBY end it 'does not register an offense when using Time for current time' do