Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions lib/packwerk/const_node_inspector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ def constant_name_from_node(node, ancestors:, relative_file:)
# reference `Spam::Eggs::Thing`, we only process the const node associated with `Spam`.
return nil unless root_constant?(parent)

if parent && constant_in_module_or_class_definition?(node, parent: parent)
fully_qualify_constant(ancestors)
else
begin
begin
if parent && constant_in_module_or_class_definition?(node, parent: parent)
fully_qualify_constant(ancestors)
else
NodeHelpers.constant_name(node)
rescue NodeHelpers::TypeError
nil
end
rescue NodeHelpers::TypeError
nil
end
end

Expand Down
18 changes: 18 additions & 0 deletions test/unit/packwerk/const_node_inspector_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,24 @@ class ConstNodeInspectorTest < ActiveSupport::TestCase
assert_nil constant_name
end

test "#constant_name_from_node should return nil for a superclass with a dynamic namespace" do
parent = parse("class Child < module_parent::Base; end")
node = parent.children[1]

constant_name = @inspector.constant_name_from_node(node, ancestors: [parent], relative_file: "")

assert_nil constant_name
end

test "#constant_name_from_node should return nil for a class name with a dynamic namespace" do
parent = parse("class module_parent::Foo; end")
node = parent.children[0]

constant_name = @inspector.constant_name_from_node(node, ancestors: [parent], relative_file: "")

assert_nil constant_name
end

private

def parse(code)
Expand Down
Loading