Class: RubyDetective::AST::Nodes::ClassDeclarationNode

Inherits:
GenericNode
  • Object
show all
Defined in:
lib/ruby_detective/ast/nodes/class_declaration_node.rb

Constant Summary collapse

CLASS_NAME_NODE_INDEX =
0
INHERITANCE_CLASS_NAME_NODE_INDEX =
1
PREPENDED_NAMESPACE_NODE_INDEX =
0

Instance Attribute Summary

Attributes inherited from GenericNode

#ast_node, #children, #file_path, #parent_node

Instance Method Summary collapse

Methods inherited from GenericNode

#absolute_path_sign_node?, #class_declaration_node?, #constant_reference_node?, #first_line, #generic_node?, #initialize, #last_line, #module_declaration_node?, #namespace, #query, #raw_children, #short_namespace, #value_node?

Constructor Details

This class inherits a constructor from RubyDetective::AST::Nodes::GenericNode

Instance Method Details

#class_nameObject



6
7
8
# File 'lib/ruby_detective/ast/nodes/class_declaration_node.rb', line 6

def class_name
  children[CLASS_NAME_NODE_INDEX].constant_name
end

#declared_namespaceObject



21
22
23
24
25
26
27
28
29
30
# File 'lib/ruby_detective/ast/nodes/class_declaration_node.rb', line 21

def declared_namespace
  prepended_namespace =
    children[CLASS_NAME_NODE_INDEX]
    .children[PREPENDED_NAMESPACE_NODE_INDEX]

  return [class_name] unless prepended_namespace.constant_reference_node?
  # This scenario happens when we have a class declaration with inline
  # namespace, like this: class MyNamespace::MyClass
  prepended_namespace.constant_path + [class_name]
end

#inheritance_class_nameObject



11
12
13
14
15
16
17
18
# File 'lib/ruby_detective/ast/nodes/class_declaration_node.rb', line 11

def inheritance_class_name
  inherited_class_constant = children[INHERITANCE_CLASS_NAME_NODE_INDEX]
  # If this child isn't a ConstantReference node it means it doesn't
  # have a declared class inheritance like "class Foo::Bar"
  return unless inherited_class_constant.constant_reference_node?

  inherited_class_constant.constant_name
end

#typeObject



32
33
34
# File 'lib/ruby_detective/ast/nodes/class_declaration_node.rb', line 32

def type
  :class
end