Sha256: dc7d962dcbee994a2429be4b51b690cbea69e6b2cef0c967dd3857395d0f7925

Contents?: true

Size: 1.64 KB

Versions: 2

Compression:

Stored size: 1.64 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
        include RuboCop::RSpec::SpecOnly, RuboCop::RSpec::Language

        MSG = 'Focused spec found.'.freeze

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

        focused = ExampleGroups::FOCUSED + Examples::FOCUSED

        FOCUSABLE_SELECTORS = focusable.to_node_pattern
        FOCUSING_SELECTORS  = focused.to_node_pattern

        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?, <<-PATTERN
          (send nil {#{FOCUSING_SELECTORS}} ...)
        PATTERN

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

2 entries across 2 versions & 1 rubygems

Version Path
rubocop-rspec-1.8.0 lib/rubocop/cop/rspec/focus.rb
rubocop-rspec-1.7.0 lib/rubocop/cop/rspec/focus.rb