Skip to content

Commit 28efa3f

Browse files
authored
Avoid discarding 'rooted?' information in Pin::Method#return_type (#752)
Solargraph gets confused when a return type's name exists at multiple levels - e.g., "@return [::Array]" in a Pin class method is understood as Solargraph::Pin::Array instead of Ruby's provided Array class. Avoid that issue by avoiding unnecessary round-trip serialization of UniqueType objects via #to_s, which is ambiguous as it doesn't include the '::' prefix.
1 parent 751c91e commit 28efa3f

File tree

5 files changed

+40
-6
lines changed

5 files changed

+40
-6
lines changed

lib/solargraph/complex_type.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ class ComplexType
1111
autoload :TypeMethods, 'solargraph/complex_type/type_methods'
1212
autoload :UniqueType, 'solargraph/complex_type/unique_type'
1313

14-
# @param types [Array<UniqueType>]
14+
# @param types [Array<[UniqueType, ComplexType]>]
1515
def initialize types = [UniqueType::UNDEFINED]
16-
@items = types.uniq(&:to_s)
16+
@items = types.flat_map(&:items).uniq(&:to_s)
1717
end
1818

1919
# @param api_map [ApiMap]
@@ -136,10 +136,10 @@ def all_params
136136
@items.first.all_params || []
137137
end
138138

139-
protected
140-
141139
attr_reader :items
142140

141+
protected
142+
143143
def reduce_object
144144
return self if name != 'Object' || subtypes.empty?
145145
ComplexType.try_parse(reduce_class(subtypes.join(', ')))

lib/solargraph/complex_type/unique_type.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ def to_s
5151
tag
5252
end
5353

54+
def items
55+
[self]
56+
end
57+
5458
def to_rbs
5559
"#{namespace}#{parameters? ? "[#{subtypes.map { |s| s.to_rbs }.join(', ')}]" : ''}"
5660
end
@@ -127,7 +131,7 @@ def self_to dst
127131
def selfy?
128132
@name == 'self' || @key_types.any?(&:selfy?) || @subtypes.any?(&:selfy?)
129133
end
130-
134+
131135
UNDEFINED = UniqueType.new('undefined')
132136
BOOLEAN = UniqueType.new('Boolean')
133137
end

lib/solargraph/pin/method.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def symbol_kind
4646
end
4747

4848
def return_type
49-
@return_type ||= ComplexType.try_parse(*signatures.map(&:return_type).map(&:to_s))
49+
@return_type ||= ComplexType.new(signatures.map(&:return_type).flat_map(&:items))
5050
end
5151

5252
# @return [Signature]

spec/complex_type_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@
4747
expect(types.first.scope).to eq(:instance)
4848
end
4949

50+
it "identify rooted types" do
51+
types = Solargraph::ComplexType.parse '::Array'
52+
expect(types.map(&:rooted?)).to eq([true])
53+
end
54+
55+
it "identify unrooted types" do
56+
types = Solargraph::ComplexType.parse 'Array'
57+
expect(types.map(&:rooted?)).to eq([false])
58+
end
59+
5060
it "detects namespace and scope for classes with subtypes" do
5161
types = Solargraph::ComplexType.parse 'Class<String>'
5262
expect(types.length).to eq(1)

spec/pin/method_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,26 @@ def foo bar:, baz: MyClass.new
4242
expect(pin.documentation).to include('description2')
4343
end
4444

45+
it "tracks rooted status in return types" do
46+
source = Solargraph::Source.new(<<~COMMENTS)
47+
class Foo; end
48+
module Bar
49+
class Foo; end
50+
class Baz
51+
# @return [::Foo]
52+
def bing; end
53+
# @return [Foo]
54+
def bazzle; end
55+
end
56+
end
57+
COMMENTS
58+
map = Solargraph::SourceMap.map(source)
59+
bazzle = map.pins.select{|pin| pin.path == 'Bar::Baz#bazzle'}.first
60+
expect(bazzle.return_type.rooted?).to eq(false)
61+
bing = map.pins.select{|pin| pin.path == 'Bar::Baz#bing'}.first
62+
expect(bing.return_type.rooted?).to eq(true)
63+
end
64+
4565
it "includes yieldparam tags in documentation" do
4666
comments = <<~COMMENTS
4767
@yieldparam one [First] description1

0 commit comments

Comments
 (0)