solargraph 0.54.4 → 0.57.0

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 (178) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/linting.yml +125 -0
  3. data/.github/workflows/plugins.yml +149 -5
  4. data/.github/workflows/rspec.yml +39 -4
  5. data/.github/workflows/typecheck.yml +8 -3
  6. data/.gitignore +7 -0
  7. data/.overcommit.yml +72 -0
  8. data/.rspec +1 -0
  9. data/.rubocop.yml +66 -0
  10. data/.rubocop_todo.yml +2627 -0
  11. data/.yardopts +1 -0
  12. data/CHANGELOG.md +104 -0
  13. data/README.md +20 -6
  14. data/Rakefile +125 -13
  15. data/lib/solargraph/api_map/cache.rb +3 -2
  16. data/lib/solargraph/api_map/constants.rb +218 -0
  17. data/lib/solargraph/api_map/index.rb +44 -42
  18. data/lib/solargraph/api_map/source_to_yard.rb +10 -4
  19. data/lib/solargraph/api_map/store.rb +165 -32
  20. data/lib/solargraph/api_map.rb +319 -243
  21. data/lib/solargraph/bench.rb +18 -1
  22. data/lib/solargraph/complex_type/type_methods.rb +7 -1
  23. data/lib/solargraph/complex_type/unique_type.rb +105 -16
  24. data/lib/solargraph/complex_type.rb +40 -7
  25. data/lib/solargraph/convention/active_support_concern.rb +111 -0
  26. data/lib/solargraph/convention/base.rb +20 -3
  27. data/lib/solargraph/convention/data_definition/data_assignment_node.rb +61 -0
  28. data/lib/solargraph/convention/data_definition/data_definition_node.rb +91 -0
  29. data/lib/solargraph/convention/data_definition.rb +105 -0
  30. data/lib/solargraph/convention/gemspec.rb +3 -2
  31. data/lib/solargraph/convention/struct_definition/struct_assignment_node.rb +61 -0
  32. data/lib/solargraph/convention/struct_definition/struct_definition_node.rb +102 -0
  33. data/lib/solargraph/convention/struct_definition.rb +164 -0
  34. data/lib/solargraph/convention.rb +35 -4
  35. data/lib/solargraph/diagnostics/rubocop.rb +6 -1
  36. data/lib/solargraph/diagnostics/rubocop_helpers.rb +1 -1
  37. data/lib/solargraph/doc_map.rb +313 -65
  38. data/lib/solargraph/environ.rb +9 -2
  39. data/lib/solargraph/gem_pins.rb +60 -38
  40. data/lib/solargraph/language_server/host/dispatch.rb +2 -0
  41. data/lib/solargraph/language_server/host/message_worker.rb +13 -7
  42. data/lib/solargraph/language_server/host.rb +14 -3
  43. data/lib/solargraph/language_server/message/base.rb +2 -1
  44. data/lib/solargraph/language_server/message/extended/check_gem_version.rb +2 -0
  45. data/lib/solargraph/language_server/message/extended/document.rb +5 -2
  46. data/lib/solargraph/language_server/message/extended/document_gems.rb +3 -3
  47. data/lib/solargraph/language_server/message/text_document/definition.rb +2 -0
  48. data/lib/solargraph/language_server/message/text_document/formatting.rb +16 -2
  49. data/lib/solargraph/language_server/message/text_document/type_definition.rb +1 -0
  50. data/lib/solargraph/language_server/message/workspace/did_change_workspace_folders.rb +2 -0
  51. data/lib/solargraph/language_server/progress.rb +8 -0
  52. data/lib/solargraph/language_server/request.rb +1 -0
  53. data/lib/solargraph/library.rb +53 -32
  54. data/lib/solargraph/location.rb +23 -0
  55. data/lib/solargraph/logging.rb +12 -2
  56. data/lib/solargraph/page.rb +4 -0
  57. data/lib/solargraph/parser/comment_ripper.rb +20 -7
  58. data/lib/solargraph/parser/flow_sensitive_typing.rb +255 -0
  59. data/lib/solargraph/parser/node_methods.rb +16 -2
  60. data/lib/solargraph/parser/node_processor/base.rb +10 -5
  61. data/lib/solargraph/parser/node_processor.rb +26 -9
  62. data/lib/solargraph/parser/parser_gem/class_methods.rb +17 -15
  63. data/lib/solargraph/parser/parser_gem/flawed_builder.rb +1 -0
  64. data/lib/solargraph/parser/parser_gem/node_chainer.rb +13 -11
  65. data/lib/solargraph/parser/parser_gem/node_methods.rb +8 -4
  66. data/lib/solargraph/parser/parser_gem/node_processors/alias_node.rb +2 -1
  67. data/lib/solargraph/parser/parser_gem/node_processors/and_node.rb +21 -0
  68. data/lib/solargraph/parser/parser_gem/node_processors/args_node.rb +4 -2
  69. data/lib/solargraph/parser/parser_gem/node_processors/block_node.rb +7 -4
  70. data/lib/solargraph/parser/parser_gem/node_processors/casgn_node.rb +2 -1
  71. data/lib/solargraph/parser/parser_gem/node_processors/cvasgn_node.rb +2 -1
  72. data/lib/solargraph/parser/parser_gem/node_processors/def_node.rb +6 -3
  73. data/lib/solargraph/parser/parser_gem/node_processors/defs_node.rb +2 -1
  74. data/lib/solargraph/parser/parser_gem/node_processors/gvasgn_node.rb +2 -1
  75. data/lib/solargraph/parser/parser_gem/node_processors/if_node.rb +23 -0
  76. data/lib/solargraph/parser/parser_gem/node_processors/ivasgn_node.rb +4 -2
  77. data/lib/solargraph/parser/parser_gem/node_processors/lvasgn_node.rb +2 -1
  78. data/lib/solargraph/parser/parser_gem/node_processors/masgn_node.rb +7 -1
  79. data/lib/solargraph/parser/parser_gem/node_processors/namespace_node.rb +8 -7
  80. data/lib/solargraph/parser/parser_gem/node_processors/opasgn_node.rb +42 -0
  81. data/lib/solargraph/parser/parser_gem/node_processors/orasgn_node.rb +1 -0
  82. data/lib/solargraph/parser/parser_gem/node_processors/resbody_node.rb +3 -1
  83. data/lib/solargraph/parser/parser_gem/node_processors/sclass_node.rb +4 -3
  84. data/lib/solargraph/parser/parser_gem/node_processors/send_node.rb +63 -30
  85. data/lib/solargraph/parser/parser_gem/node_processors/sym_node.rb +3 -1
  86. data/lib/solargraph/parser/parser_gem/node_processors/until_node.rb +29 -0
  87. data/lib/solargraph/parser/parser_gem/node_processors/while_node.rb +29 -0
  88. data/lib/solargraph/parser/parser_gem/node_processors.rb +14 -0
  89. data/lib/solargraph/parser/region.rb +4 -1
  90. data/lib/solargraph/parser/snippet.rb +2 -0
  91. data/lib/solargraph/parser.rb +1 -0
  92. data/lib/solargraph/pin/base.rb +360 -30
  93. data/lib/solargraph/pin/base_variable.rb +16 -10
  94. data/lib/solargraph/pin/block.rb +2 -0
  95. data/lib/solargraph/pin/breakable.rb +9 -0
  96. data/lib/solargraph/pin/callable.rb +83 -3
  97. data/lib/solargraph/pin/closure.rb +20 -1
  98. data/lib/solargraph/pin/common.rb +10 -1
  99. data/lib/solargraph/pin/constant.rb +2 -0
  100. data/lib/solargraph/pin/delegated_method.rb +21 -1
  101. data/lib/solargraph/pin/documenting.rb +16 -0
  102. data/lib/solargraph/pin/keyword.rb +7 -2
  103. data/lib/solargraph/pin/local_variable.rb +18 -6
  104. data/lib/solargraph/pin/method.rb +175 -46
  105. data/lib/solargraph/pin/method_alias.rb +3 -0
  106. data/lib/solargraph/pin/namespace.rb +17 -9
  107. data/lib/solargraph/pin/parameter.rb +78 -19
  108. data/lib/solargraph/pin/proxy_type.rb +13 -6
  109. data/lib/solargraph/pin/reference/override.rb +24 -6
  110. data/lib/solargraph/pin/reference/require.rb +2 -2
  111. data/lib/solargraph/pin/reference/superclass.rb +5 -0
  112. data/lib/solargraph/pin/reference.rb +26 -0
  113. data/lib/solargraph/pin/search.rb +3 -1
  114. data/lib/solargraph/pin/signature.rb +44 -0
  115. data/lib/solargraph/pin/singleton.rb +1 -1
  116. data/lib/solargraph/pin/symbol.rb +8 -2
  117. data/lib/solargraph/pin/until.rb +18 -0
  118. data/lib/solargraph/pin/while.rb +18 -0
  119. data/lib/solargraph/pin.rb +4 -1
  120. data/lib/solargraph/pin_cache.rb +245 -0
  121. data/lib/solargraph/position.rb +11 -0
  122. data/lib/solargraph/range.rb +10 -0
  123. data/lib/solargraph/rbs_map/conversions.rb +226 -70
  124. data/lib/solargraph/rbs_map/core_fills.rb +32 -16
  125. data/lib/solargraph/rbs_map/core_map.rb +37 -11
  126. data/lib/solargraph/rbs_map/stdlib_map.rb +15 -5
  127. data/lib/solargraph/rbs_map.rb +88 -18
  128. data/lib/solargraph/shell.rb +20 -18
  129. data/lib/solargraph/source/chain/array.rb +11 -7
  130. data/lib/solargraph/source/chain/block_symbol.rb +1 -1
  131. data/lib/solargraph/source/chain/block_variable.rb +1 -1
  132. data/lib/solargraph/source/chain/call.rb +53 -23
  133. data/lib/solargraph/source/chain/constant.rb +1 -1
  134. data/lib/solargraph/source/chain/hash.rb +4 -3
  135. data/lib/solargraph/source/chain/head.rb +1 -1
  136. data/lib/solargraph/source/chain/if.rb +1 -1
  137. data/lib/solargraph/source/chain/link.rb +12 -1
  138. data/lib/solargraph/source/chain/literal.rb +22 -2
  139. data/lib/solargraph/source/chain/or.rb +1 -1
  140. data/lib/solargraph/source/chain/z_super.rb +1 -1
  141. data/lib/solargraph/source/chain.rb +84 -47
  142. data/lib/solargraph/source/change.rb +2 -2
  143. data/lib/solargraph/source/cursor.rb +2 -3
  144. data/lib/solargraph/source/source_chainer.rb +3 -3
  145. data/lib/solargraph/source.rb +5 -2
  146. data/lib/solargraph/source_map/clip.rb +4 -2
  147. data/lib/solargraph/source_map/data.rb +4 -0
  148. data/lib/solargraph/source_map/mapper.rb +13 -7
  149. data/lib/solargraph/source_map.rb +21 -31
  150. data/lib/solargraph/type_checker/checks.rb +4 -0
  151. data/lib/solargraph/type_checker/param_def.rb +2 -0
  152. data/lib/solargraph/type_checker/rules.rb +8 -0
  153. data/lib/solargraph/type_checker.rb +208 -128
  154. data/lib/solargraph/version.rb +1 -1
  155. data/lib/solargraph/views/_method.erb +10 -10
  156. data/lib/solargraph/views/_namespace.erb +3 -3
  157. data/lib/solargraph/views/document.erb +10 -10
  158. data/lib/solargraph/workspace/config.rb +1 -3
  159. data/lib/solargraph/workspace/require_paths.rb +98 -0
  160. data/lib/solargraph/workspace.rb +38 -52
  161. data/lib/solargraph/yard_map/helpers.rb +29 -1
  162. data/lib/solargraph/yard_map/mapper/to_constant.rb +7 -5
  163. data/lib/solargraph/yard_map/mapper/to_method.rb +53 -18
  164. data/lib/solargraph/yard_map/mapper/to_namespace.rb +9 -7
  165. data/lib/solargraph/yard_map/mapper.rb +4 -3
  166. data/lib/solargraph/yard_map/to_method.rb +4 -2
  167. data/lib/solargraph/yardoc.rb +22 -10
  168. data/lib/solargraph.rb +34 -1
  169. data/rbs/fills/tuple.rbs +149 -0
  170. data/rbs_collection.yaml +19 -0
  171. data/sig/shims/parser/3.2.0.1/builders/default.rbs +195 -0
  172. data/sig/shims/thor/1.2.0.1/.rbs_meta.yaml +9 -0
  173. data/sig/shims/thor/1.2.0.1/manifest.yaml +7 -0
  174. data/sig/shims/thor/1.2.0.1/thor.rbs +17 -0
  175. data/solargraph.gemspec +15 -4
  176. metadata +157 -15
  177. data/lib/.rubocop.yml +0 -22
  178. data/lib/solargraph/cache.rb +0 -77
