module Niceness

Niceness of a node means that it cannot be nil.

Note that the module depends on the includer to provide scope (for nice_variable)

Constants

NICE_GLOBAL_METHODS

Methods that preserve niceness if all their arguments are nice These are global, called with a nil receiver

NICE_LITERAL_NODE_TYPES

Literals are nice, except the nil literal.

NICE_OPERATORS

Public Instance Methods

nice(node) click to toggle source
# File lib/zombie_killer/niceness.rb, line 21
def nice(node)
  nice_literal(node) || nice_variable(node) || nice_send(node) ||
    nice_begin(node)
end
nice_begin(node) click to toggle source
# File lib/zombie_killer/niceness.rb, line 59
def nice_begin(node)
  node.type == :begin && nice(node.children.last)
end
nice_literal(node) click to toggle source
# File lib/zombie_killer/niceness.rb, line 26
def nice_literal(node)
  NICE_LITERAL_NODE_TYPES.include? node.type
end
nice_send(node) click to toggle source
# File lib/zombie_killer/niceness.rb, line 46
def nice_send(node)
  return false unless node.type == :send
  receiver, message, *args = *node

  if receiver.nil?
    arity = NICE_GLOBAL_METHODS.fetch(message, -1)
  else
    return false unless nice(receiver)
    arity = NICE_OPERATORS.fetch(message, -1)
  end
  args.size == arity && args.all? { |a| nice(a) }
end
nice_variable(node) click to toggle source
# File lib/zombie_killer/niceness.rb, line 30
def nice_variable(node)
  node.type == :lvar && scope[node.children.first].nice
end