Class: McCabe::Parser

Inherits:
Parser::CurrentRuby
  • Object
show all
Defined in:
lib/mccabe/parser.rb

Class Method Summary collapse

Class Method Details

.collect_methods(ast) ⇒ Object

Collect all the methods from the AST.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 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