Skip to content

Commit

Permalink
Display version with cop added when pending cops warning
Browse files Browse the repository at this point in the history
Follow up of #7757 (comment).

When warning about a pending cop, this PR displays the version with the cop added.

## Before

```console
% rubocop
The following cops were added to RuboCop, but are not configured. Please
set Enabled to either `true` or `false` in your `.rubocop.yml` file:
 - Style/HashEachMethods
 - Style/HashTransformKeys
 - Style/HashTransformValues
```

## After

```console
% rubocop
The following cops were added to RuboCop, but are not configured. Please
set Enabled to either `true` or `false` in your `.rubocop.yml` file:
 - Style/HashEachMethods (0.80)
 - Style/HashTransformKeys (0.80)
 - Style/HashTransformValues (0.80)
For more information: https://docs.rubocop.org/en/latest/versioning/
```
  • Loading branch information
koic committed Feb 29, 2020
1 parent 63c7666 commit 20350e8
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -10,6 +10,10 @@
* [#7745](https://github.com/rubocop-hq/rubocop/issues/7745): Suppress a pending cop warnings when pending cop's department is disabled. ([@koic][])
* [#7759](https://github.com/rubocop-hq/rubocop/issues/7759): Fix an error for `Layout/LineLength` cop when using lambda syntax that argument is not enclosed in parentheses. ([@koic][])

### Changes

* [#7765](https://github.com/rubocop-hq/rubocop/pull/7765): When warning about a pending cop, display the version with the cop added. ([@koic][])

## 0.80.0 (2020-02-18)

### New features
Expand Down
9 changes: 7 additions & 2 deletions lib/rubocop/config.rb
Expand Up @@ -16,6 +16,8 @@ class Config
include FileFinder
extend Forwardable

CopConfig = Struct.new(:name, :metadata)

DEFAULT_RAILS_VERSION = 5.0
attr_reader :loaded_path

Expand Down Expand Up @@ -219,11 +221,14 @@ def bundler_lock_file_path
end

def pending_cops
keys.select do |qualified_cop_name|
keys.each_with_object([]) do |qualified_cop_name, pending_cops|
department = department_of(qualified_cop_name)
next if department && department['Enabled'] == false

self[qualified_cop_name]['Enabled'] == 'pending'
cop_metadata = self[qualified_cop_name]
next unless cop_metadata['Enabled'] == 'pending'

pending_cops << CopConfig.new(qualified_cop_name, cop_metadata)
end
end

Expand Down
11 changes: 7 additions & 4 deletions lib/rubocop/config_loader.rb
Expand Up @@ -92,7 +92,7 @@ def configuration_from_file(config_file)
add_excludes_from_files(config, config_file)
end
merge_with_default(config, config_file).tap do |merged_config|
warn_on_pending_cops(merged_config)
warn_on_pending_cops(merged_config.pending_cops)
end
end

Expand All @@ -116,17 +116,20 @@ def default_configuration
end
end

def warn_on_pending_cops(config)
pending_cops = config.pending_cops
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

pending_cops.each do |cop|
warn Rainbow(" - #{cop}").yellow
warn Rainbow(
" - #{cop.name} (#{cop.metadata['VersionAdded']})"
).yellow
end

warn Rainbow('For more information: https://docs.rubocop.org/en/latest/versioning/').yellow
end

# Merges the given configuration with the default one. If
Expand Down
15 changes: 15 additions & 0 deletions manual/versioning.md
Expand Up @@ -5,5 +5,20 @@ RuboCop is stable between major versions, both in terms of API and cops.
New cops introduced between major versions are set to a special pending
status and are not enabled by default. A warning is emitted if such cops
are not explicitly enabled or disabled in the user configuration.
Please set `Enabled` to either `true` or `false` in your `.rubocop.yml` file.

`Style/ANewCop` is an example of a newly added pending cop:

```yaml
Style/ANewCop:
Enabled: true
```

or

```yaml
Style/ANewCop:
Enabled: false
```

On major version updates, pending cops are enabled in bulk.
14 changes: 13 additions & 1 deletion spec/rubocop/cli/cli_options_spec.rb
Expand Up @@ -240,6 +240,10 @@ class SomeCop < Cop
2 files inspected, no offenses detected
INSPECTED_OUTPUT

let(:versioning_manual_url) { <<~VERSIONING_MANUAL_URL.chop }
For more information: https://docs.rubocop.org/en/latest/versioning/
VERSIONING_MANUAL_URL

before do
create_file('rubocop_ext.rb', <<~RUBY)
module RuboCop
Expand All @@ -263,6 +267,7 @@ class SomeCop < Cop
Style/SomeCop:
Description: Something
Enabled: pending
VersionAdded: '0.80'
YAML
end

Expand All @@ -273,7 +278,14 @@ class SomeCop < Cop
remaining_range =
pending_cop_warning.length..-(inspected_output.length + 1)
pending_cops = output[remaining_range].split("\n")
expect(pending_cops).to include(' - Style/SomeCop')

expect(pending_cops).to include(
' - Style/SomeCop (0.80)'
)

manual_url = output[remaining_range].split("\n").last

expect(manual_url).to eq(versioning_manual_url)
end
end

Expand Down

0 comments on commit 20350e8

Please sign in to comment.