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

Optimize Base#relevant_rubocop_rspec_file? #967

Closed
wants to merge 1 commit into from
Closed
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
44 changes: 16 additions & 28 deletions lib/rubocop/cop/rspec/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,6 @@ class Base < ::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:disable Lint/MissingSuper
RuboCop::Cop::Base.inherited(subclass)
Expand All @@ -41,32 +33,28 @@ def relevant_file?(file)
private

def relevant_rubocop_rspec_file?(file)
rspec_pattern.match?(file)
self.class.rspec_pattern.match?(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
Expand Down