Sha256: 0c9cbb6bf85068521719311c624a1ecae1b19936ed1602873ed6bb35ace85c11

Contents?: true

Size: 1.65 KB

Versions: 39

Compression:

Stored size: 1.65 KB

Contents

# frozen_string_literal: true

module RuboCop
  module RSpec
    # Wrapper for RSpec example groups
    class ExampleGroup < Concept
      # @!method scope_change?(node)
      #
      #   Detect if the node is an example group or shared example
      #
      #   Selectors which indicate that we should stop searching
      #
      def_node_matcher :scope_change?, <<~PATTERN
        (block {
          (send #rspec? {#SharedGroups.all #ExampleGroups.all} ...)
          (send nil? #Includes.all ...)
        } ...)
      PATTERN

      def lets
        find_all_in_scope(node, :let?)
      end

      def subjects
        find_all_in_scope(node, :subject?)
      end

      def examples
        find_all_in_scope(node, :example?).map do |node|
          Example.new(node)
        end
      end

      def hooks
        find_all_in_scope(node, :hook?).map do |node|
          Hook.new(node)
        end
      end

      private

      # Recursively search for predicate within the current scope
      #
      # Searches node and halts when a scope change is detected
      #
      # @param node [RuboCop::AST::Node] node to recursively search
      # @param predicate [Symbol] method to call with node as argument
      #
      # @return [Array<RuboCop::AST::Node>] discovered nodes
      def find_all_in_scope(node, predicate)
        node.each_child_node.flat_map do |child|
          find_all(child, predicate)
        end
      end

      def find_all(node, predicate)
        if public_send(predicate, node)
          [node]
        elsif scope_change?(node) || example?(node)
          []
        else
          find_all_in_scope(node, predicate)
        end
      end
    end
  end
end

Version data entries

39 entries across 39 versions & 7 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-3.0.3/lib/rubocop/rspec/example_group.rb
rubocop-rspec-3.3.0 lib/rubocop/rspec/example_group.rb
rubocop-rspec-3.2.0 lib/rubocop/rspec/example_group.rb
rubocop-rspec-3.1.0 lib/rubocop/rspec/example_group.rb
rubocop-rspec-3.0.5 lib/rubocop/rspec/example_group.rb
rubocop-rspec-3.0.4 lib/rubocop/rspec/example_group.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-3.0.1/lib/rubocop/rspec/example_group.rb
rubocop-rspec-3.0.3 lib/rubocop/rspec/example_group.rb
rubocop-rspec-3.0.2 lib/rubocop/rspec/example_group.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-rspec-3.0.1/lib/rubocop/rspec/example_group.rb
rubocop-rspec-3.0.1 lib/rubocop/rspec/example_group.rb
rubocop-rspec-3.0.0 lib/rubocop/rspec/example_group.rb
rubocop-rspec-3.0.0.pre lib/rubocop/rspec/example_group.rb
rubocop-rspec-2.31.0 lib/rubocop/rspec/example_group.rb
rubocop-rspec-2.30.0 lib/rubocop/rspec/example_group.rb
rubocop-rspec-2.29.2 lib/rubocop/rspec/example_group.rb
rubocop-rspec-2.29.1 lib/rubocop/rspec/example_group.rb
rubocop-rspec-2.29.0 lib/rubocop/rspec/example_group.rb
rubocop-rspec-2.28.0 lib/rubocop/rspec/example_group.rb
rubocop-rspec-2.27.1 lib/rubocop/rspec/example_group.rb