Skip to content

Commit 71cee51

Browse files
committed
cleanup guard method to have conditional in private method
1 parent f9941e7 commit 71cee51

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

lib/packwerk/const_node_inspector.rb

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

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

19-
if constant_in_module_or_class_definition?(node, parent: parent)
16+
if parent && constant_in_module_or_class_definition?(node, parent: parent)
2017
fully_qualify_constant(node, ancestors: ancestors)
2118
else
2219
begin
@@ -29,11 +26,15 @@ def constant_name_from_node(node, ancestors:)
2926

3027
private
3128

29+
# Only process the root `const` node for namespaced constant references. For example, in the
30+
# reference `Spam::Eggs::Thing`, we only process the const node associated with `Spam`.
31+
def root_constant?(parent)
32+
!parent || !Node.constant?(parent)
33+
end
34+
3235
def constant_in_module_or_class_definition?(node, parent:)
33-
if parent
34-
parent_name = Node.module_name_from_definition(parent)
35-
parent_name && parent_name == Node.constant_name(node)
36-
end
36+
parent_name = Node.module_name_from_definition(parent)
37+
parent_name && parent_name == Node.constant_name(node)
3738
end
3839

3940
def fully_qualify_constant(node, ancestors:)

0 commit comments

Comments
 (0)