diff --git a/lib/packwerk/const_node_inspector.rb b/lib/packwerk/const_node_inspector.rb index b4290307..87b20a04 100644 --- a/lib/packwerk/const_node_inspector.rb +++ b/lib/packwerk/const_node_inspector.rb @@ -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 diff --git a/test/unit/packwerk/const_node_inspector_test.rb b/test/unit/packwerk/const_node_inspector_test.rb index d76b4a35..b2a06624 100644 --- a/test/unit/packwerk/const_node_inspector_test.rb +++ b/test/unit/packwerk/const_node_inspector_test.rb @@ -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)