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