Skip to content

Commit 8410e30

Browse files
committed
clarify code of ConstNodeInspector
1 parent 84e1f60 commit 8410e30

File tree

3 files changed

+34
-21
lines changed

3 files changed

+34
-21
lines changed

lib/packwerk.rb

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
require "packwerk/configuration"
1515
require "packwerk/const_node_inspector"
1616
require "packwerk/constant_discovery"
17-
require "packwerk/constant_name_inspector"
1817
require "packwerk/dependency_checker"
1918
require "packwerk/deprecated_references"
2019
require "packwerk/files_for_processing"

lib/packwerk/const_node_inspector.rb

+28-18
Original file line numberDiff line numberDiff line change
@@ -9,36 +9,46 @@ class ConstNodeInspector
99
include ConstantNameInspector
1010

1111
def constant_name_from_node(node, ancestors:)
12-
return nil unless Node.type(node) == Node::CONSTANT
13-
12+
return nil unless Node.constant?(node)
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.type(parent) == Node::CONSTANT
18-
19-
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("::")
15+
return nil if root_constant?(ancestors)
2416

25-
"::" + name
17+
if constant_in_module_or_class_definition?(node, ancestors: ancestors)
18+
fully_qualify_constant(node, ancestors: ancestors)
2619
else
27-
begin
28-
Node.constant_name(node)
29-
rescue Node::TypeError
30-
nil
31-
end
20+
Node.constant_name(node)
3221
end
22+
rescue Node::TypeError
23+
nil
3324
end
3425

3526
private
3627

37-
def constant_in_module_or_class_definition?(node, parent:)
38-
if parent
28+
def root_constant?(ancestors)
29+
parent = ancestors.first
30+
parent && Node.constant?(parent)
31+
end
32+
33+
def constant_in_module_or_class_definition?(node, ancestors:)
34+
if (parent = ancestors.first)
3935
parent_name = Node.module_name_from_definition(parent)
4036
parent_name && parent_name == Node.constant_name(node)
4137
end
4238
end
39+
40+
def fully_qualify_constant(node, ancestors:)
41+
# We're defining a class with this name, in which case the constant is implicitly fully qualified by its
42+
# enclosing namespace
43+
name = Node.parent_module_name(ancestors: ancestors)
44+
name ||= generate_qualified_constant(node, ancestors)
45+
"::" + name
46+
end
47+
48+
def generate_qualified_constant(node, ancestors:)
49+
namespace_path = Node.enclosing_namespace_path(node, ancestors: ancestors)
50+
constant_name = Node.constant_name(node)
51+
namespace_path.push(constant_name).join("::")
52+
end
4353
end
4454
end

lib/packwerk/node.rb

+6-2
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ def location(node)
103103
Location.new(location.line, location.column)
104104
end
105105

106+
def constant?(node)
107+
type(node) == CONSTANT
108+
end
109+
106110
def method_arguments(method_call_node)
107111
raise TypeError unless type(method_call_node) == METHOD_CALL
108112

@@ -223,15 +227,15 @@ def module_creation?(node)
223227
# "Class.new"
224228
# "Module.new"
225229
type(node) == METHOD_CALL &&
226-
type(receiver(node)) == CONSTANT &&
230+
constant?(receiver(node)) &&
227231
["Class", "Module"].include?(constant_name(receiver(node))) &&
228232
method_name(node) == :new
229233
end
230234

231235
def name_from_block_definition(node)
232236
if method_name(method_call_node(node)) == :class_eval
233237
receiver = receiver(node)
234-
constant_name(receiver) if receiver && type(receiver) == CONSTANT
238+
constant_name(receiver) if receiver && constant?(receiver)
235239
end
236240
end
237241

0 commit comments

Comments
 (0)