Skip to content

Commit dac7b10

Browse files
authored
Merge pull request #39 from Shopify/clarify_const_node_inspector
Clarify ConstNodeInspector code
2 parents 3fa7df4 + ea2e68a commit dac7b10

File tree

3 files changed

+32
-19
lines changed

3 files changed

+32
-19
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

+26-16
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,12 @@ class ConstNodeInspector
99
include ConstantNameInspector
1010

1111
def constant_name_from_node(node, ancestors:)
12-
return nil unless Node.type(node) == Node::CONSTANT
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`.
12+
return nil unless Node.constant?(node)
1613
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("::")
14+
return nil unless root_constant?(parent)
2415

25-
"::" + name
16+
if parent && constant_in_module_or_class_definition?(node, parent: parent)
17+
fully_qualify_constant(node, ancestors: ancestors)
2618
else
2719
begin
2820
Node.constant_name(node)
@@ -34,11 +26,29 @@ def constant_name_from_node(node, ancestors:)
3426

3527
private
3628

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+
3735
def constant_in_module_or_class_definition?(node, parent:)
38-
if parent
39-
parent_name = Node.module_name_from_definition(parent)
40-
parent_name && parent_name == Node.constant_name(node)
41-
end
36+
parent_name = Node.module_name_from_definition(parent)
37+
parent_name && parent_name == Node.constant_name(node)
38+
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("::")
4252
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_call?(node)
107111
type(node) == METHOD_CALL
108112
end
@@ -239,15 +243,15 @@ def module_creation?(node)
239243
# "Class.new"
240244
# "Module.new"
241245
method_call?(node) &&
242-
type(receiver(node)) == CONSTANT &&
246+
constant?(receiver(node)) &&
243247
["Class", "Module"].include?(constant_name(receiver(node))) &&
244248
method_name(node) == :new
245249
end
246250

247251
def name_from_block_definition(node)
248252
if method_name(method_call_node(node)) == :class_eval
249253
receiver = receiver(node)
250-
constant_name(receiver) if receiver && type(receiver) == CONSTANT
254+
constant_name(receiver) if receiver && constant?(receiver)
251255
end
252256
end
253257

0 commit comments

Comments
 (0)