Sha256: 40bf344b905f2533a8ea12efeac1941173b8599a36482fb8766a6b1d62956d2f

Contents?: true

Size: 1.48 KB

Versions: 19

Compression:

Stored size: 1.48 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RSpec
      # Checks if examples are focused.
      #
      # @example
      #   # bad
      #   describe MyClass, focus: true do
      #   end
      #
      #   describe MyClass, :focus do
      #   end
      #
      #   fdescribe MyClass do
      #   end
      #
      #   # good
      #   describe MyClass do
      #   end
      class Focus < Cop
        MSG = 'Focused spec found.'.freeze

        focusable =
          ExampleGroups::GROUPS  +
          ExampleGroups::SKIPPED +
          Examples::EXAMPLES     +
          Examples::SKIPPED

        focused = ExampleGroups::FOCUSED + Examples::FOCUSED

        FOCUSABLE_SELECTORS = focusable.node_pattern_union

        FOCUS_SYMBOL = s(:sym, :focus)
        FOCUS_TRUE   = s(:pair, FOCUS_SYMBOL, s(:true))

        def_node_matcher :metadata, <<-PATTERN
          {(send nil? #{FOCUSABLE_SELECTORS} ... (hash $...))
           (send nil? #{FOCUSABLE_SELECTORS} $...)}
        PATTERN

        def_node_matcher :focused_block?, focused.send_pattern

        def on_send(node)
          focus_metadata(node) do |focus|
            add_offense(focus, location: :expression)
          end
        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
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
rubocop-rspec-1.31.0 lib/rubocop/cop/rspec/focus.rb
rubocop-rspec-1.30.1 lib/rubocop/cop/rspec/focus.rb
rubocop-rspec-1.30.0 lib/rubocop/cop/rspec/focus.rb
rubocop-rspec-1.29.1 lib/rubocop/cop/rspec/focus.rb
rubocop-rspec-1.29.0 lib/rubocop/cop/rspec/focus.rb
rubocop-rspec-1.28.0 lib/rubocop/cop/rspec/focus.rb
rubocop-rspec-1.27.0 lib/rubocop/cop/rspec/focus.rb
rubocop-rspec-1.26.0 lib/rubocop/cop/rspec/focus.rb
rubocop-rspec-1.25.1 lib/rubocop/cop/rspec/focus.rb
rubocop-rspec-1.25.0 lib/rubocop/cop/rspec/focus.rb
rubocop-rspec-1.24.0 lib/rubocop/cop/rspec/focus.rb
rubocop-rspec-1.23.0 lib/rubocop/cop/rspec/focus.rb
rubocop-rspec-1.22.2 lib/rubocop/cop/rspec/focus.rb
rubocop-rspec-1.22.1 lib/rubocop/cop/rspec/focus.rb
rubocop-rspec-1.22.0 lib/rubocop/cop/rspec/focus.rb
rubocop-rspec-1.21.0 lib/rubocop/cop/rspec/focus.rb
rubocop-rspec-1.20.1 lib/rubocop/cop/rspec/focus.rb
rubocop-rspec-1.20.0 lib/rubocop/cop/rspec/focus.rb
rubocop-rspec-1.19.0 lib/rubocop/cop/rspec/focus.rb