module RuboCop
  module Cop
    module Rouge
      class NoHugeCollections < RuboCop::Cop::Base
        MSG = 'Avoid collections of over 300 elements. ' \
              'Place those in a lazy-loaded file, ideally ' \
              'generated by a Rake task from the relevant documentation.'


        def_node_matcher :set_literal?, <<~PATTERN
          (send (const nil? :Set) :[] ...)
        PATTERN

        RESTRICT_ON_SEND = [:[]]
        MAX_SIZE = 300

        Set["1", "2", "3"]
        Set.new %w(1 2 3)

        def on_send(node)
          return unless set_literal?(node)

          # adjust by 2 for the target and method name
          if node.children.size - 2 > MAX_SIZE
            add_offense(node)
          end
        end

        def on_array(node)
          if node.children.size >= MAX_SIZE
            add_offense(node)
          end
        end
      end
    end
  end
end