@@ -6,7 +6,9 @@ module Solargraph
6
6
  class RbsMap
7
7
  # Functions for converting RBS declarations to Solargraph pins
8
8
  #
9
- module Conversions
9
+ class Conversions
10
+ include Logging
11
+
10
12
  # A container for tracking the current context of the RBS conversion
11
13
  # process, e.g., what visibility is declared for methods in the current
12
14
  # scope
@@ -20,11 +22,19 @@ module Solargraph
20
22
  end
21
23
  end
22
24
 
23
- # @return [Array<Pin::Base>]
24
- def pins
25
- @pins ||= []
25
+ # @param loader [RBS::EnvironmentLoader]
26
+ def initialize(loader:)
27
+ @loader = loader
28
+ @pins = []
29
+ load_environment_to_pins(loader)
26
30
  end
27
31
 
32
+ # @return [RBS::EnvironmentLoader]
33
+ attr_reader :loader
34
+
35
+ # @return [Array<Pin::Base>]
36
+ attr_reader :pins
37
+
28
38
  private
29
39
 
30
40
  # @return [Hash{String => RBS::AST::Declarations::TypeAlias}]
@@ -37,9 +47,11 @@ module Solargraph
37
47
  def load_environment_to_pins(loader)
38
48
  environment = RBS::Environment.from_loader(loader).resolve_type_names
