class Packwerk::ConstNodeInspector
Extracts a constant name from an AST node of type :const
Public Instance Methods
constant_name_from_node(node, ancestors:)
click to toggle source
# File lib/packwerk/const_node_inspector.rb, line 15 def constant_name_from_node(node, ancestors:) return nil unless Node.constant?(node) parent = ancestors.first return nil unless root_constant?(parent) if parent && constant_in_module_or_class_definition?(node, parent: parent) fully_qualify_constant(ancestors) else begin Node.constant_name(node) rescue Node::TypeError nil end end end
Private Instance Methods
constant_in_module_or_class_definition?(node, parent:)
click to toggle source
# File lib/packwerk/const_node_inspector.rb, line 41 def constant_in_module_or_class_definition?(node, parent:) parent_name = Node.module_name_from_definition(parent) parent_name && parent_name == Node.constant_name(node) end
fully_qualify_constant(ancestors)
click to toggle source
# File lib/packwerk/const_node_inspector.rb, line 47 def fully_qualify_constant(ancestors) # We're defining a class with this name, in which case the constant is implicitly fully qualified by its # enclosing namespace "::" + Node.parent_module_name(ancestors: ancestors) end
root_constant?(parent)
click to toggle source
# File lib/packwerk/const_node_inspector.rb, line 36 def root_constant?(parent) !(parent && Node.constant?(parent)) end