From 995b4fe5f16cd73feb2625eadd0fad236cfab38e Mon Sep 17 00:00:00 2001 From: Keiji Yoshimi Date: Mon, 27 Nov 2017 00:19:36 +0900 Subject: [PATCH] Improve performance when user does not override default RSpec Pattern config --- CHANGELOG.md | 2 ++ lib/rubocop/cop/rspec/cop.rb | 24 ++++++++++++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b8c592457..c4fd3b59e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Master (Unreleased) +* Improve performance when user does not override default RSpec Pattern config. ([@walf443][]) + ## 1.20.1 (2017-11-15) * Add "without" to list of default allowed prefixes for `RSpec/ContextWording`. ([@bquorning][]) diff --git a/lib/rubocop/cop/rspec/cop.rb b/lib/rubocop/cop/rspec/cop.rb index 325cc4b39..d81604f31 100644 --- a/lib/rubocop/cop/rspec/cop.rb +++ b/lib/rubocop/cop/rspec/cop.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module RuboCop module Cop # rubocop:disable Style/Documentation WorkaroundCop = Cop.dup @@ -42,6 +44,11 @@ class Cop < WorkaroundCop 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) @@ -58,12 +65,25 @@ def relevant_rubocop_rspec_file?(file) end def rspec_pattern - Regexp.union(rspec_pattern_config.map(&Regexp.public_method(:new))) + if rspec_pattern_config? + Regexp.union(rspec_pattern_config.map(&Regexp.public_method(:new))) + else + DEFAULT_PATTERN_RE + end end - def rspec_pattern_config + def all_cops_config config .for_all_cops + end + + def rspec_pattern_config? + return unless all_cops_config.key?('RSpec') + all_cops_config.fetch('RSpec').key?('Patterns') + end + + def rspec_pattern_config + all_cops_config .fetch('RSpec', DEFAULT_CONFIGURATION) .fetch('Patterns') end