Skip to content

Commit

Permalink
Allow enabling departments when DisabledByDefault is true
Browse files Browse the repository at this point in the history
When DisabledByDefault is true, only cops explicitly enabled
in user configuration are run. This should work for
departments too.
  • Loading branch information
jonas054 authored and bbatsov committed Apr 23, 2017
1 parent bb521fd commit 4a4134c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
* [#4268](https://github.com/bbatsov/rubocop/issues/4268): Handle end-of-line comments when autocorrecting Style/EmptyLinesAroundAccessModifier. ([@vergenzt][])
* [#4275](https://github.com/bbatsov/rubocop/issues/4275): Prevent `Style/MethodCallWithArgsParentheses` from blowing up on `yield`. ([@drenmi][])
* [#3969](https://github.com/bbatsov/rubocop/issues/3969): Handle multiline method call alignment for arguments to methods. ([@jonas054][])
* [#4304](https://github.com/bbatsov/rubocop/pull/4304): Allow enabling whole departments when `DisabledByDefault` is `true`. ([@jonas054][])

## 0.48.1 (2017-04-03)

Expand Down
22 changes: 19 additions & 3 deletions lib/rubocop/config_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,7 @@ def merge_with_default(config, config_file)
end

if disabled_by_default
config = transform(config) do |params|
{ 'Enabled' => true }.merge(params) # Set true if not set.
end
config = handle_disabled_by_default(config, default_configuration)
end

Config.new(merge(default_configuration, config), config_file)
Expand All @@ -164,6 +162,24 @@ def target_ruby_version_to_f!(hash)

private

def handle_disabled_by_default(config, new_default_configuration)
department_config = config.to_hash.reject { |cop| cop.include?('/') }
department_config.each do |dept, dept_params|
next unless dept_params['Enabled']

new_default_configuration.each do |cop, params|
next unless cop.start_with?(dept + '/')

# Retain original default configuration for cops in the department.
params['Enabled'] = default_configuration[cop]['Enabled']
end
end

transform(config) do |params|
{ 'Enabled' => true }.merge(params) # Set true if not set.
end
end

# Returns a new hash where the parameters of the given config hash have
# been replaced by parameters returned by the given block.
def transform(config)
Expand Down
26 changes: 26 additions & 0 deletions spec/rubocop/config_loader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,32 @@ def cop_enabled?(cop_class)
cop_class = RuboCop::Cop::Layout::TrailingWhitespace
expect(cop_enabled?(cop_class)).to be false
end

context 'and a department is enabled' do
let(:config) do
<<-END.strip_indent
AllCops:
DisabledByDefault: true
Style:
Enabled: true
END
end

it 'enables cops in that department' do
cop_class = RuboCop::Cop::Style::Alias
expect(cop_enabled?(cop_class)).to be true
end

it 'disables cops in other departments' do
cop_class = RuboCop::Cop::Layout::AlignHash
expect(cop_enabled?(cop_class)).to be false
end

it 'keeps cops that are disabled in default configuration disabled' do
cop_class = RuboCop::Cop::Style::AutoResourceCleanup
expect(cop_enabled?(cop_class)).to be false
end
end
end

context 'when EnabledByDefault is true' do
Expand Down

0 comments on commit 4a4134c

Please sign in to comment.