Skip to content

Commit

Permalink
Fully qualify paths where necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
wagenet authored and mame committed May 24, 2024
1 parent b19a641 commit 6fc1f22
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
19 changes: 17 additions & 2 deletions lib/typeprof/analyzer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions smoke/rbs-base-namespace.rb
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions smoke/rbs-base-namespace.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Foo
class Bar
end
end

0 comments on commit 6fc1f22

Please sign in to comment.