39
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
40
54
  environment.declarations.each { |decl| convert_decl_to_pin(decl, Solargraph::Pin::ROOT_PIN) }
41
- added_pins = pins[cursor..-1]
42
- added_pins.each { |pin| pin.source = :rbs }
43
55
  end
44
56
 
45
57
  # @param decl [RBS::AST::Declarations::Base]
@@ -86,7 +98,8 @@ module Solargraph
86
98
  name: decl.name.relative!.to_s,
87
99
  type_location: location_decl_to_pin_location(decl.location),
88
100
  generic_values: generic_values,
89
- closure: closure
101
+ closure: closure,
102
+ source: :rbs
90
103
  )
91
104
  pins.push include_pin
92
105
  end
@@ -95,24 +108,24 @@ module Solargraph
95
108
  # @param closure [Pin::Namespace]
96
109
  # @return [void]
97
110
  def convert_members_to_pins decl, closure
98
- context = Context.new
111
+ context = Conversions::Context.new
99
112
  decl.members.each { |m| context = convert_member_to_pin(m, closure, context) }
100
113
  end
101
114
 
102
115
  # @param member [RBS::AST::Members::Base,RBS::AST::Declarations::Base]
103
116
  # @param closure [Pin::Namespace]
104
117
  # @param context [Context]
105
- # @return [void]
118
+ # @return [Context]
106
119
  def convert_member_to_pin member, closure, context
107
120
  case member
108
121
  when RBS::AST::Members::MethodDefinition
109
- method_def_to_pin(member, closure)
122
+ method_def_to_pin(member, closure, context)
110
123
  when RBS::AST::Members::AttrReader
111
- attr_reader_to_pin(member, closure)
124
+ attr_reader_to_pin(member, closure, context)
112
125
  when RBS::AST::Members::AttrWriter
113
- attr_writer_to_pin(member, closure)
126
+ attr_writer_to_pin(member, closure, context)
114
127
  when RBS::AST::Members::AttrAccessor
115
- attr_accessor_to_pin(member, closure)
128
+ attr_accessor_to_pin(member, closure, context)
116
129
  when RBS::AST::Members::Include
117
130
  include_to_pin(member, closure)
118
131
  when RBS::AST::Members::Prepend
@@ -128,9 +141,9 @@ module Solargraph
128
141
  when RBS::AST::Members::InstanceVariable
129
142
  ivar_to_pin(member, closure)
130
143
  when RBS::AST::Members::Public
131
- return Context.new(visibility: :public)
144
+ return Context.new(:public)
132
145
  when RBS::AST::Members::Private
133
- return Context.new(visibility: :private)
146
+ return Context.new(:private)
134
147
  when RBS::AST::Declarations::Base
135
148
  convert_decl_to_pin(member, closure)
136
149
  else
@@ -142,23 +155,39 @@ module Solargraph
142
155
  # @param decl [RBS::AST::Declarations::Class]
143
156
  # @return [void]
144
157
  def class_decl_to_pin decl
158
+ generics = decl.type_params.map(&:name).map(&:to_s)
159
+ generic_defaults = {}
160
+ decl.type_params.each do |param|
161
+ if param.default_type
162
+ tag = other_type_to_tag param.default_type
163
+ generic_defaults[param.name.to_s] = ComplexType.parse(tag).force_rooted
164
+ end
165
+ end
166
+ class_name = decl.name.relative!.to_s
145
167
  class_pin = Solargraph::Pin::Namespace.new(
146
168
  type: :class,
147
- name: decl.name.relative!.to_s,
169
+ name: class_name,
148
170
  closure: Solargraph::Pin::ROOT_PIN,
149
171
  comments: decl.comment&.string,
150
172
  type_location: location_decl_to_pin_location(decl.location),
151
173
  # @todo some type parameters in core/stdlib have default
152
174
  # values; Solargraph doesn't support that yet as so these
153
175
  # get treated as undefined if not specified
154
- generics: decl.type_params.map(&:name).map(&:to_s)
176
+ generics: generics,
177
+ generic_defaults: generic_defaults,
178
+ source: :rbs
155
179
  )
