solargraph 0.58.1 → 0.58.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (147) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/CHANGELOG.md +7 -1
  4. data/lib/solargraph/api_map/cache.rb +110 -110
  5. data/lib/solargraph/api_map/constants.rb +279 -279
  6. data/lib/solargraph/api_map/index.rb +193 -193
  7. data/lib/solargraph/api_map/source_to_yard.rb +97 -97
  8. data/lib/solargraph/api_map/store.rb +384 -384
  9. data/lib/solargraph/api_map.rb +945 -945
  10. data/lib/solargraph/complex_type/type_methods.rb +228 -228
  11. data/lib/solargraph/complex_type/unique_type.rb +482 -482
  12. data/lib/solargraph/complex_type.rb +444 -444
  13. data/lib/solargraph/convention/data_definition/data_definition_node.rb +91 -91
  14. data/lib/solargraph/convention/data_definition.rb +105 -105
  15. data/lib/solargraph/convention/struct_definition/struct_assignment_node.rb +61 -61
  16. data/lib/solargraph/convention/struct_definition/struct_definition_node.rb +102 -102
  17. data/lib/solargraph/convention/struct_definition.rb +164 -164
  18. data/lib/solargraph/diagnostics/require_not_found.rb +53 -53
  19. data/lib/solargraph/diagnostics/rubocop.rb +118 -118
  20. data/lib/solargraph/diagnostics/rubocop_helpers.rb +68 -68
  21. data/lib/solargraph/diagnostics/type_check.rb +55 -55
  22. data/lib/solargraph/doc_map.rb +439 -439
  23. data/lib/solargraph/equality.rb +34 -34
  24. data/lib/solargraph/gem_pins.rb +98 -98
  25. data/lib/solargraph/language_server/host/diagnoser.rb +89 -89
  26. data/lib/solargraph/language_server/host/dispatch.rb +130 -130
  27. data/lib/solargraph/language_server/host/message_worker.rb +112 -112
  28. data/lib/solargraph/language_server/host/sources.rb +99 -99
  29. data/lib/solargraph/language_server/host.rb +878 -878
  30. data/lib/solargraph/language_server/message/extended/check_gem_version.rb +114 -114
  31. data/lib/solargraph/language_server/message/extended/document.rb +23 -23
  32. data/lib/solargraph/language_server/message/text_document/completion.rb +56 -56
  33. data/lib/solargraph/language_server/message/text_document/definition.rb +40 -40
  34. data/lib/solargraph/language_server/message/text_document/document_symbol.rb +26 -26
  35. data/lib/solargraph/language_server/message/text_document/formatting.rb +148 -148
  36. data/lib/solargraph/language_server/message/text_document/hover.rb +58 -58
  37. data/lib/solargraph/language_server/message/text_document/signature_help.rb +24 -24
  38. data/lib/solargraph/language_server/message/text_document/type_definition.rb +25 -25
  39. data/lib/solargraph/language_server/message/workspace/workspace_symbol.rb +23 -23
  40. data/lib/solargraph/library.rb +683 -683
  41. data/lib/solargraph/location.rb +82 -82
  42. data/lib/solargraph/logging.rb +37 -37
  43. data/lib/solargraph/parser/comment_ripper.rb +69 -69
  44. data/lib/solargraph/parser/flow_sensitive_typing.rb +255 -255
  45. data/lib/solargraph/parser/node_processor/base.rb +92 -92
  46. data/lib/solargraph/parser/node_processor.rb +62 -62
  47. data/lib/solargraph/parser/parser_gem/class_methods.rb +149 -149
  48. data/lib/solargraph/parser/parser_gem/node_chainer.rb +166 -166
  49. data/lib/solargraph/parser/parser_gem/node_methods.rb +486 -486
  50. data/lib/solargraph/parser/parser_gem/node_processors/and_node.rb +22 -22
  51. data/lib/solargraph/parser/parser_gem/node_processors/args_node.rb +59 -59
  52. data/lib/solargraph/parser/parser_gem/node_processors/begin_node.rb +15 -15
  53. data/lib/solargraph/parser/parser_gem/node_processors/block_node.rb +46 -46
  54. data/lib/solargraph/parser/parser_gem/node_processors/def_node.rb +53 -53
  55. data/lib/solargraph/parser/parser_gem/node_processors/if_node.rb +23 -23
  56. data/lib/solargraph/parser/parser_gem/node_processors/ivasgn_node.rb +40 -40
  57. data/lib/solargraph/parser/parser_gem/node_processors/lvasgn_node.rb +29 -29
  58. data/lib/solargraph/parser/parser_gem/node_processors/masgn_node.rb +59 -59
  59. data/lib/solargraph/parser/parser_gem/node_processors/opasgn_node.rb +98 -98
  60. data/lib/solargraph/parser/parser_gem/node_processors/orasgn_node.rb +17 -17
  61. data/lib/solargraph/parser/parser_gem/node_processors/resbody_node.rb +38 -38
  62. data/lib/solargraph/parser/parser_gem/node_processors/sclass_node.rb +52 -52
  63. data/lib/solargraph/parser/parser_gem/node_processors/send_node.rb +291 -291
  64. data/lib/solargraph/parser/parser_gem/node_processors/while_node.rb +29 -29
  65. data/lib/solargraph/parser/parser_gem/node_processors.rb +70 -70
  66. data/lib/solargraph/parser/region.rb +69 -69
  67. data/lib/solargraph/parser/snippet.rb +17 -17
  68. data/lib/solargraph/pin/base.rb +729 -729
  69. data/lib/solargraph/pin/base_variable.rb +126 -126
  70. data/lib/solargraph/pin/block.rb +104 -104
  71. data/lib/solargraph/pin/breakable.rb +9 -9
  72. data/lib/solargraph/pin/callable.rb +231 -231
  73. data/lib/solargraph/pin/closure.rb +72 -72
  74. data/lib/solargraph/pin/common.rb +79 -79
  75. data/lib/solargraph/pin/conversions.rb +123 -123
  76. data/lib/solargraph/pin/delegated_method.rb +120 -120
  77. data/lib/solargraph/pin/documenting.rb +114 -114
  78. data/lib/solargraph/pin/instance_variable.rb +34 -34
  79. data/lib/solargraph/pin/keyword.rb +20 -20
  80. data/lib/solargraph/pin/local_variable.rb +75 -75
  81. data/lib/solargraph/pin/method.rb +672 -672
  82. data/lib/solargraph/pin/method_alias.rb +34 -34
  83. data/lib/solargraph/pin/namespace.rb +115 -115
  84. data/lib/solargraph/pin/parameter.rb +275 -275
  85. data/lib/solargraph/pin/proxy_type.rb +39 -39
  86. data/lib/solargraph/pin/reference/override.rb +47 -47
  87. data/lib/solargraph/pin/reference/superclass.rb +15 -15
  88. data/lib/solargraph/pin/reference.rb +39 -39
  89. data/lib/solargraph/pin/search.rb +61 -61
  90. data/lib/solargraph/pin/signature.rb +61 -61
  91. data/lib/solargraph/pin/symbol.rb +53 -53
  92. data/lib/solargraph/pin/until.rb +18 -18
  93. data/lib/solargraph/pin/while.rb +18 -18
  94. data/lib/solargraph/pin.rb +44 -44
  95. data/lib/solargraph/pin_cache.rb +245 -245
  96. data/lib/solargraph/position.rb +132 -119
  97. data/lib/solargraph/range.rb +112 -112
  98. data/lib/solargraph/rbs_map/conversions.rb +823 -823
  99. data/lib/solargraph/rbs_map/core_map.rb +58 -58
  100. data/lib/solargraph/rbs_map/stdlib_map.rb +43 -43
  101. data/lib/solargraph/rbs_map.rb +163 -163
  102. data/lib/solargraph/shell.rb +352 -352
  103. data/lib/solargraph/source/chain/call.rb +337 -337
  104. data/lib/solargraph/source/chain/constant.rb +26 -26
  105. data/lib/solargraph/source/chain/hash.rb +34 -34
  106. data/lib/solargraph/source/chain/if.rb +28 -28
  107. data/lib/solargraph/source/chain/instance_variable.rb +13 -13
  108. data/lib/solargraph/source/chain/literal.rb +48 -48
  109. data/lib/solargraph/source/chain/or.rb +23 -23
  110. data/lib/solargraph/source/chain.rb +291 -291
  111. data/lib/solargraph/source/change.rb +82 -82
  112. data/lib/solargraph/source/cursor.rb +166 -166
  113. data/lib/solargraph/source/source_chainer.rb +194 -194
  114. data/lib/solargraph/source/updater.rb +55 -55
  115. data/lib/solargraph/source.rb +498 -498
  116. data/lib/solargraph/source_map/clip.rb +226 -226
  117. data/lib/solargraph/source_map/data.rb +34 -34
  118. data/lib/solargraph/source_map/mapper.rb +259 -259
  119. data/lib/solargraph/source_map.rb +212 -212
  120. data/lib/solargraph/type_checker/checks.rb +124 -124
  121. data/lib/solargraph/type_checker/param_def.rb +37 -37
  122. data/lib/solargraph/type_checker/problem.rb +32 -32
  123. data/lib/solargraph/type_checker/rules.rb +84 -84
  124. data/lib/solargraph/type_checker.rb +814 -814
  125. data/lib/solargraph/version.rb +1 -1
  126. data/lib/solargraph/workspace/config.rb +255 -255
  127. data/lib/solargraph/workspace/require_paths.rb +97 -97
  128. data/lib/solargraph/workspace.rb +220 -220
  129. data/lib/solargraph/yard_map/helpers.rb +44 -44
  130. data/lib/solargraph/yard_map/mapper/to_method.rb +130 -130
  131. data/lib/solargraph/yard_map/mapper/to_namespace.rb +31 -31
  132. data/lib/solargraph/yard_map/mapper.rb +79 -79
  133. data/lib/solargraph/yard_map/to_method.rb +89 -89
  134. data/lib/solargraph/yardoc.rb +87 -87
  135. data/lib/solargraph.rb +105 -105
  136. data/rbs_collection.yaml +1 -1
  137. metadata +12 -12
  138. /data/{sig → rbs}/shims/ast/0/node.rbs +0 -0
  139. /data/{sig → rbs}/shims/ast/2.4/.rbs_meta.yaml +0 -0
  140. /data/{sig → rbs}/shims/ast/2.4/ast.rbs +0 -0
  141. /data/{sig → rbs}/shims/parser/3.2.0.1/builders/default.rbs +0 -0
  142. /data/{sig → rbs}/shims/parser/3.2.0.1/manifest.yaml +0 -0
  143. /data/{sig → rbs}/shims/parser/3.2.0.1/parser.rbs +0 -0
  144. /data/{sig → rbs}/shims/parser/3.2.0.1/polyfill.rbs +0 -0
  145. /data/{sig → rbs}/shims/thor/1.2.0.1/.rbs_meta.yaml +0 -0
  146. /data/{sig → rbs}/shims/thor/1.2.0.1/manifest.yaml +0 -0
  147. /data/{sig → rbs}/shims/thor/1.2.0.1/thor.rbs +0 -0
