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