Sha256: 8c04f49a54fdfe6080dd5080f4ae6c62daae8ada6598749052a7eec4c8ab29f6

Contents?: true

Size: 1.47 KB

Versions: 13

Compression:

Stored size: 1.47 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, :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

13 entries across 13 versions & 1 rubygems

Version Path
rubocop-rspec-1.18.0 lib/rubocop/cop/rspec/focus.rb
rubocop-rspec-1.17.1 lib/rubocop/cop/rspec/focus.rb
rubocop-rspec-1.17.0 lib/rubocop/cop/rspec/focus.rb
rubocop-rspec-1.16.0 lib/rubocop/cop/rspec/focus.rb
rubocop-rspec-1.15.1 lib/rubocop/cop/rspec/focus.rb
rubocop-rspec-1.15.0 lib/rubocop/cop/rspec/focus.rb
rubocop-rspec-1.14.0 lib/rubocop/cop/rspec/focus.rb
rubocop-rspec-1.13.0 lib/rubocop/cop/rspec/focus.rb
rubocop-rspec-1.12.0 lib/rubocop/cop/rspec/focus.rb
rubocop-rspec-1.11.0 lib/rubocop/cop/rspec/focus.rb
rubocop-rspec-1.10.0 lib/rubocop/cop/rspec/focus.rb
rubocop-rspec-1.9.1 lib/rubocop/cop/rspec/focus.rb
rubocop-rspec-1.9.0 lib/rubocop/cop/rspec/focus.rb