Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display version with cop added when pending cops warning #7765

Merged
merged 1 commit into from Feb 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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