class Ducalis::PossibleTap

Constants

ASSIGNS
DETAILS
OFFENSE
PAIRS

Public Instance Methods

on_def(node) click to toggle source
# File lib/ducalis/cops/possible_tap.rb, line 24
def on_def(node)
  _name, _args, body = *node
  return if body.nil?
  return unless (possibe_var = return_var?(body) || return_var_call?(body))
  return unless (assign_node = find_assign(body, possibe_var))

  add_offense(assign_node, :expression, OFFENSE)
end

Private Instance Methods

find_assign(body, var_node) click to toggle source
# File lib/ducalis/cops/possible_tap.rb, line 39
def find_assign(body, var_node)
  subnodes(body).find do |subnode|
    unwrap_assign(subnode).type == PAIRS[var_node.type] &&
      unwrap_assign(subnode).to_a.first == var_node.to_a.first
  end
end
last_child(body) click to toggle source
# File lib/ducalis/cops/possible_tap.rb, line 68
def last_child(body)
  body.children.last
end
return_var?(body) click to toggle source
# File lib/ducalis/cops/possible_tap.rb, line 46
def return_var?(body)
  return unless body.children.last.respond_to?(:type)
  return unless ASSIGNS.include?(body.children.last.type)

  body.children.last
end
return_var_call?(body) click to toggle source
# File lib/ducalis/cops/possible_tap.rb, line 53
def return_var_call?(body)
  return unless last_child(body).respond_to?(:children)
  return if last_child(body).type == :if

  subnodes(last_child(body).to_a.first).find do |node|
    ASSIGNS.include?(node.type)
  end
end
subnodes(node) click to toggle source
# File lib/ducalis/cops/possible_tap.rb, line 62
def subnodes(node)
  return [] unless node.respond_to?(:children)

  ([node] + node.children).select { |child| child.respond_to?(:type) }
end
unwrap_assign(node) click to toggle source
# File lib/ducalis/cops/possible_tap.rb, line 35
def unwrap_assign(node)
  node.type == :or_asgn ? node.children.first : node
end