Skip to content

Commit 9b6e61d

Browse files
committed
access parent through ancestors
I'm removing the parent variable here and access the parent ancestor directly through the ancestors variable. This allows me to move the check if a node has a root constant fully into a private method.
1 parent 9fafd20 commit 9b6e61d

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

lib/packwerk/const_node_inspector.rb

+9-6
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,11 @@ class ConstNodeInspector
1010

1111
def constant_name_from_node(node, ancestors:)
1212
return nil unless Node.constant?(node)
13-
1413
# Only process the root `const` node for namespaced constant references. For example, in the
1514
# reference `Spam::Eggs::Thing`, we only process the const node associated with `Spam`.
16-
parent = ancestors.first
17-
return nil if parent && Node.constant?(parent)
15+
return nil if root_constant?(ancestors)
1816

19-
if constant_in_module_or_class_definition?(node, parent: parent)
17+
if constant_in_module_or_class_definition?(node, ancestors: ancestors)
2018
fully_qualify_constant(node, ancestors: ancestors)
2119
else
2220
begin
@@ -29,8 +27,13 @@ def constant_name_from_node(node, ancestors:)
2927

3028
private
3129

32-
def constant_in_module_or_class_definition?(node, parent:)
33-
if parent
30+
def root_constant?(ancestors)
31+
parent = ancestors.first
32+
parent && Node.constant?(parent)
33+
end
34+
35+
def constant_in_module_or_class_definition?(node, ancestors:)
36+
if (parent = ancestors.first)
3437
parent_name = Node.module_name_from_definition(parent)
3538
parent_name && parent_name == Node.constant_name(node)
3639
end

0 commit comments

Comments
 (0)