156
180
  pins.push class_pin
157
181
  if decl.super_class
182
+ type = build_type(decl.super_class.name, decl.super_class.args)
183
+ generic_values = type.all_params.map(&:to_s)
184
+ superclass_name = decl.super_class.name.to_s
158
185
  pins.push Solargraph::Pin::Reference::Superclass.new(
159
186
  type_location: location_decl_to_pin_location(decl.super_class.location),
160
187
  closure: class_pin,
161
- name: decl.super_class.name.relative!.to_s
188
+ generic_values: generic_values,
189
+ name: superclass_name,
190
+ source: :rbs
162
191
  )
163
192
  end
164
193
  add_mixins decl, class_pin
@@ -178,7 +207,8 @@ module Solargraph
178
207
  generics: decl.type_params.map(&:name).map(&:to_s),
179
208
  # HACK: Using :hidden to keep interfaces from appearing in
180
209
  # autocompletion
181
- visibility: :hidden
210
+ visibility: :hidden,
211
+ source: :rbs
182
212
  )
183
213
  class_pin.docstring.add_tag(YARD::Tags::Tag.new(:abstract, '(RBS interface)'))
184
214
  pins.push class_pin
@@ -195,6 +225,7 @@ module Solargraph
195
225
  closure: Solargraph::Pin::ROOT_PIN,
196
226
  comments: decl.comment&.string,
197
227
  generics: decl.type_params.map(&:name).map(&:to_s),
228
+ source: :rbs
198
229
  )
199
230
  pins.push module_pin
200
231
  convert_self_types_to_pins decl, module_pin
@@ -223,7 +254,8 @@ module Solargraph
223
254
  name: name,
224
255
  closure: closure,
225
256
  type_location: location_decl_to_pin_location(decl.location),
226
- comments: comments
257
+ comments: comments,
258
+ source: :rbs
227
259
  )
228
260
  tag = "#{base}<#{tag}>" if base
229
261
  rooted_tag = ComplexType.parse(tag).force_rooted.rooted_tags
@@ -267,32 +299,101 @@ module Solargraph
267
299
  name: name,
268
300
  closure: closure,
269
301
  comments: decl.comment&.string,
302
+ type_location: location_decl_to_pin_location(decl.location),
303
+ source: :rbs
270
304
  )
271
305
  rooted_tag = ComplexType.parse(other_type_to_tag(decl.type)).force_rooted.rooted_tags
272
306
  pin.docstring.add_tag(YARD::Tags::Tag.new(:type, '', rooted_tag))
273
307
  pins.push pin
274
308
  end
275
309
 
310
+
311
+ # Visibility overrides that will allow the Solargraph project
312
+ # and plugins to pass typechecking using SOLARGRAPH_ASSERTS=on,
313
+ # so that we can detect any regressions/issues elsewhere in the
314
+ # visibility logic.
315
+ #
316
+ # These should either reflect a bug upstream in the RBS
317
+ # definitions, or include a @todo indicating what needs to be
318
+ # fixed in Solargraph to properly understand it.
319
+ #
320
+ # @todo PR these fixes upstream and list open PRs here above
321
+ # related overrides
322
+ # @todo externalize remaining overrides into yaml file, then
323
+ # allow that to be extended via .solargraph.yml
324
+ # @type [Hash{Array(String, Symbol, String) => Symbol}
325
+ VISIBILITY_OVERRIDE = {
326
+ ["Rails::Engine", :instance, "run_tasks_blocks"] => :protected,
327
+ # Should have been marked as both instance and class method in module -e.g., 'module_function'
328
+ ["Kernel", :instance, "pretty_inspect"] => :private,
329
+ # marked incorrectly in RBS
330
+ ["WEBrick::HTTPUtils::FormData", :instance, "next_data"] => :protected,
331
+ ["Rails::Command", :class, "command_type"] => :private,
332
+ ["Rails::Command", :class, "lookup_paths"] => :private,
333
+ ["Rails::Command", :class, "file_lookup_paths"] => :private,
334
+ ["Rails::Railtie", :instance, "run_console_blocks"] => :protected,
335
+ ["Rails::Railtie", :instance, "run_generators_blocks"] => :protected,
336
+ ["Rails::Railtie", :instance, "run_runner_blocks"] => :protected,
337
+ ["Rails::Railtie", :instance, "run_tasks_blocks"] => :protected,
338
+ ["ActionController::Base", :instance, "_protected_ivars"] => :private,
339
+ ["ActionView::Template", :instance, "method_name"] => :public,
340
+ ["Module", :instance, "ruby2_keywords"] => :private,
341
+ ["Nokogiri::XML::Node", :instance, "coerce"] => :protected,
342
+ ["Nokogiri::XML::Document", :class, "empty_doc?"] => :private,
343
+ ["Nokogiri::Decorators::Slop", :instance, "respond_to_missing?"] => :public,
344
+ ["RuboCop::Cop::RangeHelp", :instance, "source_range"] => :private,
345
+ ["AST::Node", :instance, "original_dup"] => :private,
346
+ ["Rainbow::Presenter", :instance, "wrap_with_sgr"] => :private,
347
+ }
348
+
349
+ # @param decl [RBS::AST::Members::MethodDefinition, RBS::AST::Members::AttrReader, RBS::AST::Members::AttrAccessor]
350
+ # @param closure [Pin::Closure]
351
+ # @param context [Context]
352
+ # @param scope [Symbol] :instance or :class
353
+ # @param name [String] The name of the method
354
+ # @sg-ignore
355
+ # @return [Symbol]
356
+ def calculate_method_visibility(decl, context, closure, scope, name)
357
+ override_key = [closure.path, scope, name]
358
+ visibility = VISIBILITY_OVERRIDE[override_key]
359
+ simple_override_key = [closure.path, scope]
360
+ visibility ||= VISIBILITY_OVERRIDE[simple_override_key]
361
+ visibility ||= :private if closure.path == 'Kernel' && Kernel.private_instance_methods(false).include?(decl.name)
362
+ if decl.kind == :singleton_instance
363
+ # this is a 'module function'
364
+ visibility ||= :private
365
+ end
366
+ visibility ||= decl.visibility
367
+ visibility ||= context.visibility
368
+ visibility ||= :public
369
+ visibility
370
+ end
371
+
276
372
  # @param decl [RBS::AST::Members::MethodDefinition]