@@ -1,823 +1,823 @@
1
- # frozen_string_literal: true
2
-
3
- require 'rbs'
4
-
5
- module Solargraph
6
- class RbsMap
7
- # Functions for converting RBS declarations to Solargraph pins
8
- #
9
- class Conversions
10
- include Logging
11
-
12
- # A container for tracking the current context of the RBS conversion
13
- # process, e.g., what visibility is declared for methods in the current
14
- # scope
15
- #
16
- class Context
17
- attr_reader :visibility
18
-
19
- # @param visibility [Symbol]
20
- def initialize visibility = :public
21
- @visibility = visibility
22
- end
23
- end
24
-
25
- # @param loader [RBS::EnvironmentLoader]
26
- def initialize(loader:)
27
- @loader = loader
28
- @pins = []
29
- load_environment_to_pins(loader)
30
- end
31
-
32
- # @return [RBS::EnvironmentLoader]
33
- attr_reader :loader
34
-
35
- # @return [Array<Pin::Base>]
36
- attr_reader :pins
37
-
38
- private
39
-
40
- # @return [Hash{String => RBS::AST::Declarations::TypeAlias}]
41
- def type_aliases
42
- @type_aliases ||= {}
43
- end
44
-
45
- # @param loader [RBS::EnvironmentLoader]
46
- # @return [void]
47
- def load_environment_to_pins(loader)
48
- environment = RBS::Environment.from_loader(loader).resolve_type_names
49
- cursor = pins.length
50
- if environment.declarations.empty?
51
- Solargraph.logger.info "No RBS declarations found in environment for core_root #{loader.core_root.inspect}, libraries #{loader.libs} and directories #{loader.dirs}"
52
- return
53
- end
54
- environment.declarations.each { |decl| convert_decl_to_pin(decl, Solargraph::Pin::ROOT_PIN) }
55
- end
56
-
57
- # @param decl [RBS::AST::Declarations::Base]
58
- # @param closure [Pin::Closure]
59
- # @return [void]
60
- def convert_decl_to_pin decl, closure
61
- case decl
62
- when RBS::AST::Declarations::Class
63
- class_decl_to_pin decl
64
- when RBS::AST::Declarations::Interface
65
- # STDERR.puts "Skipping interface #{decl.name.relative!}"
66
- interface_decl_to_pin decl, closure
67
- when RBS::AST::Declarations::TypeAlias
68
- # @sg-ignore https://github.com/castwide/solargraph/pull/1114
69
- type_aliases[decl.name.to_s] = decl
70
- when RBS::AST::Declarations::Module
71
- module_decl_to_pin decl
72
- when RBS::AST::Declarations::Constant
73
- constant_decl_to_pin decl
74
- when RBS::AST::Declarations::ClassAlias
75
- class_alias_decl_to_pin decl
76
- when RBS::AST::Declarations::ModuleAlias
77
- module_alias_decl_to_pin decl
78
- when RBS::AST::Declarations::Global
79
- global_decl_to_pin decl
80
- else
81
- Solargraph.logger.warn "Skipping declaration #{decl.class}"
82
- end
83
- end
84
-
85
- # @param decl [RBS::AST::Declarations::Module]
86
- # @param module_pin [Pin::Namespace]
87
- # @return [void]
88
- def convert_self_types_to_pins decl, module_pin
89
- decl.self_types.each { |self_type| context = convert_self_type_to_pins(self_type, module_pin) }
90
- end
91
-
92
- # @param decl [RBS::AST::Declarations::Module::Self]
93
- # @param closure [Pin::Namespace]
94
- # @return [void]
95
- def convert_self_type_to_pins decl, closure
96
- type = build_type(decl.name, decl.args)
97
- generic_values = type.all_params.map(&:to_s)
98
- include_pin = Solargraph::Pin::Reference::Include.new(
99
- name: decl.name.relative!.to_s,
100
- type_location: location_decl_to_pin_location(decl.location),
101
- generic_values: generic_values,
102
- closure: closure,
103
- source: :rbs
104
- )
105
- pins.push include_pin
106
- end
107
-
108
- # @param decl [RBS::AST::Declarations::Module,RBS::AST::Declarations::Class,RBS::AST::Declarations::Interface]
109
- # @param closure [Pin::Namespace]
110
- # @return [void]
111
- def convert_members_to_pins decl, closure
112
- context = Conversions::Context.new
113
- decl.members.each { |m| context = convert_member_to_pin(m, closure, context) }
114
- end
115
-
116
- # @param member [RBS::AST::Members::Base,RBS::AST::Declarations::Base]
117
- # @param closure [Pin::Namespace]
118
- # @param context [Context]
119
- # @return [Context]
120
- def convert_member_to_pin member, closure, context
121
- case member
122
- when RBS::AST::Members::MethodDefinition
123
- method_def_to_pin(member, closure, context)
124
- when RBS::AST::Members::AttrReader
125
- attr_reader_to_pin(member, closure, context)
126
- when RBS::AST::Members::AttrWriter
127
- attr_writer_to_pin(member, closure, context)
128
- when RBS::AST::Members::AttrAccessor
129
- attr_accessor_to_pin(member, closure, context)
130
- when RBS::AST::Members::Include
131
- include_to_pin(member, closure)
132
- when RBS::AST::Members::Prepend
133
- prepend_to_pin(member, closure)
134
- when RBS::AST::Members::Extend
135
- extend_to_pin(member, closure)
136
- when RBS::AST::Members::Alias
137
- alias_to_pin(member, closure)
138
- when RBS::AST::Members::ClassInstanceVariable
139
- civar_to_pin(member, closure)
140
- when RBS::AST::Members::ClassVariable
141
- cvar_to_pin(member, closure)
142
- when RBS::AST::Members::InstanceVariable
143
- ivar_to_pin(member, closure)
144
- when RBS::AST::Members::Public
145
- return Context.new(:public)
146
- when RBS::AST::Members::Private
147
- return Context.new(:private)
148
- when RBS::AST::Declarations::Base
149
- convert_decl_to_pin(member, closure)
150
- else
151
- Solargraph.logger.warn "Skipping member type #{member.class}"
152
- end
153
- context
154
- end
155
-
156
- # @param decl [RBS::AST::Declarations::Class]
157
- # @return [void]
158
- def class_decl_to_pin decl
159
- generics = decl.type_params.map(&:name).map(&:to_s)
160
- generic_defaults = {}
161
- decl.type_params.each do |param|
162
- if param.default_type
163
- tag = other_type_to_tag param.default_type
164
- generic_defaults[param.name.to_s] = ComplexType.parse(tag).force_rooted
165
- end
166
- end
167
- class_name = decl.name.relative!.to_s
168
- class_pin = Solargraph::Pin::Namespace.new(
169
- type: :class,
170
- name: class_name,
171
- closure: Solargraph::Pin::ROOT_PIN,
172
- comments: decl.comment&.string,
173
- type_location: location_decl_to_pin_location(decl.location),
174
- # @todo some type parameters in core/stdlib have default
175
- # values; Solargraph doesn't support that yet as so these
176
- # get treated as undefined if not specified
177
- generics: generics,
178
- generic_defaults: generic_defaults,
179
- source: :rbs
180
- )
181
- pins.push class_pin
182
- if decl.super_class
183
- type = build_type(decl.super_class.name, decl.super_class.args)
184
- generic_values = type.all_params.map(&:to_s)
185
- superclass_name = decl.super_class.name.to_s
186
- pins.push Solargraph::Pin::Reference::Superclass.new(
187
- type_location: location_decl_to_pin_location(decl.super_class.location),
188
- closure: class_pin,
189
- generic_values: generic_values,
190
- name: superclass_name,
191
- source: :rbs
192
- )
193
- end
194
- add_mixins decl, class_pin
195
- convert_members_to_pins decl, class_pin
196
- end
197
-
198
- # @param decl [RBS::AST::Declarations::Interface]
199
- # @param closure [Pin::Closure]
200
- # @return [void]
201
- def interface_decl_to_pin decl, closure
202
- class_pin = Solargraph::Pin::Namespace.new(
203
- type: :module,
204
- type_location: location_decl_to_pin_location(decl.location),
205
- name: decl.name.relative!.to_s,
206
- closure: Solargraph::Pin::ROOT_PIN,
207
- comments: decl.comment&.string,
208
- generics: decl.type_params.map(&:name).map(&:to_s),
209
- # HACK: Using :hidden to keep interfaces from appearing in
210
- # autocompletion
211
- visibility: :hidden,
212
- source: :rbs
213
- )
214
- class_pin.docstring.add_tag(YARD::Tags::Tag.new(:abstract, '(RBS interface)'))
215
- pins.push class_pin
216
- convert_members_to_pins decl, class_pin
217
- end
218
-
219
- # @param decl [RBS::AST::Declarations::Module]
220
- # @return [void]
221
- def module_decl_to_pin decl
222
- module_pin = Solargraph::Pin::Namespace.new(
223
- type: :module,
224
- name: decl.name.relative!.to_s,
225
- type_location: location_decl_to_pin_location(decl.location),
226
- closure: Solargraph::Pin::ROOT_PIN,
227
- comments: decl.comment&.string,
228
- generics: decl.type_params.map(&:name).map(&:to_s),
229
- source: :rbs
230
- )
231
- pins.push module_pin
232
- convert_self_types_to_pins decl, module_pin
233
- convert_members_to_pins decl, module_pin
234
-
235
- add_mixins decl, module_pin.closure
236
- end
237
-
238
- # @param name [String]
239
- # @param tag [String]
240
- # @param comments [String]
241
- # @param decl [RBS::AST::Declarations::ClassAlias, RBS::AST::Declarations::Constant, RBS::AST::Declarations::ModuleAlias]
242
- # @param base [String, nil] Optional conversion of tag to base<tag>
243
- #
244
- # @return [Solargraph::Pin::Constant]
245
- def create_constant(name, tag, comments, decl, base = nil)
246
- parts = name.split('::')
247
- if parts.length > 1
248
- name = parts.last
249
- closure = pins.select { |pin| pin && pin.path == parts[0..-2].join('::') }.first
250
- else
251
- name = parts.first
252
- closure = Solargraph::Pin::ROOT_PIN
253
- end
254
- constant_pin = Solargraph::Pin::Constant.new(
255
- name: name,
256
- closure: closure,
257
- type_location: location_decl_to_pin_location(decl.location),
258
- comments: comments,
259
- source: :rbs
260
- )
261
- tag = "#{base}<#{tag}>" if base
262
- rooted_tag = ComplexType.parse(tag).force_rooted.rooted_tags
263
- constant_pin.docstring.add_tag(YARD::Tags::Tag.new(:return, '', rooted_tag))
264
- constant_pin
265
- end
266
-
267
- # @param decl [RBS::AST::Declarations::ClassAlias]
268
- # @return [void]
269
- def class_alias_decl_to_pin decl
270
- # See https://www.rubydoc.info/gems/rbs/3.4.3/RBS/AST/Declarations/ClassAlias
271
- new_name = decl.new_name.relative!.to_s
272
- old_name = decl.old_name.relative!.to_s
273
-
274
- pins.push create_constant(new_name, old_name, decl.comment&.string, decl, 'Class')
275
- end
276
-
277
- # @param decl [RBS::AST::Declarations::ModuleAlias]
278
- # @return [void]
279
- def module_alias_decl_to_pin decl
280
- # See https://www.rubydoc.info/gems/rbs/3.4.3/RBS/AST/Declarations/ModuleAlias
281
- new_name = decl.new_name.relative!.to_s
282
- old_name = decl.old_name.relative!.to_s
283
-
284
- pins.push create_constant(new_name, old_name, decl.comment&.string, decl, 'Module')
285
- end
286
-
287
- # @param decl [RBS::AST::Declarations::Constant]
288
- # @return [void]
289
- def constant_decl_to_pin decl
290
- tag = other_type_to_tag(decl.type)
291
- pins.push create_constant(decl.name.relative!.to_s, tag, decl.comment&.string, decl)
292
- end
293
-
294
- # @param decl [RBS::AST::Declarations::Global]
295
- # @return [void]
296
- def global_decl_to_pin decl
297
- closure = Solargraph::Pin::ROOT_PIN
298
- name = decl.name.to_s
299
- pin = Solargraph::Pin::GlobalVariable.new(
300
- name: name,
301
- closure: closure,
302
- comments: decl.comment&.string,
303
- type_location: location_decl_to_pin_location(decl.location),
304
- source: :rbs
305
- )
306
- rooted_tag = ComplexType.parse(other_type_to_tag(decl.type)).force_rooted.rooted_tags
307
- pin.docstring.add_tag(YARD::Tags::Tag.new(:type, '', rooted_tag))
308
- pins.push pin
309
- end
310
-
311
-
312
- # Visibility overrides that will allow the Solargraph project
313
- # and plugins to pass typechecking using SOLARGRAPH_ASSERTS=on,
314
- # so that we can detect any regressions/issues elsewhere in the
315
- # visibility logic.
316
- #
317
- # These should either reflect a bug upstream in the RBS
318
- # definitions, or include a @todo indicating what needs to be
319
- # fixed in Solargraph to properly understand it.
320
- #
321
- # @todo PR these fixes upstream and list open PRs here above
322
- # related overrides
323
- # @todo externalize remaining overrides into yaml file, then
324
- # allow that to be extended via .solargraph.yml
325
- # @type [Hash{Array(String, Symbol, String) => Symbol}
326
- VISIBILITY_OVERRIDE = {
327
- ["Rails::Engine", :instance, "run_tasks_blocks"] => :protected,
328
- # Should have been marked as both instance and class method in module -e.g., 'module_function'
329
- ["Kernel", :instance, "pretty_inspect"] => :private,
330
- # marked incorrectly in RBS
331
- ["WEBrick::HTTPUtils::FormData", :instance, "next_data"] => :protected,
332
- ["Rails::Command", :class, "command_type"] => :private,
333
- ["Rails::Command", :class, "lookup_paths"] => :private,
334
- ["Rails::Command", :class, "file_lookup_paths"] => :private,
335
- ["Rails::Railtie", :instance, "run_console_blocks"] => :protected,
336
- ["Rails::Railtie", :instance, "run_generators_blocks"] => :protected,
337
- ["Rails::Railtie", :instance, "run_runner_blocks"] => :protected,
338
- ["Rails::Railtie", :instance, "run_tasks_blocks"] => :protected,
339
- ["ActionController::Base", :instance, "_protected_ivars"] => :private,
340
- ["ActionView::Template", :instance, "method_name"] => :public,
341
- ["Module", :instance, "ruby2_keywords"] => :private,
342
- ["Nokogiri::XML::Node", :instance, "coerce"] => :protected,
343
- ["Nokogiri::XML::Document", :class, "empty_doc?"] => :private,
344
- ["Nokogiri::Decorators::Slop", :instance, "respond_to_missing?"] => :public,
345
- ["RuboCop::Cop::RangeHelp", :instance, "source_range"] => :private,
346
- ["AST::Node", :instance, "original_dup"] => :private,
347
- ["Rainbow::Presenter", :instance, "wrap_with_sgr"] => :private,
348
- }
349
-
350
- # @param decl [RBS::AST::Members::MethodDefinition, RBS::AST::Members::AttrReader, RBS::AST::Members::AttrAccessor]
351
- # @param closure [Pin::Closure]
352
- # @param context [Context]
353
- # @param scope [Symbol] :instance or :class
354
- # @param name [String] The name of the method
355
- # @sg-ignore
356
- # @return [Symbol]
357
- def calculate_method_visibility(decl, context, closure, scope, name)
358
- override_key = [closure.path, scope, name]
359
- visibility = VISIBILITY_OVERRIDE[override_key]
360
- simple_override_key = [closure.path, scope]
361
- visibility ||= VISIBILITY_OVERRIDE[simple_override_key]
362
- visibility ||= :private if closure.path == 'Kernel' && Kernel.private_instance_methods(false).include?(decl.name)
363
- if decl.kind == :singleton_instance
364
- # this is a 'module function'
365
- visibility ||= :private
366
- end
367
- visibility ||= decl.visibility
368
- visibility ||= context.visibility
369
- visibility ||= :public
370
- visibility
371
- end
372
-
373
- # @param decl [RBS::AST::Members::MethodDefinition]
374
- # @param closure [Pin::Closure]
375
- # @param context [Context]
376
- # @return [void]
377
- def method_def_to_pin decl, closure, context
378
- # there may be edge cases here around different signatures
379
- # having different type params / orders - we may need to match
380
- # this data model and have generics live in signatures to
381
- # handle those correctly
382
- generics = decl.overloads.map(&:method_type).flat_map(&:type_params).map(&:name).map(&:to_s).uniq
383
-
384
- if decl.instance?
385
- name = decl.name.to_s
386
- final_scope = :instance
387
- visibility = calculate_method_visibility(decl, context, closure, final_scope, name)
388
- pin = Solargraph::Pin::Method.new(
389
- name: name,
390
- closure: closure,
391
- type_location: location_decl_to_pin_location(decl.location),
392
- comments: decl.comment&.string,
393
- scope: final_scope,
394
- signatures: [],
395
- generics: generics,
396
- visibility: visibility,
397
- source: :rbs
398
- )
399
- pin.signatures.concat method_def_to_sigs(decl, pin)
400
- pins.push pin
401
- if pin.name == 'initialize'
402
- pin.instance_variable_set(:@visibility, :private)
403
- pin.instance_variable_set(:@return_type, ComplexType::VOID)
404
- end
405
- end
406
- if decl.singleton?
407
- final_scope = :class
408
- name = decl.name.to_s
409
- visibility = calculate_method_visibility(decl, context, closure, final_scope, name)
410
- pin = Solargraph::Pin::Method.new(
411
- name: name,
412
- closure: closure,
413
- comments: decl.comment&.string,
414
- type_location: location_decl_to_pin_location(decl.location),
415
- visibility: visibility,
416
- scope: final_scope,
417
- signatures: [],
418
- generics: generics,
419
- source: :rbs
420
- )
421
- pin.signatures.concat method_def_to_sigs(decl, pin)
422
- pins.push pin
423
- end
424
- end
425
-
426
- # @param decl [RBS::AST::Members::MethodDefinition]
427
- # @param pin [Pin::Method]
428
- # @return [void]
429
- def method_def_to_sigs decl, pin
430
- # @param overload [RBS::AST::Members::MethodDefinition::Overload]
431
- decl.overloads.map do |overload|
432
- type_location = location_decl_to_pin_location(overload.method_type.location)
433
- generics = overload.method_type.type_params.map(&:name).map(&:to_s)
434
- signature_parameters, signature_return_type = parts_of_function(overload.method_type, pin)
435
- block = if overload.method_type.block
436
- block_parameters, block_return_type = parts_of_function(overload.method_type.block, pin)
437
- Pin::Signature.new(generics: generics, parameters: block_parameters, return_type: block_return_type, source: :rbs,
438
- type_location: type_location, closure: pin)
439
- end
440
- Pin::Signature.new(generics: generics, parameters: signature_parameters, return_type: signature_return_type, block: block, source: :rbs,
441
- type_location: type_location, closure: pin)
442
- end
443
- end
444
-
445
- # @param location [RBS::Location, nil]
446
- # @return [Solargraph::Location, nil]
447
- def location_decl_to_pin_location(location)
448
- return nil if location&.name.nil?
449
-
450
- start_pos = Position.new(location.start_line - 1, location.start_column)
451
- end_pos = Position.new(location.end_line - 1, location.end_column)
452
- range = Range.new(start_pos, end_pos)
453
- Location.new(location.name.to_s, range)
454
- end
455
-
456
- # @param type [RBS::MethodType,RBS::Types::Block]
457
- # @param pin [Pin::Method]
458
- # @return [Array(Array<Pin::Parameter>, ComplexType)]
459
- def parts_of_function type, pin
460
- type_location = pin.type_location
461
- if defined?(RBS::Types::UntypedFunction) && type.type.is_a?(RBS::Types::UntypedFunction)
462
- return [
463
- [Solargraph::Pin::Parameter.new(decl: :restarg, name: 'arg', closure: pin, source: :rbs, type_location: type_location)],
464
- ComplexType.try_parse(method_type_to_tag(type)).force_rooted
465
- ]
466
- end
467
-
468
- parameters = []
469
- arg_num = -1
470
- type.type.required_positionals.each do |param|
471
- # @sg-ignore RBS generic type understanding issue
472
- name = param.name ? param.name.to_s : "arg_#{arg_num += 1}"
473
- # @sg-ignore RBS generic type understanding issue
474
- parameters.push Solargraph::Pin::Parameter.new(decl: :arg, name: name, closure: pin, return_type: ComplexType.try_parse(other_type_to_tag(param.type)).force_rooted, source: :rbs, type_location: type_location)
475
- end
476
- type.type.optional_positionals.each do |param|
477
- # @sg-ignore RBS generic type understanding issue
478
- name = param.name ? param.name.to_s : "arg_#{arg_num += 1}"
479
- parameters.push Solargraph::Pin::Parameter.new(decl: :optarg, name: name, closure: pin,
480
- # @sg-ignore RBS generic type understanding issue
481
- return_type: ComplexType.try_parse(other_type_to_tag(param.type)).force_rooted,
482
- type_location: type_location,
483
- source: :rbs)
484
- end
485
- if type.type.rest_positionals
486
- name = type.type.rest_positionals.name ? type.type.rest_positionals.name.to_s : "arg_#{arg_num += 1}"
487
- inner_rest_positional_type =
488
- ComplexType.try_parse(other_type_to_tag(type.type.rest_positionals.type))
489
- rest_positional_type = ComplexType::UniqueType.new('Array',
490
- [],
491
- [inner_rest_positional_type],
492
- rooted: true, parameters_type: :list)
493
- parameters.push Solargraph::Pin::Parameter.new(decl: :restarg, name: name, closure: pin,
494
- source: :rbs, type_location: type_location,
495
- return_type: rest_positional_type,)
496
- end
497
- type.type.trailing_positionals.each do |param|
498
- # @sg-ignore RBS generic type understanding issue
499
- name = param.name ? param.name.to_s : "arg_#{arg_num += 1}"
500
- parameters.push Solargraph::Pin::Parameter.new(decl: :arg, name: name, closure: pin, source: :rbs, type_location: type_location)
501
- end
502
- type.type.required_keywords.each do |orig, param|
503
- # @sg-ignore RBS generic type understanding issue
504
- name = orig ? orig.to_s : "arg_#{arg_num += 1}"
505
- parameters.push Solargraph::Pin::Parameter.new(decl: :kwarg, name: name, closure: pin,
506
- # @sg-ignore RBS generic type understanding issue
507
- return_type: ComplexType.try_parse(other_type_to_tag(param.type)).force_rooted,
508
- source: :rbs, type_location: type_location)
509
- end
510
- type.type.optional_keywords.each do |orig, param|
511
- # @sg-ignore RBS generic type understanding issue
512
- name = orig ? orig.to_s : "arg_#{arg_num += 1}"
513
- parameters.push Solargraph::Pin::Parameter.new(decl: :kwoptarg, name: name, closure: pin,
514
- # @sg-ignore RBS generic type understanding issue
515
- return_type: ComplexType.try_parse(other_type_to_tag(param.type)).force_rooted,
516
- type_location: type_location,
517
- source: :rbs)
518
- end
519
- if type.type.rest_keywords
520
- name = type.type.rest_keywords.name ? type.type.rest_keywords.name.to_s : "arg_#{arg_num += 1}"
521
- parameters.push Solargraph::Pin::Parameter.new(decl: :kwrestarg, name: type.type.rest_keywords.name.to_s, closure: pin,
522
- source: :rbs, type_location: type_location)
523
- end
524
-
525
- rooted_tag = method_type_to_tag(type)
526
- return_type = ComplexType.try_parse(rooted_tag).force_rooted
527
- [parameters, return_type]
528
- end
529
-
530
- # @param decl [RBS::AST::Members::AttrReader,RBS::AST::Members::AttrAccessor]
531
- # @param closure [Pin::Namespace]
532
- # @param context [Context]
533
- # @return [void]
534
- def attr_reader_to_pin(decl, closure, context)
535
- name = decl.name.to_s
536
- final_scope = decl.kind == :instance ? :instance : :class
537
- visibility = calculate_method_visibility(decl, context, closure, final_scope, name)
538
- pin = Solargraph::Pin::Method.new(
539
- name: name,
540
- type_location: location_decl_to_pin_location(decl.location),
541
- closure: closure,
542
- comments: decl.comment&.string,
543
- scope: final_scope,
544
- attribute: true,
545
- visibility: visibility,
546
- source: :rbs
547
- )
548
- rooted_tag = ComplexType.parse(other_type_to_tag(decl.type)).force_rooted.rooted_tags
549
- pin.docstring.add_tag(YARD::Tags::Tag.new(:return, '', rooted_tag))
550
- logger.debug { "Conversions#attr_reader_to_pin(name=#{name.inspect}, visibility=#{visibility.inspect}) => #{pin.inspect}" }
551
- pins.push pin
552
- end
553
-
554
- # @param decl [RBS::AST::Members::AttrWriter, RBS::AST::Members::AttrAccessor]
555
- # @param closure [Pin::Namespace]
556
- # @param context [Context]
557
- # @return [void]
558
- def attr_writer_to_pin(decl, closure, context)
559
- final_scope = decl.kind == :instance ? :instance : :class
560
- name = "#{decl.name.to_s}="
561
- visibility = calculate_method_visibility(decl, context, closure, final_scope, name)
562
- type_location = location_decl_to_pin_location(decl.location)
563
- pin = Solargraph::Pin::Method.new(
564
- name: name,
565
- type_location: type_location,
566
- closure: closure,
567
- parameters: [],
568
- comments: decl.comment&.string,
569
- scope: final_scope,
570
- attribute: true,
571
- visibility: visibility,
572
- source: :rbs
573
- )
574
- pin.parameters <<
575
- Solargraph::Pin::Parameter.new(
576
- name: 'value',
577
- return_type: ComplexType.try_parse(other_type_to_tag(decl.type)).force_rooted,
578
- source: :rbs,
579
- closure: pin,
580
- type_location: type_location
581
- )
582
- rooted_tag = ComplexType.parse(other_type_to_tag(decl.type)).force_rooted.rooted_tags
583
- pin.docstring.add_tag(YARD::Tags::Tag.new(:return, '', rooted_tag))
584
- pins.push pin
585
- end
586
-
587
- # @param decl [RBS::AST::Members::AttrAccessor]
588
- # @param closure [Pin::Namespace]
589
- # @param context [Context]
590
- # @return [void]
591
- def attr_accessor_to_pin(decl, closure, context)
592
- attr_reader_to_pin(decl, closure, context)
593
- attr_writer_to_pin(decl, closure, context)
594
- end
595
-
596
- # @param decl [RBS::AST::Members::InstanceVariable]
597
- # @param closure [Pin::Namespace]
598
- # @return [void]
599
- def ivar_to_pin(decl, closure)
600
- pin = Solargraph::Pin::InstanceVariable.new(
601
- name: decl.name.to_s,
602
- closure: closure,
603
- type_location: location_decl_to_pin_location(decl.location),
604
- comments: decl.comment&.string,
605
- source: :rbs
606
- )
607
- rooted_tag = ComplexType.parse(other_type_to_tag(decl.type)).force_rooted.rooted_tags
608
- pin.docstring.add_tag(YARD::Tags::Tag.new(:type, '', rooted_tag))
609
- pins.push pin
610
- end
611
-
612
- # @param decl [RBS::AST::Members::ClassVariable]
613
- # @param closure [Pin::Namespace]
614
- # @return [void]
615
- def cvar_to_pin(decl, closure)
616
- name = decl.name.to_s
617
- pin = Solargraph::Pin::ClassVariable.new(
618
- name: name,
619
- closure: closure,
620
- comments: decl.comment&.string,
621
- type_location: location_decl_to_pin_location(decl.location),
622
- source: :rbs
623
- )
624
- rooted_tag = ComplexType.parse(other_type_to_tag(decl.type)).force_rooted.rooted_tags
625
- pin.docstring.add_tag(YARD::Tags::Tag.new(:type, '', rooted_tag))
626
- pins.push pin
627
- end
628
-
629
- # @param decl [RBS::AST::Members::ClassInstanceVariable]
630
- # @param closure [Pin::Namespace]
631
- # @return [void]
632
- def civar_to_pin(decl, closure)
633
- name = decl.name.to_s
634
- pin = Solargraph::Pin::InstanceVariable.new(
635
- name: name,
636
- closure: closure,
637
- comments: decl.comment&.string,
638
- type_location: location_decl_to_pin_location(decl.location),
639
- source: :rbs
640
- )
641
- rooted_tag = ComplexType.parse(other_type_to_tag(decl.type)).force_rooted.rooted_tags
642
- pin.docstring.add_tag(YARD::Tags::Tag.new(:type, '', rooted_tag))
643
- pins.push pin
644
- end
645
-
646
- # @param decl [RBS::AST::Members::Include]
647
- # @param closure [Pin::Namespace]
648
- # @return [void]
649
- def include_to_pin decl, closure
650
- type = build_type(decl.name, decl.args)
651
- generic_values = type.all_params.map(&:to_s)
652
- pins.push Solargraph::Pin::Reference::Include.new(
653
- name: decl.name.relative!.to_s,
654
- type_location: location_decl_to_pin_location(decl.location),
655
- generic_values: generic_values,
656
- closure: closure,
657
- source: :rbs
658
- )
659
- end
660
-
661
- # @param decl [RBS::AST::Members::Prepend]
662
- # @param closure [Pin::Namespace]
663
- # @return [void]
664
- def prepend_to_pin decl, closure
665
- pins.push Solargraph::Pin::Reference::Prepend.new(
666
- name: decl.name.relative!.to_s,
667
- type_location: location_decl_to_pin_location(decl.location),
668
- closure: closure,
669
- source: :rbs
670
- )
671
- end
672
-
673
- # @param decl [RBS::AST::Members::Extend]
674
- # @param closure [Pin::Namespace]
675
- # @return [void]
676
- def extend_to_pin decl, closure
677
- pins.push Solargraph::Pin::Reference::Extend.new(
678
- name: decl.name.relative!.to_s,
679
- type_location: location_decl_to_pin_location(decl.location),
680
- closure: closure,
681
- source: :rbs
682
- )
683
- end
684
-
685
- # @param decl [RBS::AST::Members::Alias]
686
- # @param closure [Pin::Namespace]
687
- # @return [void]
688
- def alias_to_pin decl, closure
689
- final_scope = decl.singleton? ? :class : :instance
690
- pins.push Solargraph::Pin::MethodAlias.new(
691
- name: decl.new_name.to_s,
692
- type_location: location_decl_to_pin_location(decl.location),
693
- original: decl.old_name.to_s,
694
- closure: closure,
695
- scope: final_scope,
696
- source: :rbs,
697
- )
698
- end
699
-
700
- RBS_TO_YARD_TYPE = {
701
- 'bool' => 'Boolean',
702
- 'string' => 'String',
703
- 'int' => 'Integer',
704
- 'untyped' => '',
705
- 'NilClass' => 'nil'
706
- }
707
-
708
- # @param type [RBS::MethodType]
709
- # @return [String]
710
- def method_type_to_tag type
711
- if type_aliases.key?(type.type.return_type.to_s)
712
- other_type_to_tag(type_aliases[type.type.return_type.to_s].type)
713
- else
714
- other_type_to_tag type.type.return_type
715
- end
716
- end
717
-
718
- # @param type_name [RBS::TypeName]
719
- # @param type_args [Enumerable<RBS::Types::Bases::Base>]
720
- # @return [ComplexType::UniqueType]
721
- def build_type(type_name, type_args = [])
722
- base = RBS_TO_YARD_TYPE[type_name.relative!.to_s] || type_name.relative!.to_s
723
- params = type_args.map { |a| other_type_to_tag(a) }.map do |t|
724
- ComplexType.try_parse(t).force_rooted
725
- end
726
- if base == 'Hash' && params.length == 2
727
- ComplexType::UniqueType.new(base, [params.first], [params.last], rooted: true, parameters_type: :hash)
728
- else
729
- ComplexType::UniqueType.new(base, [], params.reject(&:undefined?), rooted: true, parameters_type: :list)
730
- end
731
- end
732
-
733
- # @param type_name [RBS::TypeName]
734
- # @param type_args [Enumerable<RBS::Types::Bases::Base>]
735
- # @return [String]
736
- def type_tag(type_name, type_args = [])
737
- build_type(type_name, type_args).tags
738
- end
739
-
740
- # @param type [RBS::Types::Bases::Base]
741
- # @return [String]
742
- def other_type_to_tag type
743
- if type.is_a?(RBS::Types::Optional)
744
- "#{other_type_to_tag(type.type)}, nil"
745
- elsif type.is_a?(RBS::Types::Bases::Any)
746
- 'undefined'
747
- elsif type.is_a?(RBS::Types::Bases::Bool)
748
- 'Boolean'
749
- elsif type.is_a?(RBS::Types::Tuple)
750
- "Array(#{type.types.map { |t| other_type_to_tag(t) }.join(', ')})"
751
- elsif type.is_a?(RBS::Types::Literal)
752
- type.literal.inspect
753
- elsif type.is_a?(RBS::Types::Union)
754
- type.types.map { |t| other_type_to_tag(t) }.join(', ')
755
- elsif type.is_a?(RBS::Types::Record)
756
- # @todo Better record support
757
- 'Hash'
758
- elsif type.is_a?(RBS::Types::Bases::Nil)
759
- 'nil'
760
- elsif type.is_a?(RBS::Types::Bases::Self)
761
- 'self'
762
- elsif type.is_a?(RBS::Types::Bases::Void)
763
- 'void'
764
- elsif type.is_a?(RBS::Types::Variable)
765
- "#{Solargraph::ComplexType::GENERIC_TAG_NAME}<#{type.name}>"
766
- elsif type.is_a?(RBS::Types::ClassInstance) #&& !type.args.empty?
767
- type_tag(type.name, type.args)
768
- elsif type.is_a?(RBS::Types::Bases::Instance)
769
- 'self'
770
- elsif type.is_a?(RBS::Types::Bases::Top)
771
- # top is the most super superclass
772
- 'BasicObject'
773
- elsif type.is_a?(RBS::Types::Bases::Bottom)
774
- # bottom is used in contexts where nothing will ever return
775
- # - e.g., it could be the return type of 'exit()' or 'raise'
776
- #
777
- # @todo define a specific bottom type and use it to
778
- # determine dead code
779
- 'undefined'
780
- elsif type.is_a?(RBS::Types::Intersection)
781
- type.types.map { |member| other_type_to_tag(member) }.join(', ')
782
- elsif type.is_a?(RBS::Types::Proc)
783
- 'Proc'
784
- elsif type.is_a?(RBS::Types::Alias)
785
- # type-level alias use - e.g., 'bool' in "type bool = true | false"
786
- # @todo ensure these get resolved after processing all aliases
787
- # @todo handle recursive aliases
788
- type_tag(type.name, type.args)
789
- elsif type.is_a?(RBS::Types::Interface)
790
- # represents a mix-in module which can be considered a
791
- # subtype of a consumer of it
792
- type_tag(type.name, type.args)
793
- elsif type.is_a?(RBS::Types::ClassSingleton)
794
- # e.g., singleton(String)
795
- type_tag(type.name)
796
- else
797
- Solargraph.logger.warn "Unrecognized RBS type: #{type.class} at #{type.location}"
798
- 'undefined'
799
- end
800
- end
801
-
802
- # @param decl [RBS::AST::Declarations::Class, RBS::AST::Declarations::Module]
803
- # @param namespace [Pin::Namespace]
804
- # @return [void]
805
- def add_mixins decl, namespace
806
- # @param mixin [RBS::AST::Members::Include, RBS::AST::Members::Members::Extend, RBS::AST::Members::Members::Prepend]
807
- decl.each_mixin do |mixin|
808
- # @todo are we handling prepend correctly?
809
- klass = mixin.is_a?(RBS::AST::Members::Include) ? Pin::Reference::Include : Pin::Reference::Extend
810
- type = build_type(mixin.name, mixin.args)
811
- generic_values = type.all_params.map(&:to_s)
812
- pins.push klass.new(
813
- name: mixin.name.relative!.to_s,
814
- type_location: location_decl_to_pin_location(mixin.location),
815
- generic_values: generic_values,
816
- closure: namespace,
817
- source: :rbs
818
- )
819
- end
820
- end
821
- end
822
- end
823
- end
1
+ # frozen_string_literal: true
2
+
3
+ require 'rbs'
4
+
5
+ module Solargraph
6
+ class RbsMap
7
+ # Functions for converting RBS declarations to Solargraph pins
8
+ #
9
+ class Conversions
10
+ include Logging
11
+
12
+ # A container for tracking the current context of the RBS conversion
13
+ # process, e.g., what visibility is declared for methods in the current
14
+ # scope
15
+ #
16
+ class Context
17
+ attr_reader :visibility
18
+
19
+ # @param visibility [Symbol]
20
+ def initialize visibility = :public
21
+ @visibility = visibility
22
+ end
23
+ end
24
+
25
+ # @param loader [RBS::EnvironmentLoader]
26
+ def initialize(loader:)
27
+ @loader = loader
28
+ @pins = []
29
+ load_environment_to_pins(loader)
30
+ end
31
+
32
+ # @return [RBS::EnvironmentLoader]
33
+ attr_reader :loader
34
+
35
+ # @return [Array<Pin::Base>]
36
+ attr_reader :pins
37
+
38
+ private
39
+
40
+ # @return [Hash{String => RBS::AST::Declarations::TypeAlias}]
41
+ def type_aliases
42
+ @type_aliases ||= {}
43
+ end
44
+
45
+ # @param loader [RBS::EnvironmentLoader]
46
+ # @return [void]
47
+ def load_environment_to_pins(loader)
48
+ environment = RBS::Environment.from_loader(loader).resolve_type_names
49
+ cursor = pins.length
50
+ if environment.declarations.empty?
51
+ Solargraph.logger.info "No RBS declarations found in environment for core_root #{loader.core_root.inspect}, libraries #{loader.libs} and directories #{loader.dirs}"
52
+ return
53
+ end
54
+ environment.declarations.each { |decl| convert_decl_to_pin(decl, Solargraph::Pin::ROOT_PIN) }
55
+ end
56
+
57
+ # @param decl [RBS::AST::Declarations::Base]
58
+ # @param closure [Pin::Closure]
59
+ # @return [void]
60
+ def convert_decl_to_pin decl, closure
61
+ case decl
62
+ when RBS::AST::Declarations::Class
63
+ class_decl_to_pin decl
64
+ when RBS::AST::Declarations::Interface
65
+ # STDERR.puts "Skipping interface #{decl.name.relative!}"
66
+ interface_decl_to_pin decl, closure
67
+ when RBS::AST::Declarations::TypeAlias
68
+ # @sg-ignore https://github.com/castwide/solargraph/pull/1114
69
+ type_aliases[decl.name.to_s] = decl
70
+ when RBS::AST::Declarations::Module
71
+ module_decl_to_pin decl
72
+ when RBS::AST::Declarations::Constant
73
+ constant_decl_to_pin decl
74
+ when RBS::AST::Declarations::ClassAlias
75
+ class_alias_decl_to_pin decl
76
+ when RBS::AST::Declarations::ModuleAlias
77
+ module_alias_decl_to_pin decl
78
+ when RBS::AST::Declarations::Global
79
+ global_decl_to_pin decl
80
+ else
81
+ Solargraph.logger.warn "Skipping declaration #{decl.class}"
82
+ end
83
+ end
84
+
85
+ # @param decl [RBS::AST::Declarations::Module]
86
+ # @param module_pin [Pin::Namespace]
87
+ # @return [void]
88
+ def convert_self_types_to_pins decl, module_pin
89
+ decl.self_types.each { |self_type| context = convert_self_type_to_pins(self_type, module_pin) }
90
+ end
91
+
92
+ # @param decl [RBS::AST::Declarations::Module::Self]
93
+ # @param closure [Pin::Namespace]
94
+ # @return [void]
95
+ def convert_self_type_to_pins decl, closure
96
+ type = build_type(decl.name, decl.args)
97
+ generic_values = type.all_params.map(&:to_s)
98
+ include_pin = Solargraph::Pin::Reference::Include.new(
99
+ name: decl.name.relative!.to_s,
100
+ type_location: location_decl_to_pin_location(decl.location),
101
+ generic_values: generic_values,
102
+ closure: closure,
103
+ source: :rbs
104
+ )
105
+ pins.push include_pin
106
+ end
107
+
108
+ # @param decl [RBS::AST::Declarations::Module,RBS::AST::Declarations::Class,RBS::AST::Declarations::Interface]
109
+ # @param closure [Pin::Namespace]
110
+ # @return [void]
111
+ def convert_members_to_pins decl, closure
112
+ context = Conversions::Context.new
113
+ decl.members.each { |m| context = convert_member_to_pin(m, closure, context) }
114
+ end
115
+
116
+ # @param member [RBS::AST::Members::Base,RBS::AST::Declarations::Base]
117
+ # @param closure [Pin::Namespace]
118
+ # @param context [Context]
119
+ # @return [Context]
120
+ def convert_member_to_pin member, closure, context
121
+ case member
122
+ when RBS::AST::Members::MethodDefinition
123
+ method_def_to_pin(member, closure, context)
124
+ when RBS::AST::Members::AttrReader
125
+ attr_reader_to_pin(member, closure, context)
126
+ when RBS::AST::Members::AttrWriter
127
+ attr_writer_to_pin(member, closure, context)
128
+ when RBS::AST::Members::AttrAccessor
129
+ attr_accessor_to_pin(member, closure, context)
130
+ when RBS::AST::Members::Include
131
+ include_to_pin(member, closure)
132
+ when RBS::AST::Members::Prepend
133
+ prepend_to_pin(member, closure)
134
+ when RBS::AST::Members::Extend
135
+ extend_to_pin(member, closure)
136
+ when RBS::AST::Members::Alias
137
+ alias_to_pin(member, closure)
138
+ when RBS::AST::Members::ClassInstanceVariable
139
+ civar_to_pin(member, closure)
140
+ when RBS::AST::Members::ClassVariable
141
+ cvar_to_pin(member, closure)
142
+ when RBS::AST::Members::InstanceVariable
143
+ ivar_to_pin(member, closure)
144
+ when RBS::AST::Members::Public
145
+ return Context.new(:public)
146
+ when RBS::AST::Members::Private
147
+ return Context.new(:private)
148
+ when RBS::AST::Declarations::Base
149
+ convert_decl_to_pin(member, closure)
150
+ else
151
+ Solargraph.logger.warn "Skipping member type #{member.class}"
152
+ end
153
+ context
154
+ end
155
+
156
+ # @param decl [RBS::AST::Declarations::Class]
157
+ # @return [void]
158
+ def class_decl_to_pin decl
159
+ generics = decl.type_params.map(&:name).map(&:to_s)
160
+ generic_defaults = {}
161
+ decl.type_params.each do |param|
162
+ if param.default_type
163
+ tag = other_type_to_tag param.default_type
164
+ generic_defaults[param.name.to_s] = ComplexType.parse(tag).force_rooted
165
+ end
166
+ end
167
+ class_name = decl.name.relative!.to_s
168
+ class_pin = Solargraph::Pin::Namespace.new(
169
+ type: :class,
170
+ name: class_name,
171
+ closure: Solargraph::Pin::ROOT_PIN,
172
+ comments: decl.comment&.string,
173
+ type_location: location_decl_to_pin_location(decl.location),
174
+ # @todo some type parameters in core/stdlib have default
175
+ # values; Solargraph doesn't support that yet as so these
176
+ # get treated as undefined if not specified
177
+ generics: generics,
178
+ generic_defaults: generic_defaults,
179
+ source: :rbs
180
+ )
181
+ pins.push class_pin
182
+ if decl.super_class
183
+ type = build_type(decl.super_class.name, decl.super_class.args)
184
+ generic_values = type.all_params.map(&:to_s)
185
+ superclass_name = decl.super_class.name.to_s
186
+ pins.push Solargraph::Pin::Reference::Superclass.new(
187
+ type_location: location_decl_to_pin_location(decl.super_class.location),
188
+ closure: class_pin,
189
+ generic_values: generic_values,
190
+ name: superclass_name,
191
+ source: :rbs
192
+ )
193
+ end
194
+ add_mixins decl, class_pin
195
+ convert_members_to_pins decl, class_pin
196
+ end
197
+
198
+ # @param decl [RBS::AST::Declarations::Interface]
199
+ # @param closure [Pin::Closure]
200
+ # @return [void]
201
+ def interface_decl_to_pin decl, closure
202
+ class_pin = Solargraph::Pin::Namespace.new(
203
+ type: :module,
204
+ type_location: location_decl_to_pin_location(decl.location),
205
+ name: decl.name.relative!.to_s,
206
+ closure: Solargraph::Pin::ROOT_PIN,
207
+ comments: decl.comment&.string,
208
+ generics: decl.type_params.map(&:name).map(&:to_s),
209
+ # HACK: Using :hidden to keep interfaces from appearing in
210
+ # autocompletion
211
+ visibility: :hidden,
212
+ source: :rbs
213
+ )
214
+ class_pin.docstring.add_tag(YARD::Tags::Tag.new(:abstract, '(RBS interface)'))
215
+ pins.push class_pin
216
+ convert_members_to_pins decl, class_pin
217
+ end
218
+
219
+ # @param decl [RBS::AST::Declarations::Module]
220
+ # @return [void]
221
+ def module_decl_to_pin decl
222
+ module_pin = Solargraph::Pin::Namespace.new(
223
+ type: :module,
224
+ name: decl.name.relative!.to_s,
225
+ type_location: location_decl_to_pin_location(decl.location),
226
+ closure: Solargraph::Pin::ROOT_PIN,
227
+ comments: decl.comment&.string,
228
+ generics: decl.type_params.map(&:name).map(&:to_s),
229
+ source: :rbs
230
+ )
231
+ pins.push module_pin
232
+ convert_self_types_to_pins decl, module_pin
233
+ convert_members_to_pins decl, module_pin
234
+
235
+ add_mixins decl, module_pin.closure
236
+ end
237
+
238
+ # @param name [String]
239
+ # @param tag [String]
240
+ # @param comments [String]
241
+ # @param decl [RBS::AST::Declarations::ClassAlias, RBS::AST::Declarations::Constant, RBS::AST::Declarations::ModuleAlias]
242
+ # @param base [String, nil] Optional conversion of tag to base<tag>
243
+ #
244
+ # @return [Solargraph::Pin::Constant]
245
+ def create_constant(name, tag, comments, decl, base = nil)
246
+ parts = name.split('::')
247
+ if parts.length > 1
248
+ name = parts.last
249
+ closure = pins.select { |pin| pin && pin.path == parts[0..-2].join('::') }.first
250
+ else
251
+ name = parts.first
252
+ closure = Solargraph::Pin::ROOT_PIN
253
+ end
254
+ constant_pin = Solargraph::Pin::Constant.new(
255
+ name: name,
256
+ closure: closure,
257
+ type_location: location_decl_to_pin_location(decl.location),
258
+ comments: comments,
259
+ source: :rbs
260
+ )
261
+ tag = "#{base}<#{tag}>" if base
262
+ rooted_tag = ComplexType.parse(tag).force_rooted.rooted_tags
263
+ constant_pin.docstring.add_tag(YARD::Tags::Tag.new(:return, '', rooted_tag))
264
+ constant_pin
265
+ end
266
+
267
+ # @param decl [RBS::AST::Declarations::ClassAlias]
268
+ # @return [void]
269
+ def class_alias_decl_to_pin decl
270
+ # See https://www.rubydoc.info/gems/rbs/3.4.3/RBS/AST/Declarations/ClassAlias
271
+ new_name = decl.new_name.relative!.to_s
272
+ old_name = decl.old_name.relative!.to_s
273
+
274
+ pins.push create_constant(new_name, old_name, decl.comment&.string, decl, 'Class')
275
+ end
276
+
277
+ # @param decl [RBS::AST::Declarations::ModuleAlias]
278
+ # @return [void]
279
+ def module_alias_decl_to_pin decl
280
+ # See https://www.rubydoc.info/gems/rbs/3.4.3/RBS/AST/Declarations/ModuleAlias
281
+ new_name = decl.new_name.relative!.to_s
282
+ old_name = decl.old_name.relative!.to_s
283
+
284
+ pins.push create_constant(new_name, old_name, decl.comment&.string, decl, 'Module')
285
+ end
286
+
287
+ # @param decl [RBS::AST::Declarations::Constant]
288
+ # @return [void]
289
+ def constant_decl_to_pin decl
290
+ tag = other_type_to_tag(decl.type)
291
+ pins.push create_constant(decl.name.relative!.to_s, tag, decl.comment&.string, decl)
292
+ end
293
+
294
+ # @param decl [RBS::AST::Declarations::Global]
295
+ # @return [void]
296
+ def global_decl_to_pin decl
297
+ closure = Solargraph::Pin::ROOT_PIN
298
+ name = decl.name.to_s
299
+ pin = Solargraph::Pin::GlobalVariable.new(
300
+ name: name,
301
+ closure: closure,
302
+ comments: decl.comment&.string,
303
+ type_location: location_decl_to_pin_location(decl.location),
304
+ source: :rbs
305
+ )
306
+ rooted_tag = ComplexType.parse(other_type_to_tag(decl.type)).force_rooted.rooted_tags
307
+ pin.docstring.add_tag(YARD::Tags::Tag.new(:type, '', rooted_tag))
308
+ pins.push pin
309
+ end
310
+
311
+
312
+ # Visibility overrides that will allow the Solargraph project
313
+ # and plugins to pass typechecking using SOLARGRAPH_ASSERTS=on,
314
+ # so that we can detect any regressions/issues elsewhere in the
315
+ # visibility logic.
316
+ #
317
+ # These should either reflect a bug upstream in the RBS
318
+ # definitions, or include a @todo indicating what needs to be
319
+ # fixed in Solargraph to properly understand it.
320
+ #
321
+ # @todo PR these fixes upstream and list open PRs here above
322
+ # related overrides
323
+ # @todo externalize remaining overrides into yaml file, then
324
+ # allow that to be extended via .solargraph.yml
325
+ # @type [Hash{Array(String, Symbol, String) => Symbol}
326
+ VISIBILITY_OVERRIDE = {
327
+ ["Rails::Engine", :instance, "run_tasks_blocks"] => :protected,
328
+ # Should have been marked as both instance and class method in module -e.g., 'module_function'
329
+ ["Kernel", :instance, "pretty_inspect"] => :private,
330
+ # marked incorrectly in RBS
331
+ ["WEBrick::HTTPUtils::FormData", :instance, "next_data"] => :protected,
332
+ ["Rails::Command", :class, "command_type"] => :private,
333
+ ["Rails::Command", :class, "lookup_paths"] => :private,
334
+ ["Rails::Command", :class, "file_lookup_paths"] => :private,
335
+ ["Rails::Railtie", :instance, "run_console_blocks"] => :protected,
336
+ ["Rails::Railtie", :instance, "run_generators_blocks"] => :protected,
337
+ ["Rails::Railtie", :instance, "run_runner_blocks"] => :protected,
338
+ ["Rails::Railtie", :instance, "run_tasks_blocks"] => :protected,
339
+ ["ActionController::Base", :instance, "_protected_ivars"] => :private,
340
+ ["ActionView::Template", :instance, "method_name"] => :public,
341
+ ["Module", :instance, "ruby2_keywords"] => :private,
342
+ ["Nokogiri::XML::Node", :instance, "coerce"] => :protected,
343
+ ["Nokogiri::XML::Document", :class, "empty_doc?"] => :private,
344
+ ["Nokogiri::Decorators::Slop", :instance, "respond_to_missing?"] => :public,
345
+ ["RuboCop::Cop::RangeHelp", :instance, "source_range"] => :private,
346
+ ["AST::Node", :instance, "original_dup"] => :private,
347
+ ["Rainbow::Presenter", :instance, "wrap_with_sgr"] => :private,
348
+ }
349
+
350
+ # @param decl [RBS::AST::Members::MethodDefinition, RBS::AST::Members::AttrReader, RBS::AST::Members::AttrAccessor]
351
+ # @param closure [Pin::Closure]
352
+ # @param context [Context]
353
+ # @param scope [Symbol] :instance or :class
354
+ # @param name [String] The name of the method
355
+ # @sg-ignore
356
+ # @return [Symbol]
357
+ def calculate_method_visibility(decl, context, closure, scope, name)
358
+ override_key = [closure.path, scope, name]
359
+ visibility = VISIBILITY_OVERRIDE[override_key]
360
+ simple_override_key = [closure.path, scope]
361
+ visibility ||= VISIBILITY_OVERRIDE[simple_override_key]
362
+ visibility ||= :private if closure.path == 'Kernel' && Kernel.private_instance_methods(false).include?(decl.name)
363
+ if decl.kind == :singleton_instance
364
+ # this is a 'module function'
365
+ visibility ||= :private
366
+ end
367
+ visibility ||= decl.visibility
368
+ visibility ||= context.visibility
369
+ visibility ||= :public
370
+ visibility
371
+ end
372
+
373
+ # @param decl [RBS::AST::Members::MethodDefinition]
374
+ # @param closure [Pin::Closure]
375
+ # @param context [Context]
376
+ # @return [void]
377
+ def method_def_to_pin decl, closure, context
378
+ # there may be edge cases here around different signatures
379
+ # having different type params / orders - we may need to match
380
+ # this data model and have generics live in signatures to
381
+ # handle those correctly
382
+ generics = decl.overloads.map(&:method_type).flat_map(&:type_params).map(&:name).map(&:to_s).uniq
383
+
384
+ if decl.instance?
385
+ name = decl.name.to_s
386
+ final_scope = :instance
387
+ visibility = calculate_method_visibility(decl, context, closure, final_scope, name)
388
+ pin = Solargraph::Pin::Method.new(
389
+ name: name,
390
+ closure: closure,
391
+ type_location: location_decl_to_pin_location(decl.location),
392
+ comments: decl.comment&.string,
393
+ scope: final_scope,
394
+ signatures: [],
395
+ generics: generics,
396
+ visibility: visibility,
397
+ source: :rbs
398
+ )
399
+ pin.signatures.concat method_def_to_sigs(decl, pin)
400
+ pins.push pin
401
+ if pin.name == 'initialize'
402
+ pin.instance_variable_set(:@visibility, :private)
403
+ pin.instance_variable_set(:@return_type, ComplexType::VOID)
404
+ end
405
+ end
406
+ if decl.singleton?
407
+ final_scope = :class
408
+ name = decl.name.to_s
409
+ visibility = calculate_method_visibility(decl, context, closure, final_scope, name)
410
+ pin = Solargraph::Pin::Method.new(
411
+ name: name,
412
+ closure: closure,
413
+ comments: decl.comment&.string,
414
+ type_location: location_decl_to_pin_location(decl.location),
415
+ visibility: visibility,
416
+ scope: final_scope,
417
+ signatures: [],
418
+ generics: generics,
419
+ source: :rbs
420
+ )
421
+ pin.signatures.concat method_def_to_sigs(decl, pin)
422
+ pins.push pin
423
+ end
424
+ end
425
+
426
+ # @param decl [RBS::AST::Members::MethodDefinition]
427
+ # @param pin [Pin::Method]
428
+ # @return [void]
429
+ def method_def_to_sigs decl, pin
430
+ # @param overload [RBS::AST::Members::MethodDefinition::Overload]
431
+ decl.overloads.map do |overload|
432
+ type_location = location_decl_to_pin_location(overload.method_type.location)
433
+ generics = overload.method_type.type_params.map(&:name).map(&:to_s)
434
+ signature_parameters, signature_return_type = parts_of_function(overload.method_type, pin)
435
+ block = if overload.method_type.block
436
+ block_parameters, block_return_type = parts_of_function(overload.method_type.block, pin)
437
+ Pin::Signature.new(generics: generics, parameters: block_parameters, return_type: block_return_type, source: :rbs,
438
+ type_location: type_location, closure: pin)
439
+ end
440
+ Pin::Signature.new(generics: generics, parameters: signature_parameters, return_type: signature_return_type, block: block, source: :rbs,
441
+ type_location: type_location, closure: pin)
442
+ end
443
+ end
444
+
445
+ # @param location [RBS::Location, nil]
446
+ # @return [Solargraph::Location, nil]
447
+ def location_decl_to_pin_location(location)
448
+ return nil if location&.name.nil?
449
+
450
+ start_pos = Position.new(location.start_line - 1, location.start_column)
451
+ end_pos = Position.new(location.end_line - 1, location.end_column)
452
+ range = Range.new(start_pos, end_pos)
453
+ Location.new(location.name.to_s, range)
454
+ end
455
+
456
+ # @param type [RBS::MethodType,RBS::Types::Block]
457
+ # @param pin [Pin::Method]
458
+ # @return [Array(Array<Pin::Parameter>, ComplexType)]
459
+ def parts_of_function type, pin
460
+ type_location = pin.type_location
461
+ if defined?(RBS::Types::UntypedFunction) && type.type.is_a?(RBS::Types::UntypedFunction)
462
+ return [
463
+ [Solargraph::Pin::Parameter.new(decl: :restarg, name: 'arg', closure: pin, source: :rbs, type_location: type_location)],
464
+ ComplexType.try_parse(method_type_to_tag(type)).force_rooted
465
+ ]
466
+ end
467
+
468
+ parameters = []
469
+ arg_num = -1
470
+ type.type.required_positionals.each do |param|
471
+ # @sg-ignore RBS generic type understanding issue
472
+ name = param.name ? param.name.to_s : "arg_#{arg_num += 1}"
473
+ # @sg-ignore RBS generic type understanding issue
474
+ parameters.push Solargraph::Pin::Parameter.new(decl: :arg, name: name, closure: pin, return_type: ComplexType.try_parse(other_type_to_tag(param.type)).force_rooted, source: :rbs, type_location: type_location)
475
+ end
476
+ type.type.optional_positionals.each do |param|
477
+ # @sg-ignore RBS generic type understanding issue
478
+ name = param.name ? param.name.to_s : "arg_#{arg_num += 1}"
479
+ parameters.push Solargraph::Pin::Parameter.new(decl: :optarg, name: name, closure: pin,
480
+ # @sg-ignore RBS generic type understanding issue
481
+ return_type: ComplexType.try_parse(other_type_to_tag(param.type)).force_rooted,
482
+ type_location: type_location,
483
+ source: :rbs)
484
+ end
485
+ if type.type.rest_positionals
486
+ name = type.type.rest_positionals.name ? type.type.rest_positionals.name.to_s : "arg_#{arg_num += 1}"
487
+ inner_rest_positional_type =
488
+ ComplexType.try_parse(other_type_to_tag(type.type.rest_positionals.type))
489
+ rest_positional_type = ComplexType::UniqueType.new('Array',
490
+ [],
491
+ [inner_rest_positional_type],
492
+ rooted: true, parameters_type: :list)
493
+ parameters.push Solargraph::Pin::Parameter.new(decl: :restarg, name: name, closure: pin,
494
+ source: :rbs, type_location: type_location,
495
+ return_type: rest_positional_type,)
496
+ end
497
+ type.type.trailing_positionals.each do |param|
498
+ # @sg-ignore RBS generic type understanding issue
499
+ name = param.name ? param.name.to_s : "arg_#{arg_num += 1}"
500
+ parameters.push Solargraph::Pin::Parameter.new(decl: :arg, name: name, closure: pin, source: :rbs, type_location: type_location)
501
+ end
502
+ type.type.required_keywords.each do |orig, param|
503
+ # @sg-ignore RBS generic type understanding issue
504
+ name = orig ? orig.to_s : "arg_#{arg_num += 1}"
505
+ parameters.push Solargraph::Pin::Parameter.new(decl: :kwarg, name: name, closure: pin,
506
+ # @sg-ignore RBS generic type understanding issue
507
+ return_type: ComplexType.try_parse(other_type_to_tag(param.type)).force_rooted,
508
+ source: :rbs, type_location: type_location)
509
+ end
510
+ type.type.optional_keywords.each do |orig, param|
511
+ # @sg-ignore RBS generic type understanding issue
512
+ name = orig ? orig.to_s : "arg_#{arg_num += 1}"
513
+ parameters.push Solargraph::Pin::Parameter.new(decl: :kwoptarg, name: name, closure: pin,
514
+ # @sg-ignore RBS generic type understanding issue
515
+ return_type: ComplexType.try_parse(other_type_to_tag(param.type)).force_rooted,
516
+ type_location: type_location,
517
+ source: :rbs)
518
+ end
519
+ if type.type.rest_keywords
520
+ name = type.type.rest_keywords.name ? type.type.rest_keywords.name.to_s : "arg_#{arg_num += 1}"
521
+ parameters.push Solargraph::Pin::Parameter.new(decl: :kwrestarg, name: type.type.rest_keywords.name.to_s, closure: pin,
522
+ source: :rbs, type_location: type_location)
523
+ end
524
+
525
+ rooted_tag = method_type_to_tag(type)
526
+ return_type = ComplexType.try_parse(rooted_tag).force_rooted
527
+ [parameters, return_type]
528
+ end
529
+
530
+ # @param decl [RBS::AST::Members::AttrReader,RBS::AST::Members::AttrAccessor]
531
+ # @param closure [Pin::Namespace]
532
+ # @param context [Context]
533
+ # @return [void]
534
+ def attr_reader_to_pin(decl, closure, context)
535
+ name = decl.name.to_s
536
+ final_scope = decl.kind == :instance ? :instance : :class
537
+ visibility = calculate_method_visibility(decl, context, closure, final_scope, name)
538
+ pin = Solargraph::Pin::Method.new(
539
+ name: name,
540
+ type_location: location_decl_to_pin_location(decl.location),
541
+ closure: closure,
542
+ comments: decl.comment&.string,
543
+ scope: final_scope,
544
+ attribute: true,
545
+ visibility: visibility,
546
+ source: :rbs
547
+ )
548
+ rooted_tag = ComplexType.parse(other_type_to_tag(decl.type)).force_rooted.rooted_tags
549
+ pin.docstring.add_tag(YARD::Tags::Tag.new(:return, '', rooted_tag))
550
+ logger.debug { "Conversions#attr_reader_to_pin(name=#{name.inspect}, visibility=#{visibility.inspect}) => #{pin.inspect}" }
551
+ pins.push pin
552
+ end
553
+
554
+ # @param decl [RBS::AST::Members::AttrWriter, RBS::AST::Members::AttrAccessor]
555
+ # @param closure [Pin::Namespace]
556
+ # @param context [Context]
557
+ # @return [void]
558
+ def attr_writer_to_pin(decl, closure, context)
559
+ final_scope = decl.kind == :instance ? :instance : :class
560
+ name = "#{decl.name.to_s}="
561
+ visibility = calculate_method_visibility(decl, context, closure, final_scope, name)
562
+ type_location = location_decl_to_pin_location(decl.location)
563
+ pin = Solargraph::Pin::Method.new(
564
+ name: name,
565
+ type_location: type_location,
566
+ closure: closure,
567
+ parameters: [],
568
+ comments: decl.comment&.string,
569
+ scope: final_scope,
570
+ attribute: true,
571
+ visibility: visibility,
572
+ source: :rbs
573
+ )
574
+ pin.parameters <<
575
+ Solargraph::Pin::Parameter.new(
576
+ name: 'value',
577
+ return_type: ComplexType.try_parse(other_type_to_tag(decl.type)).force_rooted,
578
+ source: :rbs,
579
+ closure: pin,
580
+ type_location: type_location
581
+ )
582
+ rooted_tag = ComplexType.parse(other_type_to_tag(decl.type)).force_rooted.rooted_tags
583
+ pin.docstring.add_tag(YARD::Tags::Tag.new(:return, '', rooted_tag))
584
+ pins.push pin
585
+ end
586
+
587
+ # @param decl [RBS::AST::Members::AttrAccessor]
588
+ # @param closure [Pin::Namespace]
589
+ # @param context [Context]
590
+ # @return [void]
591
+ def attr_accessor_to_pin(decl, closure, context)
592
+ attr_reader_to_pin(decl, closure, context)
593
+ attr_writer_to_pin(decl, closure, context)
594
+ end
595
+
596
+ # @param decl [RBS::AST::Members::InstanceVariable]
597
+ # @param closure [Pin::Namespace]
598
+ # @return [void]
599
+ def ivar_to_pin(decl, closure)
600
+ pin = Solargraph::Pin::InstanceVariable.new(
601
+ name: decl.name.to_s,
602
+ closure: closure,
603
+ type_location: location_decl_to_pin_location(decl.location),
604
+ comments: decl.comment&.string,
605
+ source: :rbs
606
+ )
607
+ rooted_tag = ComplexType.parse(other_type_to_tag(decl.type)).force_rooted.rooted_tags
608
+ pin.docstring.add_tag(YARD::Tags::Tag.new(:type, '', rooted_tag))
609
+ pins.push pin
610
+ end
611
+
612
+ # @param decl [RBS::AST::Members::ClassVariable]
613
+ # @param closure [Pin::Namespace]
614
+ # @return [void]
615
+ def cvar_to_pin(decl, closure)
616
+ name = decl.name.to_s
617
+ pin = Solargraph::Pin::ClassVariable.new(
618
+ name: name,
619
+ closure: closure,
620
+ comments: decl.comment&.string,
621
+ type_location: location_decl_to_pin_location(decl.location),
622
+ source: :rbs
623
+ )
624
+ rooted_tag = ComplexType.parse(other_type_to_tag(decl.type)).force_rooted.rooted_tags
625
+ pin.docstring.add_tag(YARD::Tags::Tag.new(:type, '', rooted_tag))
626
+ pins.push pin
627
+ end
628
+
629
+ # @param decl [RBS::AST::Members::ClassInstanceVariable]
630
+ # @param closure [Pin::Namespace]
631
+ # @return [void]
632
+ def civar_to_pin(decl, closure)
633
+ name = decl.name.to_s
634
+ pin = Solargraph::Pin::InstanceVariable.new(
635
+ name: name,
636
+ closure: closure,
637
+ comments: decl.comment&.string,
638
+ type_location: location_decl_to_pin_location(decl.location),
639
+ source: :rbs
640
+ )
641
+ rooted_tag = ComplexType.parse(other_type_to_tag(decl.type)).force_rooted.rooted_tags
642
+ pin.docstring.add_tag(YARD::Tags::Tag.new(:type, '', rooted_tag))
643
+ pins.push pin
644
+ end
645
+
646
+ # @param decl [RBS::AST::Members::Include]
647
+ # @param closure [Pin::Namespace]
648
+ # @return [void]
649
+ def include_to_pin decl, closure
650
+ type = build_type(decl.name, decl.args)
651
+ generic_values = type.all_params.map(&:to_s)
652
+ pins.push Solargraph::Pin::Reference::Include.new(
653
+ name: decl.name.relative!.to_s,
654
+ type_location: location_decl_to_pin_location(decl.location),
655
+ generic_values: generic_values,
656
+ closure: closure,
657
+ source: :rbs
658
+ )
659
+ end
660
+
661
+ # @param decl [RBS::AST::Members::Prepend]
662
+ # @param closure [Pin::Namespace]
663
+ # @return [void]
664
+ def prepend_to_pin decl, closure
665
+ pins.push Solargraph::Pin::Reference::Prepend.new(
666
+ name: decl.name.relative!.to_s,
667
+ type_location: location_decl_to_pin_location(decl.location),
668
+ closure: closure,
669
+ source: :rbs
670
+ )
671
+ end
672
+
673
+ # @param decl [RBS::AST::Members::Extend]
674
+ # @param closure [Pin::Namespace]
675
+ # @return [void]
676
+ def extend_to_pin decl, closure
677
+ pins.push Solargraph::Pin::Reference::Extend.new(
678
+ name: decl.name.relative!.to_s,
679
+ type_location: location_decl_to_pin_location(decl.location),
680
+ closure: closure,
681
+ source: :rbs
682
+ )
683
+ end
684
+
685
+ # @param decl [RBS::AST::Members::Alias]
686
+ # @param closure [Pin::Namespace]
687
+ # @return [void]
688
+ def alias_to_pin decl, closure
689
+ final_scope = decl.singleton? ? :class : :instance
690
+ pins.push Solargraph::Pin::MethodAlias.new(
691
+ name: decl.new_name.to_s,
692
+ type_location: location_decl_to_pin_location(decl.location),
693
+ original: decl.old_name.to_s,
694
+ closure: closure,
695
+ scope: final_scope,
696
+ source: :rbs,
697
+ )
698
+ end
699
+
700
+ RBS_TO_YARD_TYPE = {
701
+ 'bool' => 'Boolean',
702
+ 'string' => 'String',
703
+ 'int' => 'Integer',
704
+ 'untyped' => '',
705
+ 'NilClass' => 'nil'
706
+ }
707
+
708
+ # @param type [RBS::MethodType]
709
+ # @return [String]
710
+ def method_type_to_tag type
711
+ if type_aliases.key?(type.type.return_type.to_s)
712
+ other_type_to_tag(type_aliases[type.type.return_type.to_s].type)
713
+ else
714
+ other_type_to_tag type.type.return_type
715
+ end
716
+ end
717
+
718
+ # @param type_name [RBS::TypeName]
719
+ # @param type_args [Enumerable<RBS::Types::Bases::Base>]
720
+ # @return [ComplexType::UniqueType]
721
+ def build_type(type_name, type_args = [])
722
+ base = RBS_TO_YARD_TYPE[type_name.relative!.to_s] || type_name.relative!.to_s
723
+ params = type_args.map { |a| other_type_to_tag(a) }.map do |t|
724
+ ComplexType.try_parse(t).force_rooted
725
+ end
726
+ if base == 'Hash' && params.length == 2
727
+ ComplexType::UniqueType.new(base, [params.first], [params.last], rooted: true, parameters_type: :hash)
728
+ else
729
+ ComplexType::UniqueType.new(base, [], params.reject(&:undefined?), rooted: true, parameters_type: :list)
730
+ end
731
+ end
732
+
733
+ # @param type_name [RBS::TypeName]
734
+ # @param type_args [Enumerable<RBS::Types::Bases::Base>]
735
+ # @return [String]
736
+ def type_tag(type_name, type_args = [])
737
+ build_type(type_name, type_args).tags
738
+ end
739
+
740
+ # @param type [RBS::Types::Bases::Base]
741
+ # @return [String]
742
+ def other_type_to_tag type
743
+ if type.is_a?(RBS::Types::Optional)
744
+ "#{other_type_to_tag(type.type)}, nil"
745
+ elsif type.is_a?(RBS::Types::Bases::Any)
746
+ 'undefined'
747
+ elsif type.is_a?(RBS::Types::Bases::Bool)
748
+ 'Boolean'
749
+ elsif type.is_a?(RBS::Types::Tuple)
750
+ "Array(#{type.types.map { |t| other_type_to_tag(t) }.join(', ')})"
751
+ elsif type.is_a?(RBS::Types::Literal)
752
+ type.literal.inspect
753
+ elsif type.is_a?(RBS::Types::Union)
754
+ type.types.map { |t| other_type_to_tag(t) }.join(', ')
755
+ elsif type.is_a?(RBS::Types::Record)
756
+ # @todo Better record support
757
+ 'Hash'
758
+ elsif type.is_a?(RBS::Types::Bases::Nil)
759
+ 'nil'
760
+ elsif type.is_a?(RBS::Types::Bases::Self)
761
+ 'self'
762
+ elsif type.is_a?(RBS::Types::Bases::Void)
763
+ 'void'
764
+ elsif type.is_a?(RBS::Types::Variable)
765
+ "#{Solargraph::ComplexType::GENERIC_TAG_NAME}<#{type.name}>"
766
+ elsif type.is_a?(RBS::Types::ClassInstance) #&& !type.args.empty?
767
+ type_tag(type.name, type.args)
768
+ elsif type.is_a?(RBS::Types::Bases::Instance)
769
+ 'self'
770
+ elsif type.is_a?(RBS::Types::Bases::Top)
771
+ # top is the most super superclass
772
+ 'BasicObject'
773
+ elsif type.is_a?(RBS::Types::Bases::Bottom)
774
+ # bottom is used in contexts where nothing will ever return
775
+ # - e.g., it could be the return type of 'exit()' or 'raise'
776
+ #
777
+ # @todo define a specific bottom type and use it to
778
+ # determine dead code
779
+ 'undefined'
780
+ elsif type.is_a?(RBS::Types::Intersection)
781
+ type.types.map { |member| other_type_to_tag(member) }.join(', ')
782
+ elsif type.is_a?(RBS::Types::Proc)
783
+ 'Proc'
784
+ elsif type.is_a?(RBS::Types::Alias)
785
+ # type-level alias use - e.g., 'bool' in "type bool = true | false"
786
+ # @todo ensure these get resolved after processing all aliases
787
+ # @todo handle recursive aliases
788
+ type_tag(type.name, type.args)
789
+ elsif type.is_a?(RBS::Types::Interface)
790
+ # represents a mix-in module which can be considered a
791
+ # subtype of a consumer of it
792
+ type_tag(type.name, type.args)
793
+ elsif type.is_a?(RBS::Types::ClassSingleton)
794
+ # e.g., singleton(String)
795
+ type_tag(type.name)
796
+ else
797
+ Solargraph.logger.warn "Unrecognized RBS type: #{type.class} at #{type.location}"
798
+ 'undefined'
799
+ end
800
+ end
801
+
802
+ # @param decl [RBS::AST::Declarations::Class, RBS::AST::Declarations::Module]
803
+ # @param namespace [Pin::Namespace]
804
+ # @return [void]
805
+ def add_mixins decl, namespace
806
+ # @param mixin [RBS::AST::Members::Include, RBS::AST::Members::Members::Extend, RBS::AST::Members::Members::Prepend]
807
+ decl.each_mixin do |mixin|
808
+ # @todo are we handling prepend correctly?
809
+ klass = mixin.is_a?(RBS::AST::Members::Include) ? Pin::Reference::Include : Pin::Reference::Extend
810
+ type = build_type(mixin.name, mixin.args)
811
+ generic_values = type.all_params.map(&:to_s)
812
+ pins.push klass.new(
813
+ name: mixin.name.relative!.to_s,
814
+ type_location: location_decl_to_pin_location(mixin.location),
815
+ generic_values: generic_values,
816
+ closure: namespace,
817
+ source: :rbs
818
+ )
819
+ end
820
+ end
821
+ end
822
+ end
823
+ end