class McCabe::Parser

Public Class Methods

collect_methods(ast) click to toggle source

Collect all the methods from the AST.

# File lib/mccabe/parser.rb, line 6
def self.collect_methods(ast)
  methods = {}
  nodes = [ast].compact
  until nodes.empty?
    node = nodes.shift
    case node.type
    when :def, :defs
      name = node.children.length == 4 ? node.children[1] : node.children.first
      methods[name] = {body: node.children.last, line: node.loc.line}
    when :class, :module, :begin
      # Have to put node.children in an array and flatten in because the parser
      # returns a frozen array.
      nodes += node.children.select { |child| child.is_a? ::Parser::AST::Node }
    end
  end
  methods
end