From 1ae0334c2f863637bf1193148a7d252bf314b1c6 Mon Sep 17 00:00:00 2001 From: Christoph Olszowka Date: Wed, 29 Jul 2020 23:53:09 +0200 Subject: [PATCH] [FiX #8413] Adjust pending cop warning to be copy&pasteable and inform about NewCops: enable --- CHANGELOG.md | 2 ++ lib/rubocop/config_loader.rb | 23 +++++++++++++++++------ spec/rubocop/cli/cli_options_spec.rb | 10 +++++----- 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e7f40938b25..86002cb230e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,7 @@ ### Changes +* [#8413](https://github.com/rubocop-hq/rubocop/issues/8413): Pending cops warning now contains snippet that can be directly copied into `.rubocop.yml` as well as a notice about `NewCops: enable` config option. ([@colszowka][]) * [#8376](https://github.com/rubocop-hq/rubocop/pull/8376): `Style/MethodMissingSuper` cop is removed in favor of new `Lint/MissingSuper` cop. ([@fatkodima][]) * [#8350](https://github.com/rubocop-hq/rubocop/pull/8350): Set default max line length to 120 for `Style/MultilineMethodSignature`. ([@koic][]) * [#8338](https://github.com/rubocop-hq/rubocop/pull/8338): **potentially breaking**. Config#for_department now returns only the config specified for that department; the 'Enabled' attribute is no longer calculated. ([@marcandre][]) @@ -4724,3 +4725,4 @@ [@knejad]: https://github.com/knejad [@iamravitejag]: https://github.com/iamravitejag [@volfgox]: https://github.com/volfgox +[@colszowka]: https://github.com/colszowka diff --git a/lib/rubocop/config_loader.rb b/lib/rubocop/config_loader.rb index b548388078a..d61e4561987 100644 --- a/lib/rubocop/config_loader.rb +++ b/lib/rubocop/config_loader.rb @@ -140,22 +140,33 @@ def project_root @project_root ||= find_project_root end + PENDING_BANNER = <<~BANNER + The following cops were added to RuboCop, but are not configured. Please set Enabled to either `true` or `false` in your `.rubocop.yml` file. + + Please also note that can also opt-in to new cops by default by adding this to your config: + AllCops: + NewCops: enable + BANNER + def warn_on_pending_cops(pending_cops) return if pending_cops.empty? - warn Rainbow('The following cops were added to RuboCop, but are not ' \ - 'configured. Please set Enabled to either `true` or ' \ - '`false` in your `.rubocop.yml` file:').yellow + warn Rainbow(PENDING_BANNER).yellow pending_cops.each do |cop| - version = cop.metadata['VersionAdded'] || 'N/A' - - warn Rainbow(" - #{cop.name} (#{version})").yellow + warn_pending_cop cop end warn Rainbow('For more information: https://docs.rubocop.org/rubocop/versioning.html').yellow end + def warn_pending_cop(cop) + version = cop.metadata['VersionAdded'] || 'N/A' + + warn Rainbow("#{cop.name}: # (new in #{version})").yellow + warn Rainbow(' Enabled: true').yellow + end + # Merges the given configuration with the default one. def merge_with_default(config, config_file, unset_nil: true) resolver.merge_with_default(config, config_file, unset_nil: unset_nil) diff --git a/spec/rubocop/cli/cli_options_spec.rb b/spec/rubocop/cli/cli_options_spec.rb index ec3b7cdf044..0ac994307ef 100644 --- a/spec/rubocop/cli/cli_options_spec.rb +++ b/spec/rubocop/cli/cli_options_spec.rb @@ -272,7 +272,7 @@ class SomeCop < Cop end let(:pending_cop_warning) { <<~PENDING_COP_WARNING } - The following cops were added to RuboCop, but are not configured. Please set Enabled to either `true` or `false` in your `.rubocop.yml` file: + The following cops were added to RuboCop, but are not configured. Please set Enabled to either `true` or `false` in your `.rubocop.yml` file. PENDING_COP_WARNING let(:inspected_output) { <<~INSPECTED_OUTPUT } @@ -327,9 +327,9 @@ class SomeCop < Cop remaining_range = pending_cop_warning.length..-(inspected_output.length + 1) - pending_cops = output[remaining_range].split("\n") + pending_cops = output[remaining_range] - expect(pending_cops).to include(' - Style/SomeCop (0.80)') + expect(pending_cops).to include("Style/SomeCop: # (new in 0.80)\n Enabled: true") manual_url = output[remaining_range].split("\n").last @@ -346,9 +346,9 @@ class SomeCop < Cop remaining_range = pending_cop_warning.length..-(inspected_output.length + 1) - pending_cops = output[remaining_range].split("\n") + pending_cops = output[remaining_range] - expect(pending_cops).to include(' - Style/SomeCop (N/A)') + expect(pending_cops).to include("Style/SomeCop: # (new in N/A)\n Enabled: true") manual_url = output[remaining_range].split("\n").last