lib/rubocop/cop/rspec/focus.rb in rubocop-rspec-1.5.1 vs lib/rubocop/cop/rspec/focus.rb in rubocop-rspec-1.5.2

- old
+ new

@@ -1,6 +1,5 @@ -# encoding: utf-8 # frozen_string_literal: true module RuboCop module Cop module RSpec @@ -19,47 +18,66 @@ # # # good # describe MyClass do # end class Focus < Cop - MESSAGE = 'Focused spec found.'.freeze + MSG = 'Focused spec found.'.freeze - FOCUSABLE_BLOCKS = [ - :example_group, :describe, :context, :xdescribe, :xcontext, - :it, :example, :specify, :xit, :xexample, :xspecify, - :feature, :scenario, :xfeature, :xscenario - ].freeze + FOCUSABLE_SELECTORS = ' + :context + :describe + :example + :example_group + :feature + :it + :scenario + :specify + :xcontext + :xdescribe + :xexample + :xfeature + :xit + :xscenario + :xspecify + '.freeze - FOCUSED_BLOCKS = [ - :fdescribe, :fcontext, - :focus, :fexample, :fit, :fspecify, - :ffeature, :fscenario - ].freeze + FOCUSING_SELECTORS = ' + :fcontext + :fdescribe + :fexample + :ffeature + :fit + :focus + :fscenario + :fspecify + '.freeze - FOCUS_KEY = s(:sym, :focus) + FOCUS_SYMBOL = s(:sym, :focus) + FOCUS_TRUE = s(:pair, FOCUS_SYMBOL, s(:true)) - FOCUS_TRUE_PAIR = s(:pair, FOCUS_KEY, s(:true)) + def_node_matcher :metadata, <<-PATTERN + {(send nil {#{FOCUSABLE_SELECTORS}} ... (hash $...)) + (send nil {#{FOCUSABLE_SELECTORS}} $...)} + PATTERN + def_node_matcher :focused_block?, <<-PATTERN + (send nil {#{FOCUSING_SELECTORS}} ...) + PATTERN + def on_send(node) - _receiver, method_name, *_args = *node - @focusable_block = FOCUSABLE_BLOCKS.include?(method_name) - if FOCUSED_BLOCKS.include?(method_name) - add_offense(node, :expression, MESSAGE) + focus_metadata(node) do |focus| + add_offense(focus, :expression) end - - # check for :focus - return unless @focusable_block - node.children.any? do |n| - add_offense(n, :expression, MESSAGE) if n == FOCUS_KEY - end end - def on_hash(node) - return unless @focusable_block - return if node.children.any? do |n| - if [FOCUS_TRUE_PAIR].include?(n) - add_offense(n, :expression, MESSAGE) - end + private + + def focus_metadata(node, &block) + yield(node) if focused_block?(node) + + metadata(node) do |matches| + matches.grep(FOCUS_SYMBOL, &block) + matches.grep(FOCUS_TRUE, &block) end end end end end