Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve performance when user does not override default RSpec Pattern config #507

Merged
merged 1 commit into from
Nov 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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