From 32d8f28a737e98f8f67f91b6ed865f94a9c5f807 Mon Sep 17 00:00:00 2001 From: Hashim1999164 <64767361+Hashim1999164@users.noreply.github.com> Date: Wed, 19 Aug 2026 00:47:08 +0500 Subject: [PATCH] Skip dynamic namespaces in class definitions so packwerk does not crash. --- lib/packwerk/const_node_inspector.rb | 12 ++++++------ .../unit/packwerk/const_node_inspector_test.rb | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 6 deletions(-) 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)