Skip to content

Commit

Permalink
Fix calculation of cop department for nested departments
Browse files Browse the repository at this point in the history
This fixes an issue I ran into with rubocop-i18n. This gem provides two
nested departments, I18n/RailsI18n and I18n/GetText. The intent is to
enable one and disable the other. However, disabling a department would
not disable its cops.

I tracked this issue down to how RuboCop::Config calculates a cops
department before looking at that department's configuration: It only
considers the part before the first `/`.

This commit changes this method by considering instead all parts before
the last `/`.
  • Loading branch information
mvz authored and bbatsov committed Dec 20, 2020
1 parent a7a6baf commit 2e7ef70
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
1 change: 1 addition & 0 deletions changelog/fix_fix_calculation_of_cop_department_for.md
@@ -0,0 +1 @@
* [#9258](https://github.com/rubocop-hq/rubocop/pull/9258): Fix calculation of cop department for nested departments. ([@mvz][])
6 changes: 3 additions & 3 deletions lib/rubocop/config.rb
Expand Up @@ -292,10 +292,10 @@ def enable_cop?(qualified_cop_name, cop_options)
end

def department_of(qualified_cop_name)
cop_department, cop_name = qualified_cop_name.split('/')
return nil if cop_name.nil?
*cop_department, _ = qualified_cop_name.split('/')
return nil if cop_department.empty?

self[cop_department]
self[cop_department.join('/')]
end
end
end
Expand Down
16 changes: 16 additions & 0 deletions spec/rubocop/config_spec.rb
Expand Up @@ -781,6 +781,22 @@ def cop_enabled(cop_class)
end
end

context 'when an nested cop department is disabled' do
context 'but an individual cop is enabled' do
let(:hash) do
{
'Foo/Bar' => { 'Enabled' => false },
'Foo/Bar/BazCop' => { 'Enabled' => true }
}
end

it 'still disables the cop' do
cop_class = 'Foo/Bar/BazCop'
expect(cop_enabled(cop_class)).to be false
end
end
end

context 'when an entire cop department is enabled' do
context 'but an individual cop is disabled' do
let(:hash) do
Expand Down

0 comments on commit 2e7ef70

Please sign in to comment.