277
373
  # @param closure [Pin::Closure]
374
+ # @param context [Context]
278
375
  # @return [void]
279
- def method_def_to_pin decl, closure
376
+ def method_def_to_pin decl, closure, context
280
377
  # there may be edge cases here around different signatures
281
378
  # having different type params / orders - we may need to match
282
379
  # this data model and have generics live in signatures to
283
380
  # handle those correctly
284
381
  generics = decl.overloads.map(&:method_type).flat_map(&:type_params).map(&:name).map(&:to_s).uniq
382
+
285
383
  if decl.instance?
384
+ name = decl.name.to_s
385
+ final_scope = :instance
386
+ visibility = calculate_method_visibility(decl, context, closure, final_scope, name)
286
387
  pin = Solargraph::Pin::Method.new(
287
- name: decl.name.to_s,
388
+ name: name,
288
389
  closure: closure,
289
390
  type_location: location_decl_to_pin_location(decl.location),
290
391
  comments: decl.comment&.string,
291
- scope: :instance,
392
+ scope: final_scope,
292
393
  signatures: [],
293
394
  generics: generics,
294
- # @todo RBS core has unreliable visibility definitions
295
- visibility: closure.path == 'Kernel' && Kernel.private_instance_methods(false).include?(decl.name) ? :private : :public
395
+ visibility: visibility,
396
+ source: :rbs
296
397
  )
297
398
  pin.signatures.concat method_def_to_sigs(decl, pin)
298
399
  pins.push pin
@@ -302,14 +403,19 @@ module Solargraph
302
403
  end
303
404
  end
304
405
  if decl.singleton?
406
+ final_scope = :class
407
+ name = decl.name.to_s
408
+ visibility = calculate_method_visibility(decl, context, closure, final_scope, name)
305
409
  pin = Solargraph::Pin::Method.new(
306
- name: decl.name.to_s,
410
+ name: name,
307
411
  closure: closure,
308
412
  comments: decl.comment&.string,
309
413
  type_location: location_decl_to_pin_location(decl.location),
310
- scope: :class,
414
+ visibility: visibility,
415
+ scope: final_scope,
311
416
  signatures: [],
312
- generics: generics
417
+ generics: generics,
418
+ source: :rbs
313
419
  )
314
420
  pin.signatures.concat method_def_to_sigs(decl, pin)
315
421
  pins.push pin
@@ -321,13 +427,16 @@ module Solargraph
321
427
  # @return [void]
322
428
  def method_def_to_sigs decl, pin
323
429
  decl.overloads.map do |overload|
430
+ type_location = location_decl_to_pin_location(overload.method_type.location)
324
431
  generics = overload.method_type.type_params.map(&:name).map(&:to_s)
325
432
  signature_parameters, signature_return_type = parts_of_function(overload.method_type, pin)
326
433
  block = if overload.method_type.block
327
434
  block_parameters, block_return_type = parts_of_function(overload.method_type.block, pin)
328
- Pin::Signature.new(generics: generics, parameters: block_parameters, return_type: block_return_type)
435
+ Pin::Signature.new(generics: generics, parameters: block_parameters, return_type: block_return_type, source: :rbs,
436
+ type_location: type_location, closure: pin)
329
437
  end
330
- Pin::Signature.new(generics: generics, parameters: signature_parameters, return_type: signature_return_type, block: block)
438
+ Pin::Signature.new(generics: generics, parameters: signature_parameters, return_type: signature_return_type, block: block, source: :rbs,
439
+ type_location: type_location, closure: pin)
331
440
  end
332
441
  end
333
442
 
@@ -346,40 +455,52 @@ module Solargraph
346
455
  # @param pin [Pin::Method]
347
456
  # @return [Array(Array<Pin::Parameter>, ComplexType)]
348
457
  def parts_of_function type, pin
349
- return [[Solargraph::Pin::Parameter.new(decl: :restarg, name: 'arg', closure: pin)], ComplexType.try_parse(method_type_to_tag(type)).force_rooted] if defined?(RBS::Types::UntypedFunction) && type.type.is_a?(RBS::Types::UntypedFunction)
458
+ type_location = pin.type_location
459
+ if defined?(RBS::Types::UntypedFunction) && type.type.is_a?(RBS::Types::UntypedFunction)
460
+ return [
461
+ [Solargraph::Pin::Parameter.new(decl: :restarg, name: 'arg', closure: pin, source: :rbs, type_location: type_location)],
462
+ ComplexType.try_parse(method_type_to_tag(type)).force_rooted
463
+ ]
464
+ end
350
465
 
351
466
  parameters = []
352
467
  arg_num = -1
353
468
  type.type.required_positionals.each do |param|
354
- name = param.name ? param.name.to_s : "arg#{arg_num += 1}"
355
- 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)
469
+ name = param.name ? param.name.to_s : "arg_#{arg_num += 1}"
470
+ 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)
356
471
  end
357
472
  type.type.optional_positionals.each do |param|
