diff --git a/lib/rubocop/cop/rspec/cop.rb b/lib/rubocop/cop/rspec/cop.rb index 0bcbdbe58..fcb04adda 100644 --- a/lib/rubocop/cop/rspec/cop.rb +++ b/lib/rubocop/cop/rspec/cop.rb @@ -69,6 +69,10 @@ def rspec_pattern_config .fetch('RSpec', DEFAULT_CONFIGURATION) .fetch('Patterns') end + + def rspec_language_config(*keys) + all_cops_config.dig('RSpec', 'Language', *keys)&.map(&:to_sym) || [] + end end end end diff --git a/lib/rubocop/rspec/language.rb b/lib/rubocop/rspec/language.rb index 2b83c9d5c..5cb0bcc84 100644 --- a/lib/rubocop/rspec/language.rb +++ b/lib/rubocop/rspec/language.rb @@ -53,6 +53,24 @@ def node_pattern attr_reader :selectors end + module BuiltIn + LANGUAGE = { + 'Hooks' => %i[ + prepend_before + before + append_before + around + prepend_after + after + append_after + ] + }.freeze + + def self.language_elements_for(*keys) + LANGUAGE.dig(*keys) || [] + end + end + module ExampleGroups GROUPS = SelectorSet.new(%i[describe context feature example_group]) SKIPPED = SelectorSet.new(%i[xdescribe xcontext xfeature]) @@ -91,16 +109,6 @@ module Examples end module Hooks - BUILT_IN_METHOD_NAMES = %i[ - prepend_before - before - append_before - around - prepend_after - after - append_after - ].freeze - module Scopes ALL = SelectorSet.new( %i[ diff --git a/lib/rubocop/rspec/language/node_pattern.rb b/lib/rubocop/rspec/language/node_pattern.rb index 36c24022a..68899a84d 100644 --- a/lib/rubocop/rspec/language/node_pattern.rb +++ b/lib/rubocop/rspec/language/node_pattern.rb @@ -29,14 +29,7 @@ module NodePattern end def hook_selectors - custom_method_names = config - .for_all_cops - .fetch('RSpec', {}) - .fetch('Language', {}) - .fetch('Hooks', []) - .map(&:to_sym) - - SelectorSet.new(Hooks::BUILT_IN_METHOD_NAMES + custom_method_names) + selectors('Hooks') end def all_selectors @@ -49,6 +42,14 @@ def all_selectors Expectations::ALL + Runners::ALL end + + protected + + def selectors(*keys) + SelectorSet.new( + BuiltIn.language_elements_for(*keys) + rspec_language_config(*keys) + ) + end end end end