Skip to content

Commit aac9e41

Browse files
authored
More annotations in Solargraph code (#801)
* More annotations in Solargraph code * More annotations from strict typechecking * More internal annotations * Add some method overloads
1 parent a8c4b23 commit aac9e41

File tree

14 files changed

+61
-10
lines changed

14 files changed

+61
-10
lines changed

lib/solargraph/api_map.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ def catalog bench
7676
self
7777
end
7878

79+
# @return [::Array<Gem::Specification>]
7980
def uncached_gemspecs
8081
@doc_map.uncached_gemspecs
8182
end

lib/solargraph/api_map/store.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,9 @@ def inspect
144144
# @param klass [Class]
145145
# @return [Enumerable<Solargraph::Pin::Base>]
146146
def pins_by_class klass
147-
@pin_select_cache[klass] ||= @pin_class_hash.each_with_object(Set.new) { |(key, o), n| n.merge(o) if key <= klass }
147+
# @type [Set<Solargraph::Pin::Base>]
148+
s = Set.new
149+
@pin_select_cache[klass] ||= @pin_class_hash.each_with_object(s) { |(key, o), n| n.merge(o) if key <= klass }
148150
end
149151

150152
# @param fqns [String]

lib/solargraph/complex_type.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ def each &block
6666
end
6767

6868
# @yieldparam [UniqueType]
69-
# @return [Enumerator<UniqueType>]
69+
# @return [void]
70+
# @overload each_unique_type()
71+
# @return [Enumerator<UniqueType>]
7072
def each_unique_type &block
7173
return enum_for(__method__) unless block_given?
7274

@@ -218,8 +220,11 @@ class << self
218220
# used internally.
219221
#
220222
# @param *strings [Array<String>] The type definitions to parse
221-
# @param partial [Boolean] True if the string is part of a another type
222-
# @return [ComplexType, Array<UniqueType>] Array if partial is true
223+
# @return [ComplexType]
224+
# @overload parse(*strings, partial: false)
225+
# @todo Need ability to use a literal true as a type below
226+
# @param partial [Boolean] True if the string is part of a another type
227+
# @return [Array<UniqueType>]
223228
def parse *strings, partial: false
224229
# @type [Hash{Array<String> => ComplexType}]
225230
@cache ||= {}

lib/solargraph/complex_type/type_methods.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ class ComplexType
1313
# methods:
1414
# transform()
1515
module TypeMethods
16+
# @!method transform(new_name = nil, &transform_type)
17+
# @param new_name [String, nil]
18+
# @yieldparam t [UniqueType]
19+
# @yieldreturn [UniqueType]
20+
# @return [UniqueType, nil]
21+
1622
# @return [String]
1723
attr_reader :name
1824

lib/solargraph/complex_type/unique_type.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ def to_a
135135
end
136136

137137
# @param new_name [String, nil]
138-
# @param new_key_types [Enumerable<UniqueType>, nil]
139-
# @param new_subtypes [Enumerable<UniqueType>, nil]
138+
# @param new_key_types [Array<UniqueType>, nil]
139+
# @param new_subtypes [Array<UniqueType>, nil]
140140
# @return [self]
141141
def recreate(new_name: nil, new_key_types: nil, new_subtypes: nil)
142142
new_name ||= name
@@ -175,8 +175,8 @@ def recreate(new_name: nil, new_key_types: nil, new_subtypes: nil)
175175
#
176176
# @param new_name [String, nil]
177177
# @yieldparam t [UniqueType]
178-
# @yieldreturn [UniqueType]
179-
# @return [UniqueType, nil]
178+
# @yieldreturn [self]
179+
# @return [self]
180180
def transform(new_name = nil, &transform_type)
181181
new_key_types = @key_types.flat_map { |ct| ct.map { |ut| ut.transform(&transform_type) } }.compact
182182
new_subtypes = @subtypes.flat_map { |ct| ct.map { |ut| ut.transform(&transform_type) } }.compact

lib/solargraph/doc_map.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ def generate
5959
end
6060
end
6161

62+
# @param gemspec [Gem::Specification]
63+
# @return [void]
6264
def try_rbs_map gemspec
6365
cache_file = File.join('gems', "#{gemspec.name}-#{gemspec.version}.ser")
6466
if Cache.exist?(cache_file)
@@ -69,6 +71,8 @@ def try_rbs_map gemspec
6971
end
7072
end
7173

74+
# @param name [String]
75+
# @return [void]
7276
def try_stdlib_map name
7377
map = RbsMap::StdlibMap.new(name)
7478
return unless map.resolved?
@@ -93,6 +97,8 @@ def resolve_path_to_gemspec path
9397
end
9498
end
9599

100+
# @param gemspec [Gem::Specification]
101+
# @param version [Gem::Version]
96102
def change_gemspec_version gemspec, version
97103
Gem::Specification.find_by_name(gemspec.name, "= #{version}")
98104
rescue Gem::MissingSpecError

lib/solargraph/gem_pins.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class << self
4949
def build_yard_pins(gemspec)
5050
Yardoc.cache(gemspec) unless Yardoc.cached?(gemspec)
5151
yardoc = Yardoc.load!(gemspec)
52-
YardMap::Mapper.new(yardoc, gemspec).map
52+
YardMap::Mapper.new(yardoc, gemspec).map
5353
end
5454

5555
# Select the first defined type.

lib/solargraph/language_server/message/workspace/did_change_configuration.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ def process
1111

1212
private
1313

14+
# @return [void]
1415
def register_from_options
1516
Solargraph.logger.debug "Registering capabilities from options: #{host.options.inspect}"
1617
# @type [Array<String>]

lib/solargraph/parser/node_methods.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ def unpack_name node
1010
raise NotImplementedError
1111
end
1212

13+
# @abstract
14+
# @todo Temporarily here for testing. Move to Solargraph::Parser.
15+
# @param node [Parser::AST::Node]
16+
# @return [Array<Parser::AST::Node>]
17+
def call_nodes_from node
18+
raise NotImplementedError
19+
end
20+
1321
# Find all the nodes within the provided node that potentially return a
1422
# value.
1523
#
@@ -27,6 +35,14 @@ def returns_from_method_body node
2735
raise NotImplementedError
2836
end
2937

38+
# @abstract
39+
# @param node [Parser::AST::Node]
40+
#
41+
# @return [Array<Parser::AST::Node>]
42+
def const_nodes_from node
43+
raise NotImplementedError
44+
end
45+
3046
# @abstract
3147
# @param cursor [Solargraph::Source::Cursor]
3248
# @return [Parser::AST::Node, nil]
@@ -43,6 +59,11 @@ def value_position_nodes_only(node)
4359
raise NotImplementedError
4460
end
4561

62+
# @abstract
63+
# @param nodes [Enumerable<Parser::AST::Node>]
64+
def any_splatted_call?(nodes)
65+
raise NotImplementedError
66+
end
4667

4768
# @abstract
4869
# @param node [Parser::AST::Node]

lib/solargraph/parser/parser_gem/class_methods.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def parse code, filename = nil, line = 0
3232
raise Parser::SyntaxError, e.message
3333
end
3434

35-
# @return [Parser::Base]
35+
# @return [::Parser::Base]
3636
def parser
3737
# @todo Consider setting an instance variable. We might not need to
3838
# recreate the parser every time we use it.
@@ -115,6 +115,7 @@ def infer_literal_node_type node
115115
NodeMethods.infer_literal_node_type node
116116
end
117117

118+
# @return [void]
118119
def version
119120
parser.version
120121
end

lib/solargraph/parser/parser_gem/node_chainer.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ def load_string(code)
5050
def generate_links n
5151
return [] unless n.is_a?(::Parser::AST::Node)
5252
return generate_links(n.children[0]) if n.type == :splat
53+
# @type [Array<Chain::Link>]
5354
result = []
5455
if n.type == :block
5556
result.concat NodeChainer.chain(n.children[0], @filename, n).links

lib/solargraph/pin/block.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ class Block < Closure
88
# @return [Parser::AST::Node]
99
attr_reader :receiver
1010

11+
# @param receiver [Parser::AST::Node, nil]
12+
# @param context [ComplexType, nil]
1113
# @param args [::Array<Parameter>]
1214
def initialize receiver: nil, args: [], context: nil, **splat
1315
super(**splat)

lib/solargraph/pin/namespace.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ def return_type
7474
@return_type ||= ComplexType.try_parse( (type == :class ? 'Class' : 'Module') + "<#{path}>" )
7575
end
7676

77+
# @return [Array<String>]
7778
def domains
7879
@domains ||= []
7980
end

lib/solargraph/shell.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,9 @@ def bundle
233233
end
234234

235235
desc 'cache', 'Cache a gem', hide: true
236+
# @return [void]
237+
# @param gem [String]
238+
# @param version [String, nil]
236239
def cache gem, version = nil
237240
spec = Gem::Specification.find_by_name(gem, version)
238241
pins = GemPins.build(spec)
@@ -241,6 +244,7 @@ def cache gem, version = nil
241244

242245
desc 'gems', 'Cache documentation for installed gems'
243246
option :rebuild, type: :boolean, desc: 'Rebuild existing documentation', default: false
247+
# @return [void]
244248
def gems *names
245249
if names.empty?
246250
Gem::Specification.to_a.each do |spec|

0 commit comments

Comments
 (0)