diff --git a/lib/rubocop/cop/rspec/cop.rb b/lib/rubocop/cop/rspec/cop.rb index d9f09d4f1..30595acad 100644 --- a/lib/rubocop/cop/rspec/cop.rb +++ b/lib/rubocop/cop/rspec/cop.rb @@ -21,14 +21,6 @@ class Cop < ::RuboCop::Cop::Base include RuboCop::RSpec::Language include RuboCop::RSpec::Language::NodePattern - DEFAULT_CONFIGURATION = - RuboCop::RSpec::CONFIG.fetch('AllCops').fetch('RSpec') - - DEFAULT_PATTERN_RE = Regexp.union( - DEFAULT_CONFIGURATION.fetch('Patterns') - .map(&Regexp.public_method(:new)) - ) - # Invoke the original inherited hook so our cops are recognized def self.inherited(subclass) RuboCop::Cop::Cop.inherited(subclass) @@ -41,32 +33,28 @@ def relevant_file?(file) private def relevant_rubocop_rspec_file?(file) - rspec_pattern =~ file + self.class.rspec_pattern =~ file end - def rspec_pattern - if rspec_pattern_config? - Regexp.union(rspec_pattern_config.map(&Regexp.public_method(:new))) - else - DEFAULT_PATTERN_RE + class << self + def rspec_pattern + @rspec_pattern ||= + Regexp.union( + rspec_pattern_config.map(&Regexp.public_method(:new)) + ) end - end - def all_cops_config - config - .for_all_cops - end + private - def rspec_pattern_config? - return unless all_cops_config.key?('RSpec') + def rspec_pattern_config + default_configuration = + RuboCop::RSpec::CONFIG.fetch('AllCops').fetch('RSpec') - all_cops_config.fetch('RSpec').key?('Patterns') - end - - def rspec_pattern_config - all_cops_config - .fetch('RSpec', DEFAULT_CONFIGURATION) - .fetch('Patterns') + Config.new + .for_all_cops + .fetch('RSpec', default_configuration) + .fetch('Patterns') + end end end end