Sha256: a90ff90d4459e2f5de305e0813ee1b6bc94650a4f4064e7796e53c600668df9f

Contents?: true

Size: 1.41 KB

Versions: 2

Compression:

Stored size: 1.41 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 < Base
        MSG = 'Focused spec found.'

        def_node_matcher :focusable_selector?, <<-PATTERN
          {
            #ExampleGroups.regular
            #ExampleGroups.skipped
            #Examples.regular
            #Examples.skipped
            #Examples.pending
          }
        PATTERN

        def_node_matcher :metadata, <<-PATTERN
          {(send #rspec? #focusable_selector? <$(sym :focus) ...>)
           (send #rspec? #focusable_selector? ... (hash <$(pair (sym :focus) true) ...>))}
        PATTERN

        def_node_matcher :focused_block?,
                         send_pattern(<<~PATTERN)
                           {#ExampleGroups.focused #Examples.focused}
                         PATTERN

        def on_send(node)
          focus_metadata(node) do |focus|
            add_offense(focus)
          end
        end

        private

        def focus_metadata(node, &block)
          yield(node) if focused_block?(node)

          metadata(node, &block)
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rubocop-rspec-2.0.1 lib/rubocop/cop/rspec/focus.rb
rubocop-rspec-2.0.0 lib/rubocop/cop/rspec/focus.rb