Sha256: 2289a2659e1f315b33226b7c68894019b96abf8b5f17b3ffe39d0b417fa717ed

Contents?: true

Size: 1.3 KB

Versions: 4

Compression:

Stored size: 1.3 KB

Contents

# frozen_string_literal: true

module RuboCop
  module RSpec
    # Helper methods for top level example group cops
    module TopLevelGroup
      extend RuboCop::NodePattern::Macros
      include RuboCop::RSpec::Language

      def_node_matcher :example_or_shared_group?,
                       (ExampleGroups::ALL + SharedGroups::ALL).block_pattern

      def on_new_investigation
        super

        return unless root_node

        top_level_groups.each do |node|
          on_top_level_example_group(node) if example_group?(node)
          on_top_level_group(node)
        end
      end

      def top_level_groups
        @top_level_groups ||=
          top_level_nodes(root_node).select { |n| example_or_shared_group?(n) }
      end

      private

      # Dummy methods to be overridden in the consumer
      def on_top_level_example_group(_node); end

      def on_top_level_group(_node); end

      def top_level_group?(node)
        top_level_groups.include?(node)
      end

      def top_level_nodes(node)
        if node.nil?
          []
        elsif node.begin_type?
          node.children
        elsif node.module_type? || node.class_type?
          top_level_nodes(node.body)
        else
          [node]
        end
      end

      def root_node
        processed_source.ast
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rubocop-rspec-2.0.0.pre lib/rubocop/rspec/top_level_group.rb
rubocop-rspec-1.44.1 lib/rubocop/rspec/top_level_group.rb
rubocop-rspec-1.44.0 lib/rubocop/rspec/top_level_group.rb
rubocop-rspec-1.43.2 lib/rubocop/rspec/top_level_group.rb