Sha256: 9d6244cebdd432d140195695c7016d460d0bdce84fcea87adc65257a30048af0

Contents?: true

Size: 1.04 KB

Versions: 3

Compression:

Stored size: 1.04 KB

Contents

module RuboCop
  module Cop
    module Mirego
      class ReceiveCounts < Cop
        MESSAGE = 'Always specify a receive count with `receive`'.freeze
        RECEIVE_COUNTS_METHODS = %i(once twice exactly at_least at_most).freeze

        def on_send(node)
          receiver, method_name, *args = *node
          return nil unless method_name == :to
          return nil if receiver.nil? || receiver.to_a[1] != :expect

          methods_name = recursive_methods_name(nodes: args)
          return nil unless methods_name.include? :receive
          return nil unless (methods_name & RECEIVE_COUNTS_METHODS).empty?

          add_offense(node, :expression, MESSAGE)
        end

      protected

        def recursive_methods_name(nodes:)
          methods_name = []
          nodes.each do |node|
            methods_name << node.children[1]

            until node.children[0].nil?
              node = node.children[0]
              methods_name << node.children[1]
            end
          end

          methods_name
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rubocop-mirego-0.1 lib/rubocop/cop/mirego/receive_counts.rb
rubocop-mirego-0.0.4 lib/rubocop/cop/mirego/receive_counts.rb
rubocop-mirego-0.0.3 lib/rubocop/cop/mirego/receive_counts.rb