From eab62cccda7bc31478ee2221739381e57d82e631 Mon Sep 17 00:00:00 2001 From: Marc-Andre Lafortune Date: Tue, 14 Jul 2020 16:48:50 -0400 Subject: [PATCH] Add Config#for_badge This optimization saves about 5MB of unneeded intermediate hashes... --- CHANGELOG.md | 1 + lib/rubocop/config.rb | 10 +++++++++- lib/rubocop/cop/base.rb | 3 +-- spec/rubocop/config_spec.rb | 25 +++++++++++++++++++++++++ 4 files changed, 36 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 91ea403f2bb..029d08fbde9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### New features * [#8322](https://github.com/rubocop-hq/rubocop/pull/8322): Support autocorrect for `Style/CaseEquality` cop. ([@fatkodima][]) +* [#8339](https://github.com/rubocop-hq/rubocop/pull/8339): Add `Config#for_badge` as an efficient way to get a cop's config merged with its department's. ([@marcandre][]) ### Bug fixes diff --git a/lib/rubocop/config.rb b/lib/rubocop/config.rb index 966723f6774..f8dc40c47ed 100644 --- a/lib/rubocop/config.rb +++ b/lib/rubocop/config.rb @@ -47,7 +47,7 @@ def check end def_delegators :@hash, :[], :[]=, :delete, :each, :key?, :keys, :each_key, - :map, :merge, :to_h, :to_hash, :transform_values + :fetch, :map, :merge, :to_h, :to_hash, :transform_values def_delegators :@validator, :validate, :target_ruby_version def to_s @@ -111,6 +111,14 @@ def for_cop(cop) @for_cop[cop.respond_to?(:cop_name) ? cop.cop_name : cop] end + # @return [Config] for the given cop merged with that of its department (if any) + # Note: the 'Enabled' attribute is same as that returned by `for_cop` + def for_badge(badge) + cop_config = for_cop(badge.to_s) + fetch(badge.department.to_s) { return cop_config } + .merge(cop_config) + end + # @return [Config] for the given department name. # Note: the 'Enabled' attribute will be present only if specified # at the department's level diff --git a/lib/rubocop/cop/base.rb b/lib/rubocop/cop/base.rb index a5d6a07cfdf..c25c9a7e936 100644 --- a/lib/rubocop/cop/base.rb +++ b/lib/rubocop/cop/base.rb @@ -189,8 +189,7 @@ def cop_name def cop_config # Use department configuration as basis, but let individual cop # configuration override. - @cop_config ||= @config.for_cop(self.class.department.to_s) - .merge(@config.for_cop(self)) + @cop_config ||= @config.for_badge(self.class.badge) end def config_to_allow_offenses diff --git a/spec/rubocop/config_spec.rb b/spec/rubocop/config_spec.rb index 444254c2e07..0077304fe79 100644 --- a/spec/rubocop/config_spec.rb +++ b/spec/rubocop/config_spec.rb @@ -735,6 +735,31 @@ end end + describe '#for_badge' do + let(:hash) do + { + 'Style' => { 'Foo' => 42, 'Bar' => 666 }, + 'Layout/TrailingWhitespace' => { 'Bar' => 43 }, + 'Style/Alias' => { 'Bar' => 44 } + } + end + + it 'takes into account the department' do + expect(configuration.for_badge(RuboCop::Cop::Style::Alias.badge)).to eq( + { 'Enabled' => true, + 'Foo' => 42, + 'Bar' => 44 } + ) + end + + it 'works if department has no config' do + expect(configuration.for_badge(RuboCop::Cop::Layout::TrailingWhitespace.badge)).to eq( + { 'Enabled' => true, + 'Bar' => 43 } + ) + end + end + context 'whether the cop is enabled' do def cop_enabled(cop_class) configuration.for_cop(cop_class).fetch('Enabled')