Skip to content

Commit

Permalink
Merge pull request #507 from walf443/improve_rspec_config_pattern
Browse files Browse the repository at this point in the history
Improve performance when user does not override default RSpec Pattern config
  • Loading branch information
bquorning committed Nov 29, 2017
2 parents 19a7034 + 995b4fe commit d270799
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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][])
Expand Down
24 changes: 22 additions & 2 deletions lib/rubocop/cop/rspec/cop.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module RuboCop
module Cop # rubocop:disable Style/Documentation
WorkaroundCop = Cop.dup
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit d270799

Please sign in to comment.