class RubyDetective::AST::NodeFactory

Constants

NODE_TYPE_DICTIONARY

A dictionary that converts the Parser gem type to our Rich AST type

Attributes

file_path[R]
node[R]
parent_node[R]
rich_node[R]

Public Class Methods

new(node, file_path:, parent_node: nil) click to toggle source

The following types also exist:

value - the last node of a branch, can be nil, a string, a symbol, etc… generic - a broader “others” type, for any nodes not mapped out

# File lib/ruby_detective/ast/node_factory.rb, line 18
def initialize(node, file_path:, parent_node: nil)
  @node = node
  @rich_node = nil
  @file_path = file_path
  @parent_node = parent_node
end

Public Instance Methods

build() click to toggle source
# File lib/ruby_detective/ast/node_factory.rb, line 25
def build
  @rich_node = node_class.new(node, file_path: file_path, parent_node: parent_node)
end
process_all_children() click to toggle source
# File lib/ruby_detective/ast/node_factory.rb, line 29
def process_all_children
  rich_node.raw_children.each do |raw_child_node|
    factory = self.class.new(
      raw_child_node,
      file_path: file_path,
      parent_node: rich_node
    )
    child_node = factory.build

    rich_node.children << child_node
    factory.process_all_children
  end
end

Private Instance Methods

node_class() click to toggle source
# File lib/ruby_detective/ast/node_factory.rb, line 45
def node_class
  case node_type
  when :class
    Nodes::ClassDeclarationNode
  when :module
    Nodes::ModuleDeclarationNode
  when :constant
    Nodes::ConstantReferenceNode
  when :absolute_path_sign
    Nodes::AbsolutePathSignNode
  when :value
    Nodes::ValueNode
  when :generic
    Nodes::GenericNode
  end
end
node_type() click to toggle source
# File lib/ruby_detective/ast/node_factory.rb, line 62
def node_type
  if not_an_ast_node?
    :value
  elsif NODE_TYPE_DICTIONARY.key?(node.type)
    NODE_TYPE_DICTIONARY[node.type]
  else
    :generic
  end
end
not_an_ast_node?() click to toggle source
# File lib/ruby_detective/ast/node_factory.rb, line 72
def not_an_ast_node?
  !node.is_a?(Parser::AST::Node)
end