diff --git a/changelog/fix_fix_calculation_of_cop_department_for.md b/changelog/fix_fix_calculation_of_cop_department_for.md new file mode 100644 index 00000000000..5d9620cd68d --- /dev/null +++ b/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][]) diff --git a/lib/rubocop/config.rb b/lib/rubocop/config.rb index 40c07a4464b..d02dcd6956f 100644 --- a/lib/rubocop/config.rb +++ b/lib/rubocop/config.rb @@ -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 diff --git a/spec/rubocop/config_spec.rb b/spec/rubocop/config_spec.rb index 020ec9c5f41..b9b541ccdb2 100644 --- a/spec/rubocop/config_spec.rb +++ b/spec/rubocop/config_spec.rb @@ -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