358
- name = param.name ? param.name.to_s : "arg#{arg_num += 1}"
473
+ name = param.name ? param.name.to_s : "arg_#{arg_num += 1}"
359
474
  parameters.push Solargraph::Pin::Parameter.new(decl: :optarg, name: name, closure: pin,
360
- return_type: ComplexType.try_parse(other_type_to_tag(param.type)).force_rooted)
475
+ return_type: ComplexType.try_parse(other_type_to_tag(param.type)).force_rooted,
476
+ type_location: type_location,
477
+ source: :rbs)
361
478
  end
362
479
  if type.type.rest_positionals
363
- name = type.type.rest_positionals.name ? type.type.rest_positionals.name.to_s : "arg#{arg_num += 1}"
364
- parameters.push Solargraph::Pin::Parameter.new(decl: :restarg, name: name, closure: pin)
480
+ name = type.type.rest_positionals.name ? type.type.rest_positionals.name.to_s : "arg_#{arg_num += 1}"
481
+ parameters.push Solargraph::Pin::Parameter.new(decl: :restarg, name: name, closure: pin, source: :rbs, type_location: type_location)
365
482
  end
366
483
  type.type.trailing_positionals.each do |param|
367
- name = param.name ? param.name.to_s : "arg#{arg_num += 1}"
368
- parameters.push Solargraph::Pin::Parameter.new(decl: :arg, name: name, closure: pin)
484
+ name = param.name ? param.name.to_s : "arg_#{arg_num += 1}"
485
+ parameters.push Solargraph::Pin::Parameter.new(decl: :arg, name: name, closure: pin, source: :rbs, type_location: type_location)
369
486
  end
370
487
  type.type.required_keywords.each do |orig, param|
371
- name = orig ? orig.to_s : "arg#{arg_num += 1}"
488
+ name = orig ? orig.to_s : "arg_#{arg_num += 1}"
372
489
  parameters.push Solargraph::Pin::Parameter.new(decl: :kwarg, name: name, closure: pin,
373
- return_type: ComplexType.try_parse(other_type_to_tag(param.type)).force_rooted)
490
+ return_type: ComplexType.try_parse(other_type_to_tag(param.type)).force_rooted,
491
+ source: :rbs, type_location: type_location)
374
492
  end
375
493
  type.type.optional_keywords.each do |orig, param|
376
- name = orig ? orig.to_s : "arg#{arg_num += 1}"
494
+ name = orig ? orig.to_s : "arg_#{arg_num += 1}"
377
495
  parameters.push Solargraph::Pin::Parameter.new(decl: :kwoptarg, name: name, closure: pin,
378
- return_type: ComplexType.try_parse(other_type_to_tag(param.type)).force_rooted)
496
+ return_type: ComplexType.try_parse(other_type_to_tag(param.type)).force_rooted,
497
+ type_location: type_location,
498
+ source: :rbs)
379
499
  end
380
500
  if type.type.rest_keywords
381
- name = type.type.rest_keywords.name ? type.type.rest_keywords.name.to_s : "arg#{arg_num += 1}"
382
- parameters.push Solargraph::Pin::Parameter.new(decl: :kwrestarg, name: type.type.rest_keywords.name.to_s, closure: pin)
501
+ name = type.type.rest_keywords.name ? type.type.rest_keywords.name.to_s : "arg_#{arg_num += 1}"
502
+ parameters.push Solargraph::Pin::Parameter.new(decl: :kwrestarg, name: type.type.rest_keywords.name.to_s, closure: pin,
503
+ source: :rbs, type_location: type_location)
383
504
  end
384
505
 
385
506
  rooted_tag = method_type_to_tag(type)
@@ -389,33 +510,56 @@ module Solargraph
389
510
 
390
511
  # @param decl [RBS::AST::Members::AttrReader,RBS::AST::Members::AttrAccessor]
391
512
  # @param closure [Pin::Namespace]
513
+ # @param context [Context]
392
514
  # @return [void]
393
- def attr_reader_to_pin(decl, closure)
515
+ def attr_reader_to_pin(decl, closure, context)
516
+ name = decl.name.to_s
517
+ final_scope = decl.kind == :instance ? :instance : :class
518
+ visibility = calculate_method_visibility(decl, context, closure, final_scope, name)
394
519
  pin = Solargraph::Pin::Method.new(
395
- name: decl.name.to_s,
520
+ name: name,
396
521
  type_location: location_decl_to_pin_location(decl.location),
397
522
  closure: closure,
398
523
  comments: decl.comment&.string,
399
- scope: :instance,
400
- attribute: true
524
+ scope: final_scope,
525
+ attribute: true,
526
+ visibility: visibility,
527
+ source: :rbs
401
528
  )
402
529
  rooted_tag = ComplexType.parse(other_type_to_tag(decl.type)).force_rooted.rooted_tags
403
530
  pin.docstring.add_tag(YARD::Tags::Tag.new(:return, '', rooted_tag))
531
+ logger.debug { "Conversions#attr_reader_to_pin(name=#{name.inspect}, visibility=#{visibility.inspect}) => #{pin.inspect}" }
404
532
  pins.push pin
405
533
  end
406
534
 
407
535
  # @param decl [RBS::AST::Members::AttrWriter, RBS::AST::Members::AttrAccessor]
408
536
  # @param closure [Pin::Namespace]
537
+ # @param context [Context]
409
538
  # @return [void]
410
- def attr_writer_to_pin(decl, closure)
539
+ def attr_writer_to_pin(decl, closure, context)
540
+ final_scope = decl.kind == :instance ? :instance : :class
541
+ name = "#{decl.name.to_s}="
542
+ visibility = calculate_method_visibility(decl, context, closure, final_scope, name)
543
+ type_location = location_decl_to_pin_location(decl.location)
411
544
  pin = Solargraph::Pin::Method.new(
412
- name: "#{decl.name.to_s}=",
413
- type_location: location_decl_to_pin_location(decl.location),
545
+ name: name,
546
+ type_location: type_location,
414
547
  closure: closure,
548
+ parameters: [],
415
549
  comments: decl.comment&.string,
416
- scope: :instance,
417
- attribute: true
550
+ scope: final_scope,
551
+ attribute: true,
552
+ visibility: visibility,
553
+ source: :rbs
418
554
  )
