diff --git a/lib/typeprof/analyzer.rb b/lib/typeprof/analyzer.rb index 66bd46ee2..5e63fc860 100644 --- a/lib/typeprof/analyzer.rb +++ b/lib/typeprof/analyzer.rb @@ -549,12 +549,27 @@ def get_class_name(klass) if klass == Type.any "???" else - path = @class_defs[klass.idx].name + class_def = @class_defs[klass.idx] + path = class_def.name if @namespace + # Find index where namespace and path don't match anymore i = 0 i += 1 while @namespace[i] && @namespace[i] == path[i] if path[i] - path[i..].join("::") + parts = path[i..] + + # Sometimes stripping off matching parts of the namespace can lead to matching the wrong + # class so we check here and fully qualify in a case of a mismatch + mismatched = (0..i).any? do |j| + search_path = @namespace[0..j] + parts + found = @class_defs.map { |(_, cd)| cd }.find { |cd| cd.name == search_path } + found && found != class_def + end + + # Use the full path and add an empty field to cause leading :: + parts = [""] + path if mismatched + + parts.join("::") else path.last.to_s end diff --git a/smoke/rbs-base-namespace.rb b/smoke/rbs-base-namespace.rb new file mode 100644 index 000000000..f50265948 --- /dev/null +++ b/smoke/rbs-base-namespace.rb @@ -0,0 +1,14 @@ +module Qux + module Foo + class Bar < ::Foo::Bar + end + end +end +__END__ +# Classes +module Qux + module Foo + class Bar < ::Foo::Bar + end + end +end diff --git a/smoke/rbs-base-namespace.rbs b/smoke/rbs-base-namespace.rbs new file mode 100644 index 000000000..7e2d4cc04 --- /dev/null +++ b/smoke/rbs-base-namespace.rbs @@ -0,0 +1,5 @@ +module Foo + class Bar + end +end +