diff --git a/lib/rubocop/cop/rspec/context_wording.rb b/lib/rubocop/cop/rspec/context_wording.rb index 693c5c185..90b40e56e 100644 --- a/lib/rubocop/cop/rspec/context_wording.rb +++ b/lib/rubocop/cop/rspec/context_wording.rb @@ -65,9 +65,17 @@ class ContextWording < Base (block (send #rspec? { :context :shared_context } $({str dstr xstr} ...) ...) ...) PATTERN + # @!method allowed?(node) + def_node_matcher :allowed?, <<~PATTERN + { + (str #matches_allowed_pattern?) + (xstr (str #matches_allowed_pattern?) ...) + } + PATTERN + def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler context_wording(node) do |context| - if bad_pattern?(context) + unless allowed?(context) message = format(MSG, patterns: expect_patterns) add_offense(context, message: message) end @@ -84,20 +92,6 @@ def prefix_regexes @prefix_regexes ||= prefixes.map { |pre| /^#{Regexp.escape(pre)}\b/ } end - def bad_pattern?(node) - return false if allowed_patterns.empty? - - !matches_allowed_pattern?(description(node)) - end - - def description(context) - if context.xstr_type? - context.value.value - else - context.value - end - end - def expect_patterns inspected = allowed_patterns.map do |pattern| pattern.inspect.gsub(/\A"|"\z/, '/')