class CustomCops::TimecopWithoutBlock

Constants

MSG

Public Instance Methods

on_send(node) click to toggle source
# File lib/simplycop/custom_cops/timecop_without_block.rb, line 9
def on_send(node)
  timecop_method(node) do |method_name|
    return if !method_name || first_child_of_block?(node) || last_child_is_a_block(node)

    add_offense(node, location: :selector, message: format(MSG, method: method_name))
  end
end

Private Instance Methods

first_child_of_block?(node) click to toggle source

Checks if the given node's parent is a block, and the given node is its first child, which would mean that the block is supplied to the given node (i.e `node { block }`)

# File lib/simplycop/custom_cops/timecop_without_block.rb, line 21
def first_child_of_block?(node)
  return false unless (parent = node.parent)

  return false unless parent.type == :block

  parent.children.first == node
end
last_child_is_a_block(node) click to toggle source

Checks whether the last child of the given node is a block. this denotes the following structure: `Timecop.method(arg1, arg2, &block)`, which is also a valid way of passing in a block

# File lib/simplycop/custom_cops/timecop_without_block.rb, line 32
def last_child_is_a_block(node)
  node.children.last&.type == :block_pass
end