# frozen_string_literal: true

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

        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| spec_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
end

# frozen_string_literal: true

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

        def on_new_investigation
          super

          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| spec_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)
          return [] if node.nil?

          case node.type
          when :begin
            node.children
          when :module, :class
            top_level_nodes(node.body)
          else
            [node]
          end
        end

        def root_node
          processed_source.ast
        end
      end
    end
  end
end

Use `case` instead of `if`
Use `case` instead of `if`

It reads nicer, and `flog` agrees. Before:

❯ flog lib/rubocop/cop/rspec/mixin/top_level_group.rb
    29.7: flog total
     5.0: flog/method average

     9.8: RuboCop::Cop::RSpec::TopLevelGroup#top_level_nodes lib/rubocop/cop/rspec/mixin/top_level_group.rb:36-44
     7.6: RuboCop::Cop::RSpec::TopLevelGroup#on_new_investigation lib/rubocop/cop/rspec/mixin/top_level_group.rb:10-16
     4.9: RuboCop::Cop::RSpec::TopLevelGroup#top_level_groups lib/rubocop/cop/rspec/mixin/top_level_group.rb:20-22

After:

❯ flog lib/rubocop/cop/rspec/mixin/top_level_group.rb
    25.8: flog total
     4.3: flog/method average

     7.2: RuboCop::Cop::RSpec::TopLevelGroup#top_level_nodes lib/rubocop/cop/rspec/mixin/top_level_group.rb:35-44
     6.3: RuboCop::Cop::RSpec::TopLevelGroup#on_new_investigation lib/rubocop/cop/rspec/mixin/top_level_group.rb:10-15
     4.9: RuboCop::Cop::RSpec::TopLevelGroup#top_level_groups lib/rubocop/cop/rspec/mixin/top_level_group.rb:19-21
