Skip to content

Commit

Permalink
[Fix #2343] Groups of cops can be disabled in rubocop.yml
Browse files Browse the repository at this point in the history
Using the cop type, it is now possible to do this:

    Style:
      Enabled: false
    Performance:
      Enabled: false

...and so on.

If there are settings for both a cop type and a cop of that type, then if
either of them has `Enabled: false`, the cop will be disabled, UNLESS it is
specified in an `--only` CLI argument.
  • Loading branch information
alexdowad committed Dec 29, 2015
1 parent 09518bb commit 37943df
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -52,6 +52,7 @@
* [#1543](https://github.com/bbatsov/rubocop/issues/1543): `Style/WordArray` has both `percent` and `brackets` (which enforces the use of bracketed arrays for strings) styles. ([@alexdowad][])
* `Style/SpaceAroundOperators` has `AllowForAlignment` config parameter which allows extra spaces on the left if they serve to align the operator with another. ([@alexdowad][])
* `Style/SymbolArray` has both `percent` and `brackets` (which enforces the user of bracketed arrays for symbols) styles. ([@alexdowad][])
* [#2343](https://github.com/bbatsov/rubocop/issues/2343): Entire cop types (or "departments") can be disabled using in .rubocop.yml using config like `Style: Enabled: false`. ([@alexdowad][])

### Bug Fixes

Expand Down
12 changes: 12 additions & 0 deletions lib/rubocop/config.rb
Expand Up @@ -3,6 +3,8 @@
require 'delegate'
require 'pathname'

# rubocop:disable Metrics/ClassLength

module RuboCop
# This class represents the configuration of the RuboCop application
# and all its cops. A Config is associated with a YAML configuration
Expand Down Expand Up @@ -78,6 +80,16 @@ def for_cop(cop)
end

def cop_enabled?(cop)
department = if cop.respond_to?(:cop_type)
cop.cop_type.to_s.capitalize
else
cop.split('/')[-2]
end

if (dept_config = self[department])
return false if dept_config['Enabled'] == false
end

for_cop(cop).empty? || for_cop(cop)['Enabled']
end

Expand Down
34 changes: 34 additions & 0 deletions spec/rubocop/config_spec.rb
Expand Up @@ -329,4 +329,38 @@
end
end
end

describe '#cop_enabled?' do
context 'when an entire cop type is disabled' do
context 'but an individual cop is enabled' do
let(:hash) do
{
'Style' => { 'Enabled' => false },
'Style/TrailingWhitespace' => { 'Enabled' => true }
}
end

it 'still disables the cop' do
cop_class = RuboCop::Cop::Style::TrailingWhitespace
expect(configuration.cop_enabled?(cop_class)).to be false
end
end
end

context 'when an entire cop type is enabled' do
context 'but an individual cop is disabled' do
let(:hash) do
{
'Style' => { 'Enabled' => true },
'Style/TrailingWhitespace' => { 'Enabled' => false }
}
end

it 'still disables the cop' do
cop_class = RuboCop::Cop::Style::TrailingWhitespace
expect(configuration.cop_enabled?(cop_class)).to be false
end
end
end
end
end

0 comments on commit 37943df

Please sign in to comment.