Class: Parser::AST::Node

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

Constant Summary collapse

MODULE_TYPES =
[:module, :class].freeze

Instance Method Summary collapse

Instance Method Details

#count_nodes_of_type(*types) ⇒ Object



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

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

#get_module_namesObject



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

def get_module_names
  children_modules = children
                     .select { |child| child.is_a?(Parser::AST::Node) }
                     .flat_map(&:get_module_names)

  if MODULE_TYPES.include?(type)
    if children_modules.empty?
      [module_name]
    else
      children_modules.map do |children_module|
        "#{module_name}::#{children_module}"
      end
    end
  else
    children_modules
  end
end

#recursive_childrenObject



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

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