Class: Parser::AST::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/rubycritic/analysers/helpers/ast_node.rb

Constant Summary collapse

MODULE_TYPES =
%i[module class].freeze

Instance Method Summary collapse

Instance Method Details

#count_nodes_of_type(*types) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/rubycritic/analysers/helpers/ast_node.rb', line 8

def count_nodes_of_type(*types)
  count = 0
  recursive_children do |child|
    count += 1 if types.include?(child.type)
  end
  count
end

#module_namesObject



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rubycritic/analysers/helpers/ast_node.rb', line 25

def module_names
  ast_node_children = children.select do |child|
    child.is_a?(Parser::AST::Node)
  end

  children_modules = ast_node_children.flat_map(&:module_names)

  if MODULE_TYPES.include?(type)
    module_names_with_children children_modules
  else
    children_modules
  end
end

#recursive_childrenObject



16
17
18
19
20
21
22
23
# File 'lib/rubycritic/analysers/helpers/ast_node.rb', line 16

def recursive_children
  children.each do |child|
    next unless child.is_a?(Parser::AST::Node)

    yield child
    child.recursive_children { |grand_child| yield grand_child }
  end
end