555
+ pin.parameters <<
556
+ Solargraph::Pin::Parameter.new(
557
+ name: 'value',
558
+ return_type: ComplexType.try_parse(other_type_to_tag(decl.type)).force_rooted,
559
+ source: :rbs,
560
+ closure: pin,
561
+ type_location: type_location
562
+ )
419
563
  rooted_tag = ComplexType.parse(other_type_to_tag(decl.type)).force_rooted.rooted_tags
420
564
  pin.docstring.add_tag(YARD::Tags::Tag.new(:return, '', rooted_tag))
421
565
  pins.push pin
@@ -423,10 +567,11 @@ module Solargraph
423
567
 
424
568
  # @param decl [RBS::AST::Members::AttrAccessor]
425
569
  # @param closure [Pin::Namespace]
570
+ # @param context [Context]
426
571
  # @return [void]
427
- def attr_accessor_to_pin(decl, closure)
428
- attr_reader_to_pin(decl, closure)
429
- attr_writer_to_pin(decl, closure)
572
+ def attr_accessor_to_pin(decl, closure, context)
573
+ attr_reader_to_pin(decl, closure, context)
574
+ attr_writer_to_pin(decl, closure, context)
430
575
  end
431
576
 
432
577
  # @param decl [RBS::AST::Members::InstanceVariable]
@@ -437,7 +582,8 @@ module Solargraph
437
582
  name: decl.name.to_s,
438
583
  closure: closure,
439
584
  type_location: location_decl_to_pin_location(decl.location),
440
- comments: decl.comment&.string
585
+ comments: decl.comment&.string,
586
+ source: :rbs
441
587
  )
442
588
  rooted_tag = ComplexType.parse(other_type_to_tag(decl.type)).force_rooted.rooted_tags
443
589
  pin.docstring.add_tag(YARD::Tags::Tag.new(:type, '', rooted_tag))
@@ -452,7 +598,9 @@ module Solargraph
452
598
  pin = Solargraph::Pin::ClassVariable.new(
453
599
  name: name,
454
600
  closure: closure,
455
- comments: decl.comment&.string
601
+ comments: decl.comment&.string,
602
+ type_location: location_decl_to_pin_location(decl.location),
603
+ source: :rbs
456
604
  )
457
605
  rooted_tag = ComplexType.parse(other_type_to_tag(decl.type)).force_rooted.rooted_tags
458
606
  pin.docstring.add_tag(YARD::Tags::Tag.new(:type, '', rooted_tag))
@@ -467,7 +615,9 @@ module Solargraph
467
615
  pin = Solargraph::Pin::InstanceVariable.new(
468
616
  name: name,
469
617
  closure: closure,
470
- comments: decl.comment&.string
618
+ comments: decl.comment&.string,
619
+ type_location: location_decl_to_pin_location(decl.location),
620
+ source: :rbs
471
621
  )
472
622
  rooted_tag = ComplexType.parse(other_type_to_tag(decl.type)).force_rooted.rooted_tags
473
623
  pin.docstring.add_tag(YARD::Tags::Tag.new(:type, '', rooted_tag))
@@ -484,7 +634,8 @@ module Solargraph
484
634
  name: decl.name.relative!.to_s,
485
635
  type_location: location_decl_to_pin_location(decl.location),
486
636
  generic_values: generic_values,
487
- closure: closure
637
+ closure: closure,
638
+ source: :rbs
488
639
  )
489
640
  end
490
641
 
@@ -495,7 +646,8 @@ module Solargraph
495
646
  pins.push Solargraph::Pin::Reference::Prepend.new(
496
647
  name: decl.name.relative!.to_s,
497
648
  type_location: location_decl_to_pin_location(decl.location),
498
- closure: closure
649
+ closure: closure,
650
+ source: :rbs
499
651
  )
500
652
  end
501
653
 
@@ -506,7 +658,8 @@ module Solargraph
506
658
  pins.push Solargraph::Pin::Reference::Extend.new(
507
659
  name: decl.name.relative!.to_s,
508
660
  type_location: location_decl_to_pin_location(decl.location),
509
- closure: closure
661
+ closure: closure,
662
+ source: :rbs
510
663
  )
511
664
  end
512
665
 
@@ -514,11 +667,14 @@ module Solargraph
514
667
  # @param closure [Pin::Namespace]
515
668
  # @return [void]
516
669
  def alias_to_pin decl, closure
670
+ final_scope = decl.singleton? ? :class : :instance
517
671
  pins.push Solargraph::Pin::MethodAlias.new(
518
672
  name: decl.new_name.to_s,
519
673
  type_location: location_decl_to_pin_location(decl.location),
520
674
  original: decl.old_name.to_s,
521
- closure: closure
675
+ closure: closure,
676
+ scope: final_scope,
677
+ source: :rbs,
522
678
  )
523
679
  end
524
680
 
@@ -545,13 +701,13 @@ module Solargraph
545
701
  # @return [ComplexType::UniqueType]
546
702
  def build_type(type_name, type_args = [])
547
703
  base = RBS_TO_YARD_TYPE[type_name.relative!.to_s] || type_name.relative!.to_s
548
- params = type_args.map { |a| other_type_to_tag(a) }.reject { |t| t == 'undefined' }.map do |t|
704
+ params = type_args.map { |a| other_type_to_tag(a) }.map do |t|
549
705
  ComplexType.try_parse(t).force_rooted
550
706
  end
551
707
  if base == 'Hash' && params.length == 2
552
708
  ComplexType::UniqueType.new(base, [params.first], [params.last], rooted: true, parameters_type: :hash)
553
709
  else
554
- ComplexType::UniqueType.new(base, [], params, rooted: true, parameters_type: :list)
710
+ ComplexType::UniqueType.new(base, [], params.reject(&:undefined?), rooted: true, parameters_type: :list)
555
711
  end
556
712
  end
557
713
 
