Skip to content

Commit f9941e7

Browse files
committed
add fully_qualify_constant private method
I'm moving a chunk of code out of the constant_name_from_node method. This makes the intend clearer.
1 parent 010828b commit f9941e7

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

lib/packwerk/const_node_inspector.rb

+15-6
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,7 @@ def constant_name_from_node(node, ancestors:)
1717
return nil if parent && Node.constant?(parent)
1818

1919
if constant_in_module_or_class_definition?(node, parent: parent)
20-
# We're defining a class with this name, in which case the constant is implicitly fully qualified by its
21-
# enclosing namespace
22-
name = Node.parent_module_name(ancestors: ancestors)
23-
name ||= Node.enclosing_namespace_path(node, ancestors: ancestors).push(Node.constant_name(node)).join("::")
24-
25-
"::" + name
20+
fully_qualify_constant(node, ancestors: ancestors)
2621
else
2722
begin
2823
Node.constant_name(node)
@@ -40,5 +35,19 @@ def constant_in_module_or_class_definition?(node, parent:)
4035
parent_name && parent_name == Node.constant_name(node)
4136
end
4237
end
38+
39+
def fully_qualify_constant(node, ancestors:)
40+
# We're defining a class with this name, in which case the constant is implicitly fully qualified by its
41+
# enclosing namespace
42+
name = Node.parent_module_name(ancestors: ancestors)
43+
name ||= generate_qualified_constant(node, ancestors)
44+
"::" + name
45+
end
46+
47+
def generate_qualified_constant(node, ancestors:)
48+
namespace_path = Node.enclosing_namespace_path(node, ancestors: ancestors)
49+
constant_name = Node.constant_name(node)
50+
namespace_path.push(constant_name).join("::")
51+
end
4352
end
4453
end

0 commit comments

Comments
 (0)