Skip to content

Commit d24769d

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

7 files changed

+42
-29
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/association_inspector.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
# typed: true
22
# frozen_string_literal: true
33

4-
require "packwerk/constant_name_inspector"
4+
require "packwerk/constant_name_inspector_interface"
55
require "packwerk/node"
66

77
module Packwerk
88
# Extracts the implicit constant reference from an active record association
99
class AssociationInspector
10-
include ConstantNameInspector
10+
include ConstantNameInspectorInterface
1111

1212
RAILS_ASSOCIATIONS = %i(
1313
belongs_to

lib/packwerk/const_node_inspector.rb

+30-20
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,54 @@
11
# typed: true
22
# frozen_string_literal: true
33

4-
require "packwerk/constant_name_inspector"
4+
require "packwerk/constant_name_inspector_interface"
55

66
module Packwerk
77
# Extracts a constant name from an AST node of type :const
88
class ConstNodeInspector
9-
include ConstantNameInspector
9+
include ConstantNameInspectorInterface
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/constant_name_inspector.rb lib/packwerk/constant_name_inspector_interface.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
module Packwerk
88
# An interface describing some object that can extract a constant name from an AST node
9-
module ConstantNameInspector
9+
module ConstantNameInspectorInterface
1010
extend T::Sig
1111
extend T::Helpers
1212

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

lib/packwerk/reference_extractor.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
require "sorbet-runtime"
55

66
require "packwerk/constant_discovery"
7-
require "packwerk/constant_name_inspector"
7+
require "packwerk/constant_name_inspector_interface"
88
require "packwerk/node"
99
require "packwerk/parsed_constant_definitions"
1010
require "packwerk/reference"
@@ -17,7 +17,7 @@ class ReferenceExtractor
1717
sig do
1818
params(
1919
context_provider: Packwerk::ConstantDiscovery,
20-
constant_name_inspectors: T::Array[Packwerk::ConstantNameInspector],
20+
constant_name_inspectors: T::Array[Packwerk::ConstantNameInspectorInterface],
2121
root_node: ::AST::Node,
2222
root_path: String,
2323
).void

test/unit/reference_extractor_test.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ def setup
198198
private
199199

200200
class DummyAssociationInspector
201-
include Packwerk::ConstantNameInspector
201+
include Packwerk::ConstantNameInspectorInterface
202202

203203
def initialize(association: false, reference_name: "Dummy", expected_args: nil)
204204
@association = association

0 commit comments

Comments
 (0)