@@ -568,14 +724,13 @@ module Solargraph
568
724
  if type.is_a?(RBS::Types::Optional)
569
725
  "#{other_type_to_tag(type.type)}, nil"
570
726
  elsif type.is_a?(RBS::Types::Bases::Any)
571
- # @todo Not sure what to do with Any yet
572
- 'BasicObject'
727
+ 'undefined'
573
728
  elsif type.is_a?(RBS::Types::Bases::Bool)
574
729
  'Boolean'
575
730
  elsif type.is_a?(RBS::Types::Tuple)
576
731
  "Array(#{type.types.map { |t| other_type_to_tag(t) }.join(', ')})"
577
732
  elsif type.is_a?(RBS::Types::Literal)
578
- type.literal.to_s
733
+ type.literal.inspect
579
734
  elsif type.is_a?(RBS::Types::Union)
580
735
  type.types.map { |t| other_type_to_tag(t) }.join(', ')
581
736
  elsif type.is_a?(RBS::Types::Record)
@@ -637,7 +792,8 @@ module Solargraph
637
792
  name: mixin.name.relative!.to_s,
638
793
  type_location: location_decl_to_pin_location(mixin.location),
639
794
  generic_values: generic_values,
640
- closure: namespace
795
+ closure: namespace,
796
+ source: :rbs
641
797
  )
642
798
  end
643
799
  end
@@ -13,38 +13,54 @@ module Solargraph
13
13
  'elsif', 'end', 'ensure', 'false', 'for', 'if', 'in', 'module', 'next',
14
14
  'nil', 'not', 'or', 'redo', 'rescue', 'retry', 'return', 'self', 'super',
15
15
  'then', 'true', 'undef', 'unless', 'until', 'when', 'while', 'yield'
16
- ].map { |k| Pin::Keyword.new(k) }
16
+ ].map { |k| Pin::Keyword.new(k, source: :core_fill) }
17
17
 
18
18
  MISSING = [
19
- Solargraph::Pin::Method.new(name: 'tap', scope: :instance,
20
- closure: Solargraph::Pin::Namespace.new(name: 'Object')),
21
19
  Solargraph::Pin::Method.new(name: 'class', scope: :instance,
22
- closure: Solargraph::Pin::Namespace.new(name: 'Object'), comments: '@return [::Class<self>]')
20
+ closure: Solargraph::Pin::Namespace.new(name: 'Object', source: :core_fill), comments: '@return [::Class<self>]',
21
+ source: :core_fill)
23
22
  ]
24
23
 
25
24
  OVERRIDES = [
26
- Override.from_comment('BasicObject#instance_eval', '@yieldreceiver [self]'),
27
- Override.from_comment('BasicObject#instance_exec', '@yieldreceiver [self]'),
28
- Override.from_comment('Module#define_method', '@yieldreceiver [::Object<self>]'),
29
- Override.from_comment('Module#class_eval', '@yieldreceiver [::Class<self>]'),
30
- Override.from_comment('Module#class_exec', '@yieldreceiver [::Class<self>]'),
31
- Override.from_comment('Module#module_eval', '@yieldreceiver [::Module<self>]'),
32
- Override.from_comment('Module#module_exec', '@yieldreceiver [::Module<self>]'),
25
+ Override.from_comment('BasicObject#instance_eval', '@yieldreceiver [self]',
26
+ source: :core_fill),
27
+ Override.from_comment('BasicObject#instance_exec', '@yieldreceiver [self]',
28
+ source: :core_fill),
29
+ Override.from_comment('Module#define_method', '@yieldreceiver [::Object<self>]',
30
+ source: :core_fill),
31
+ Override.from_comment('Module#class_eval', '@yieldreceiver [::Class<self>]',
32
+ source: :core_fill),
33
+ Override.from_comment('Module#class_exec', '@yieldreceiver [::Class<self>]',
34
+ source: :core_fill),
35
+ Override.from_comment('Module#module_eval', '@yieldreceiver [::Module<self>]',
36
+ source: :core_fill),
37
+ Override.from_comment('Module#module_exec', '@yieldreceiver [::Module<self>]',
38
+ source: :core_fill),
33
39
  # RBS does not define Class with a generic, so all calls to
34
40
  # generic() return an 'untyped'. We can do better:
35
- Override.method_return('Class#allocate', 'self')
41
+ Override.method_return('Class#allocate', 'self', source: :core_fill),
42
+ ]
43
+
44
+ # @todo I don't see any direct link in RBS to build this from -
45
+ # presumably RBS is using duck typing to match interfaces
46
+ # against concrete classes
47
+ INCLUDES = [
48
+ Solargraph::Pin::Reference::Include.new(name: '_ToAry',
49
+ closure: Solargraph::Pin::Namespace.new(name: 'Array', source: :core_fill),
50
+ generic_values: ['generic<Elem>'],
51
+ source: :core_fill)
36
52
  ]
37
53
 
38
54
  # HACK: Add Errno exception classes
39
- errno = Solargraph::Pin::Namespace.new(name: 'Errno')
55
+ errno = Solargraph::Pin::Namespace.new(name: 'Errno', source: :core_fill)
40
56
  errnos = []
41
57
  Errno.constants.each do |const|
42
- errnos.push Solargraph::Pin::Namespace.new(type: :class, name: const.to_s, closure: errno)
43
- errnos.push Solargraph::Pin::Reference::Superclass.new(closure: errnos.last, name: 'SystemCallError')
58
+ errnos.push Solargraph::Pin::Namespace.new(type: :class, name: const.to_s, closure: errno, source: :core_fill)
59
+ errnos.push Solargraph::Pin::Reference::Superclass.new(closure: errnos.last, name: 'SystemCallError', source: :core_fill)
44
60
  end
45
61
  ERRNOS = errnos
46
62
 
47
- ALL = KEYWORDS + MISSING + OVERRIDES + ERRNOS
63
+ ALL = KEYWORDS + MISSING + OVERRIDES + ERRNOS + INCLUDES
48
64
  end
49
65
  end
50
66
  end