class RubyDetective::AST::Nodes::GenericNode

Attributes

ast_node[R]
children[R]
file_path[R]
parent_node[R]

Public Class Methods

new(ast_node, file_path:, parent_node:) click to toggle source
# File lib/ruby_detective/ast/nodes/generic_node.rb, line 7
def initialize(ast_node, file_path:, parent_node:)
  @ast_node = ast_node
  @file_path = file_path
  @children = []
  @parent_node = parent_node
end

Public Instance Methods

absolute_path_sign_node?() click to toggle source
# File lib/ruby_detective/ast/nodes/generic_node.rb, line 38
def absolute_path_sign_node?
  type == :absolute_path_sign
end
class_declaration_node?() click to toggle source
# File lib/ruby_detective/ast/nodes/generic_node.rb, line 26
def class_declaration_node?
  type == :class
end
constant_reference_node?() click to toggle source
# File lib/ruby_detective/ast/nodes/generic_node.rb, line 34
def constant_reference_node?
  type == :constant
end
declared_namespace() click to toggle source
# File lib/ruby_detective/ast/nodes/generic_node.rb, line 22
def declared_namespace
  []
end
first_line() click to toggle source
# File lib/ruby_detective/ast/nodes/generic_node.rb, line 54
def first_line
  # When the node represents something that is not directly in the code
  # the `ast_node.loc.expression` can be nil, and since `.line` is just
  # sugar syntax for `loc.expression.line` it would throw an error.
  ast_node.loc.line rescue nil
end
generic_node?() click to toggle source
# File lib/ruby_detective/ast/nodes/generic_node.rb, line 46
def generic_node?
  type == :generic
end
last_line() click to toggle source
# File lib/ruby_detective/ast/nodes/generic_node.rb, line 61
def last_line
  # When the node represents something that is not directly in the code
  # the `ast_node.loc.expression` can be nil, and since `.last_line` is just
  # sugar syntax for `loc.expression.last_line` it would throw an error.
  ast_node.loc.last_line rescue nil
end
module_declaration_node?() click to toggle source
# File lib/ruby_detective/ast/nodes/generic_node.rb, line 30
def module_declaration_node?
  type == :module
end
namespace() click to toggle source
# File lib/ruby_detective/ast/nodes/generic_node.rb, line 18
def namespace
  build_namespace(self)
end
query() click to toggle source
# File lib/ruby_detective/ast/nodes/generic_node.rb, line 68
def query
  Query.new(self)
end
raw_children() click to toggle source
# File lib/ruby_detective/ast/nodes/generic_node.rb, line 72
def raw_children
  ast_node.children
end
short_namespace() click to toggle source
# File lib/ruby_detective/ast/nodes/generic_node.rb, line 14
def short_namespace
  namespace[0..-2]
end
type() click to toggle source
# File lib/ruby_detective/ast/nodes/generic_node.rb, line 50
def type
  :generic
end
value_node?() click to toggle source
# File lib/ruby_detective/ast/nodes/generic_node.rb, line 42
def value_node?
  type == :value
end

Private Instance Methods

build_namespace(node, acc = []) click to toggle source
# File lib/ruby_detective/ast/nodes/generic_node.rb, line 78
def build_namespace(node, acc = [])
  return acc.flatten.compact if node.nil?

  acc.prepend(node.declared_namespace)
  build_namespace(node.parent_node, acc)
end