From 4b2748c3eb780be03aea2679098d38918e835dc7 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Fri, 25 Sep 2020 01:40:56 +0900 Subject: [PATCH 01/10] [Fix #8165] Require Parser gem 2.7.1.5 Parser 2.7.1.5 includes https://github.com/whitequark/parser/pull/717 and fixes #8165. --- rubocop.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rubocop.gemspec b/rubocop.gemspec index e8abd630eaa9..52a485506b13 100644 --- a/rubocop.gemspec +++ b/rubocop.gemspec @@ -34,7 +34,7 @@ Gem::Specification.new do |s| } s.add_runtime_dependency('parallel', '~> 1.10') - s.add_runtime_dependency('parser', '>= 2.7.1.1') + s.add_runtime_dependency('parser', '>= 2.7.1.5') s.add_runtime_dependency('rainbow', '>= 2.2.2', '< 4.0') s.add_runtime_dependency('regexp_parser', '>= 1.7') s.add_runtime_dependency('rexml') From 69370a81496f775ad785b6887ee85708b9a1a47c Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Fri, 25 Sep 2020 01:58:56 +0900 Subject: [PATCH 02/10] Update Ruby 2.8 to Ruby 3.0 Follow https://github.com/whitequark/parser/pull/729. --- CHANGELOG.md | 4 ++++ docs/modules/ROOT/pages/compatibility.adoc | 2 +- lib/rubocop/target_ruby.rb | 2 +- rubocop.gemspec | 2 +- spec/rubocop/cli_spec.rb | 6 +++--- 5 files changed, 10 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 357dcc8674f6..9f995caf35fb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ * [#8774](https://github.com/rubocop-hq/rubocop/issues/8774): Fix a false positive for `Layout/ArrayAlignment` with parallel assignment. ([@dvandersluis][]) +### Changes + +* [#8785](https://github.com/rubocop-hq/rubocop/pull/8785): Update TargetRubyVersion 2.8 to 3.0 (experimental). ([@em-gazelle][]) + ## 0.91.1 (2020-09-23) ### Bug fixes diff --git a/docs/modules/ROOT/pages/compatibility.adoc b/docs/modules/ROOT/pages/compatibility.adoc index 4aeb7c66af1d..ea19baf0aa99 100644 --- a/docs/modules/ROOT/pages/compatibility.adoc +++ b/docs/modules/ROOT/pages/compatibility.adoc @@ -30,7 +30,7 @@ The following table is the support matrix. | 2.5 | - | 2.6 | - | 2.7 | - -| 2.8 (experimental) | - +| 3.0 (experimental) | - |=== NOTE: The compatibility xref:configuration.adoc#setting-the-target-ruby-version[target Ruby version mentioned here] is about code analysis (what RuboCop can analyze), not runtime (is RuboCop capable of running on some Ruby or not). diff --git a/lib/rubocop/target_ruby.rb b/lib/rubocop/target_ruby.rb index d9abb5b2e010..c1b02520d55b 100644 --- a/lib/rubocop/target_ruby.rb +++ b/lib/rubocop/target_ruby.rb @@ -4,7 +4,7 @@ module RuboCop # The kind of Ruby that code inspected by RuboCop is written in. # @api private class TargetRuby - KNOWN_RUBIES = [2.4, 2.5, 2.6, 2.7, 2.8].freeze + KNOWN_RUBIES = [2.4, 2.5, 2.6, 2.7, 3.0].freeze DEFAULT_VERSION = KNOWN_RUBIES.first OBSOLETE_RUBIES = { diff --git a/rubocop.gemspec b/rubocop.gemspec index 52a485506b13..693b034133aa 100644 --- a/rubocop.gemspec +++ b/rubocop.gemspec @@ -38,7 +38,7 @@ Gem::Specification.new do |s| s.add_runtime_dependency('rainbow', '>= 2.2.2', '< 4.0') s.add_runtime_dependency('regexp_parser', '>= 1.7') s.add_runtime_dependency('rexml') - s.add_runtime_dependency('rubocop-ast', '>= 0.4.0', '< 1.0') + s.add_runtime_dependency('rubocop-ast', '>= 0.5.0', '< 1.0') s.add_runtime_dependency('ruby-progressbar', '~> 1.7') s.add_runtime_dependency('unicode-display_width', '>= 1.4.0', '< 2.0') diff --git a/spec/rubocop/cli_spec.rb b/spec/rubocop/cli_spec.rb index 0ba9673d46c8..baee491a030b 100644 --- a/spec/rubocop/cli_spec.rb +++ b/spec/rubocop/cli_spec.rb @@ -1634,14 +1634,14 @@ def method(foo, bar, qux, fred, arg5, f) end #{'#' * 85} it 'fails with an error message' do create_file('.rubocop.yml', <<~YAML) AllCops: - TargetRubyVersion: 3.0 + TargetRubyVersion: 4.0 YAML expect(cli.run([])).to eq(2) expect($stderr.string.strip).to start_with( - 'Error: RuboCop found unknown Ruby version 3.0 in `TargetRubyVersion`' + 'Error: RuboCop found unknown Ruby version 4.0 in `TargetRubyVersion`' ) expect($stderr.string.strip).to match( - /Supported versions: 2.4, 2.5, 2.6, 2.7, 2.8/ + /Supported versions: 2.4, 2.5, 2.6, 2.7, 3.0/ ) end end From b1e1829d7ae2ecac674b75552a5e9c774eee5e0a Mon Sep 17 00:00:00 2001 From: Marc-Andre Lafortune Date: Thu, 24 Sep 2020 14:56:23 -0400 Subject: [PATCH 03/10] Simplify `Style/MixinUsage` and don't depend on broken `Node#macro?` Bump `rubocop/ast` requirement. See https://github.com/rubocop-hq/rubocop/issues/8765 --- lib/rubocop/cop/style/mixin_usage.rb | 34 ++++++---------------------- 1 file changed, 7 insertions(+), 27 deletions(-) diff --git a/lib/rubocop/cop/style/mixin_usage.rb b/lib/rubocop/cop/style/mixin_usage.rb index f20e28211be9..dc07ca9d01e6 100644 --- a/lib/rubocop/cop/style/mixin_usage.rb +++ b/lib/rubocop/cop/style/mixin_usage.rb @@ -50,41 +50,21 @@ class MixinUsage < Base const) PATTERN - def_node_matcher :wrapped_macro_scope?, <<~PATTERN - {({sclass class module block} ... ({begin if} ...))} + def_node_matcher :in_top_level_scope?, <<~PATTERN + { + root? # either at the top level + ^[ {kwbegin begin if def} # or wrapped within one of these + #in_top_level_scope? ] # that is in top level scope + } PATTERN def on_send(node) include_statement(node) do |statement| - return if node.argument? || - accepted_include?(node) || - belongs_to_class_or_module?(node) + return unless in_top_level_scope?(node) add_offense(node, message: format(MSG, statement: statement)) end end - - private - - def accepted_include?(node) - node.parent && (node.macro? || ascend_macro_scope?(node.parent)) - end - - def ascend_macro_scope?(ancestor) - return true if wrapped_macro_scope?(ancestor) - - ancestor.parent && ascend_macro_scope?(ancestor.parent) - end - - def belongs_to_class_or_module?(node) - if !node.parent - false - else - return true if node.parent.class_type? || node.parent.module_type? - - belongs_to_class_or_module?(node.parent) - end - end end end end From 857a32d27ad39a27f5da99abac63e1882c36ceec Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Fri, 25 Sep 2020 15:10:39 +0900 Subject: [PATCH 04/10] Fix a typo --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f995caf35fb..7a810a6a155d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,7 @@ ### Changes -* [#8785](https://github.com/rubocop-hq/rubocop/pull/8785): Update TargetRubyVersion 2.8 to 3.0 (experimental). ([@em-gazelle][]) +* [#8785](https://github.com/rubocop-hq/rubocop/pull/8785): Update TargetRubyVersion 2.8 to 3.0 (experimental). ([@koic][]) ## 0.91.1 (2020-09-23) From 8581fe5216c8d511b5a238747c6bedcd64b11d63 Mon Sep 17 00:00:00 2001 From: Marc-Andre Lafortune Date: Thu, 24 Sep 2020 17:04:04 -0400 Subject: [PATCH 05/10] Tweak spec for Layout/AccessModifierIndentation --- spec/rubocop/cop/layout/access_modifier_indentation_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/rubocop/cop/layout/access_modifier_indentation_spec.rb b/spec/rubocop/cop/layout/access_modifier_indentation_spec.rb index 331df1f93c3f..93278965d99f 100644 --- a/spec/rubocop/cop/layout/access_modifier_indentation_spec.rb +++ b/spec/rubocop/cop/layout/access_modifier_indentation_spec.rb @@ -175,7 +175,7 @@ def test; end it 'registers an offense and corrects access modifiers ' \ 'in arbitrary blocks' do expect_offense(<<~RUBY) - Test = func do + func do private ^^^^^^^ Indent access modifiers like `private`. @@ -185,7 +185,7 @@ def test; end RUBY expect_correction(<<~RUBY) - Test = func do + func do private From 73d657a3c9ff2b8f0817091ac7d4865ef9630a3b Mon Sep 17 00:00:00 2001 From: fatkodima Date: Thu, 24 Sep 2020 23:14:59 +0300 Subject: [PATCH 06/10] Add `AllowedMethods` option to `Style/OptionalBooleanParameter` cop --- CHANGELOG.md | 4 ++++ config/default.yml | 2 ++ docs/modules/ROOT/pages/cops_style.adoc | 23 ++++++++++++++++++- .../cop/style/optional_boolean_parameter.rb | 14 ++++++++--- .../style/optional_boolean_parameter_spec.rb | 22 ++++++++++++------ 5 files changed, 54 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7a810a6a155d..125122ef8f94 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## master (unreleased) +### New features + +* [#8790](https://github.com/rubocop-hq/rubocop/pull/8790): Add `AllowedMethods` option to `Style/OptionalBooleanParameter` cop. ([@fatkodima][]) + ### Bug fixes * [#8774](https://github.com/rubocop-hq/rubocop/issues/8774): Fix a false positive for `Layout/ArrayAlignment` with parallel assignment. ([@dvandersluis][]) diff --git a/config/default.yml b/config/default.yml index 595fb33236e0..5f3adaee6251 100644 --- a/config/default.yml +++ b/config/default.yml @@ -3725,6 +3725,8 @@ Style/OptionalBooleanParameter: Enabled: pending Safe: false VersionAdded: '0.89' + AllowedMethods: + - respond_to_missing? Style/OrAssignment: Description: 'Recommend usage of double pipe equals (||=) where applicable.' diff --git a/docs/modules/ROOT/pages/cops_style.adoc b/docs/modules/ROOT/pages/cops_style.adoc index f0ab71073ec9..a668eaf9d100 100644 --- a/docs/modules/ROOT/pages/cops_style.adoc +++ b/docs/modules/ROOT/pages/cops_style.adoc @@ -6888,7 +6888,8 @@ end |=== This cop checks for places where keyword arguments can be used instead of -boolean arguments when defining methods. +boolean arguments when defining methods. `respond_to_missing?` method is allowed by default. +These are customizable with `AllowedMethods` option. === Examples @@ -6911,6 +6912,26 @@ def some_method(bar: false) end ---- +==== AllowedMethods: ['some_method'] + +[source,ruby] +---- +# good +def some_method(bar = false) + puts bar +end +---- + +=== Configurable attributes + +|=== +| Name | Default value | Configurable values + +| AllowedMethods +| `respond_to_missing?` +| Array +|=== + === References * https://rubystyle.guide#boolean-keyword-arguments diff --git a/lib/rubocop/cop/style/optional_boolean_parameter.rb b/lib/rubocop/cop/style/optional_boolean_parameter.rb index 9a31c3cd10dd..0919a941dab8 100644 --- a/lib/rubocop/cop/style/optional_boolean_parameter.rb +++ b/lib/rubocop/cop/style/optional_boolean_parameter.rb @@ -4,7 +4,8 @@ module RuboCop module Cop module Style # This cop checks for places where keyword arguments can be used instead of - # boolean arguments when defining methods. + # boolean arguments when defining methods. `respond_to_missing?` method is allowed by default. + # These are customizable with `AllowedMethods` option. # # @example # # bad @@ -23,13 +24,20 @@ module Style # puts bar # end # + # @example AllowedMethods: ['some_method'] + # # good + # def some_method(bar = false) + # puts bar + # end + # class OptionalBooleanParameter < Base + include AllowedMethods + MSG = 'Use keyword arguments when defining method with boolean argument.' BOOLEAN_TYPES = %i[true false].freeze - METHODS_EXCLUDED = %i[respond_to_missing?].freeze def on_def(node) - return if METHODS_EXCLUDED.include?(node.method_name) + return if allowed_method?(node.method_name) node.arguments.each do |arg| next unless arg.optarg_type? diff --git a/spec/rubocop/cop/style/optional_boolean_parameter_spec.rb b/spec/rubocop/cop/style/optional_boolean_parameter_spec.rb index d207a1e95848..e589b04a3bd0 100644 --- a/spec/rubocop/cop/style/optional_boolean_parameter_spec.rb +++ b/spec/rubocop/cop/style/optional_boolean_parameter_spec.rb @@ -1,7 +1,9 @@ # frozen_string_literal: true -RSpec.describe RuboCop::Cop::Style::OptionalBooleanParameter do - subject(:cop) { described_class.new } +RSpec.describe RuboCop::Cop::Style::OptionalBooleanParameter, :config do + let(:cop_config) do + { 'AllowedMethods' => [] } + end it 'registers an offense when defining method with optional boolean arg' do expect_offense(<<~RUBY) @@ -49,10 +51,16 @@ def some_method(bar = 'foo') RUBY end - it 'does not register an offense when defining respond_to_missing? method with boolean arg' do - expect_no_offenses(<<~RUBY) - def respond_to_missing?(arg, bar = false) - end - RUBY + context 'when AllowedMethods is not empty' do + let(:cop_config) do + { 'AllowedMethods' => %w[respond_to_missing?] } + end + + it 'does not register an offense' do + expect_no_offenses(<<~RUBY) + def respond_to_missing?(method, include_all = false) + end + RUBY + end end end From da5b737afd4baa6445c3dfb6e018006133d4898d Mon Sep 17 00:00:00 2001 From: Daniel Vandersluis Date: Fri, 25 Sep 2020 02:35:27 -0400 Subject: [PATCH 07/10] Fix crash when `expect_corrections` runs into an infinite loop (#8779) --- lib/rubocop/rspec/expect_offense.rb | 10 +++++----- lib/rubocop/runner.rb | 6 +++++- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/rubocop/rspec/expect_offense.rb b/lib/rubocop/rspec/expect_offense.rb index c64c2359f01a..828b60368e46 100644 --- a/lib/rubocop/rspec/expect_offense.rb +++ b/lib/rubocop/rspec/expect_offense.rb @@ -133,14 +133,14 @@ def expect_offense(source, file = nil, severity: nil, **replacements) "#{@processed_source.diagnostics.map(&:render).join("\n")}" end - offenses = _investigate(cop, @processed_source) + @offenses = _investigate(cop, @processed_source) actual_annotations = - expected_annotations.with_offense_annotations(offenses) + expected_annotations.with_offense_annotations(@offenses) expect(actual_annotations).to eq(expected_annotations), '' - expect(offenses.map(&:severity).uniq).to eq([severity]) if severity + expect(@offenses.map(&:severity).uniq).to eq([severity]) if severity - offenses + @offenses end def expect_correction(correction, loop: true) @@ -157,7 +157,7 @@ def expect_correction(correction, loop: true) break corrected_source if corrected_source == @processed_source.buffer.source if iteration > RuboCop::Runner::MAX_ITERATIONS - raise RuboCop::Runner::InfiniteCorrectionLoop.new(@processed_source.path, []) + raise RuboCop::Runner::InfiniteCorrectionLoop.new(@processed_source.path, [@offenses]) end # Prepare for next loop diff --git a/lib/rubocop/runner.rb b/lib/rubocop/runner.rb index f639f667e4fc..cd46878d099e 100644 --- a/lib/rubocop/runner.rb +++ b/lib/rubocop/runner.rb @@ -16,7 +16,11 @@ def initialize(path, offenses_by_iteration, loop_start: -1) root_cause = offenses_by_iteration[loop_start..-1] .map { |x| x.map(&:cop_name).uniq.join(', ') } .join(' -> ') - super "Infinite loop detected in #{path} and caused by #{root_cause}" + + message = 'Infinite loop detected' + message += " in #{path}" if path + message += " and caused by #{root_cause}" if root_cause + super message end end From f788a75e52873f3c61459f427c4026cddeb1ed11 Mon Sep 17 00:00:00 2001 From: tleish Date: Fri, 25 Sep 2020 00:37:46 -0600 Subject: [PATCH 08/10] [Fix #8650] Improve hidden files finder performance (#8784) Co-authored-by: Bozhidar Batsov --- CHANGELOG.md | 3 +++ lib/rubocop/target_finder.rb | 5 ++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 125122ef8f94..0f04e919371c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,7 +12,9 @@ ### Changes +* [#8785](https://github.com/rubocop-hq/rubocop/pull/8785): Update TargetRubyVersion 2.8 to 3.0 (experimental). ([@em-gazelle][]) * [#8785](https://github.com/rubocop-hq/rubocop/pull/8785): Update TargetRubyVersion 2.8 to 3.0 (experimental). ([@koic][]) +* [#8650](https://github.com/rubocop-hq/rubocop/issues/8650): Faster find of hidden files in `TargetFinder` class which improves rubocop initial startup speed. ([@tleish][]) ## 0.91.1 (2020-09-23) @@ -4916,3 +4918,4 @@ [@fsateler]: https://github.com/fsateler [@iSarCasm]: https://github.com/iSarCasm [@em-gazelle]: https://github.com/em-gazelle +[@tleish]: https://github.com/tleish diff --git a/lib/rubocop/target_finder.rb b/lib/rubocop/target_finder.rb index 3f6f45ed3974..76294783611c 100644 --- a/lib/rubocop/target_finder.rb +++ b/lib/rubocop/target_finder.rb @@ -5,6 +5,8 @@ module RuboCop # and picking ruby files. # @api private class TargetFinder + HIDDEN_PATH_SUBSTRING = "#{File::SEPARATOR}." + def initialize(config_store, options = {}) @config_store = config_store @options = options @@ -55,7 +57,8 @@ def target_files_in_dir(base_dir = Dir.pwd) # Support Windows: Backslashes from command-line -> forward slashes base_dir = base_dir.gsub(File::ALT_SEPARATOR, File::SEPARATOR) if File::ALT_SEPARATOR all_files = find_files(base_dir, File::FNM_DOTMATCH) - hidden_files = Set.new(all_files - find_files(base_dir, 0)) + # use file.include? for performance optimization + hidden_files = all_files.select { |file| file.include?(HIDDEN_PATH_SUBSTRING) } base_dir_config = @config_store.for(base_dir) target_files = all_files.select do |file| From 0d168ddea51cacd812cf9adf7c2a72cfd66dfd18 Mon Sep 17 00:00:00 2001 From: Marc-Andre Lafortune Date: Thu, 24 Sep 2020 15:20:07 -0400 Subject: [PATCH 09/10] Add callback for `rubocop/ast` Loading the version first also gives an opportunity for `rubocop/ast` to react. Removing handling of rubocop-ast <1.0 in gemspec no longer needed --- lib/rubocop.rb | 6 ++++-- rubocop.gemspec | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/rubocop.rb b/lib/rubocop.rb index a2fb01208534..005bf21be7d8 100644 --- a/lib/rubocop.rb +++ b/lib/rubocop.rb @@ -8,12 +8,13 @@ require 'forwardable' require 'regexp_parser' require 'unicode/display_width/no_string_ext' + +require_relative 'rubocop/version' require 'rubocop-ast' + require_relative 'rubocop/ast_aliases' require_relative 'rubocop/ext/regexp_node' -require_relative 'rubocop/version' - require_relative 'rubocop/core_ext/string' require_relative 'rubocop/ext/processed_source' @@ -636,3 +637,4 @@ unless File.exist?("#{__dir__}/../rubocop.gemspec") # Check if we are a gem RuboCop::ResultCache.rubocop_required_features = $LOADED_FEATURES - before_us end +RuboCop::AST.rubocop_loaded if RuboCop::AST.respond_to?(:rubocop_loaded) diff --git a/rubocop.gemspec b/rubocop.gemspec index 693b034133aa..f124b343d337 100644 --- a/rubocop.gemspec +++ b/rubocop.gemspec @@ -38,7 +38,7 @@ Gem::Specification.new do |s| s.add_runtime_dependency('rainbow', '>= 2.2.2', '< 4.0') s.add_runtime_dependency('regexp_parser', '>= 1.7') s.add_runtime_dependency('rexml') - s.add_runtime_dependency('rubocop-ast', '>= 0.5.0', '< 1.0') + s.add_runtime_dependency('rubocop-ast', '>= 0.5.0') s.add_runtime_dependency('ruby-progressbar', '~> 1.7') s.add_runtime_dependency('unicode-display_width', '>= 1.4.0', '< 2.0') From fb275119de364c60a800d018916ca048f29870b6 Mon Sep 17 00:00:00 2001 From: Daniel Vandersluis Date: Fri, 25 Sep 2020 02:40:15 -0400 Subject: [PATCH 10/10] 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 0f04e919371c..a53fc1330a44 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 5f3adaee6251..0d7b46acc3a6 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 a668eaf9d100..e1ea36e09d05 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 a3ff50cfa545..d7b9a6c00977 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 02249c8f679a..85dc2893eac6 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