solargraph 0.59.0.dev.1 → 0.59.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.
- checksums.yaml +4 -4
- data/.github/workflows/linting.yml +3 -1
- data/.github/workflows/plugins.yml +12 -3
- data/.github/workflows/rspec.yml +9 -54
- data/.github/workflows/typecheck.yml +2 -1
- data/.gitignore +1 -0
- data/.rubocop.yml +38 -6
- data/.rubocop_todo.yml +40 -931
- data/CHANGELOG.md +22 -1
- data/Gemfile +3 -1
- data/Rakefile +25 -23
- data/bin/solargraph +2 -1
- data/lib/solargraph/api_map/constants.rb +0 -1
- data/lib/solargraph/api_map/index.rb +11 -11
- data/lib/solargraph/api_map/source_to_yard.rb +9 -8
- data/lib/solargraph/api_map/store.rb +28 -20
- data/lib/solargraph/api_map.rb +70 -41
- data/lib/solargraph/bench.rb +44 -45
- data/lib/solargraph/complex_type/type_methods.rb +14 -16
- data/lib/solargraph/complex_type/unique_type.rb +56 -47
- data/lib/solargraph/complex_type.rb +70 -62
- data/lib/solargraph/convention/data_definition/data_assignment_node.rb +61 -61
- data/lib/solargraph/convention/data_definition/data_definition_node.rb +4 -4
- data/lib/solargraph/convention/data_definition.rb +1 -1
- data/lib/solargraph/convention/gemfile.rb +15 -15
- data/lib/solargraph/convention/gemspec.rb +23 -23
- data/lib/solargraph/convention/rakefile.rb +17 -17
- data/lib/solargraph/convention/struct_definition/struct_assignment_node.rb +1 -1
- data/lib/solargraph/convention/struct_definition/struct_definition_node.rb +3 -3
- data/lib/solargraph/convention/struct_definition.rb +3 -3
- data/lib/solargraph/convention.rb +78 -78
- data/lib/solargraph/converters/dd.rb +19 -17
- data/lib/solargraph/converters/dl.rb +17 -15
- data/lib/solargraph/converters/dt.rb +17 -15
- data/lib/solargraph/converters/misc.rb +3 -1
- data/lib/solargraph/diagnostics/rubocop.rb +10 -10
- data/lib/solargraph/diagnostics/rubocop_helpers.rb +3 -3
- data/lib/solargraph/diagnostics/type_check.rb +10 -10
- data/lib/solargraph/diagnostics/update_errors.rb +37 -41
- data/lib/solargraph/doc_map.rb +370 -132
- data/lib/solargraph/equality.rb +3 -3
- data/lib/solargraph/gem_pins.rb +19 -18
- data/lib/solargraph/language_server/error_codes.rb +20 -20
- data/lib/solargraph/language_server/host/diagnoser.rb +89 -89
- data/lib/solargraph/language_server/host/dispatch.rb +2 -3
- data/lib/solargraph/language_server/host/message_worker.rb +2 -2
- data/lib/solargraph/language_server/host/sources.rb +1 -1
- data/lib/solargraph/language_server/host.rb +24 -21
- data/lib/solargraph/language_server/message/base.rb +97 -97
- data/lib/solargraph/language_server/message/client/register_capability.rb +13 -15
- data/lib/solargraph/language_server/message/completion_item/resolve.rb +58 -60
- data/lib/solargraph/language_server/message/extended/check_gem_version.rb +10 -11
- data/lib/solargraph/language_server/message/extended/document_gems.rb +32 -32
- data/lib/solargraph/language_server/message/extended/download_core.rb +20 -19
- data/lib/solargraph/language_server/message/extended/search.rb +20 -20
- data/lib/solargraph/language_server/message/initialize.rb +197 -191
- data/lib/solargraph/language_server/message/text_document/completion.rb +8 -8
- data/lib/solargraph/language_server/message/text_document/definition.rb +41 -34
- data/lib/solargraph/language_server/message/text_document/document_highlight.rb +23 -16
- data/lib/solargraph/language_server/message/text_document/document_symbol.rb +29 -21
- data/lib/solargraph/language_server/message/text_document/formatting.rb +6 -6
- data/lib/solargraph/language_server/message/text_document/hover.rb +3 -5
- data/lib/solargraph/language_server/message/text_document/prepare_rename.rb +18 -11
- data/lib/solargraph/language_server/message/text_document/references.rb +23 -16
- data/lib/solargraph/language_server/message/text_document/rename.rb +26 -19
- data/lib/solargraph/language_server/message/text_document/signature_help.rb +2 -2
- data/lib/solargraph/language_server/message/text_document/type_definition.rb +25 -19
- data/lib/solargraph/language_server/message/workspace/did_change_configuration.rb +41 -35
- data/lib/solargraph/language_server/message/workspace/did_change_watched_files.rb +48 -40
- data/lib/solargraph/language_server/message/workspace/did_change_workspace_folders.rb +32 -26
- data/lib/solargraph/language_server/message/workspace/workspace_symbol.rb +27 -19
- data/lib/solargraph/language_server/message.rb +94 -94
- data/lib/solargraph/language_server/request.rb +29 -27
- data/lib/solargraph/language_server/transport/data_reader.rb +72 -74
- data/lib/solargraph/language_server/uri_helpers.rb +49 -49
- data/lib/solargraph/library.rb +68 -95
- data/lib/solargraph/location.rb +10 -12
- data/lib/solargraph/logging.rb +4 -6
- data/lib/solargraph/page.rb +92 -92
- data/lib/solargraph/parser/comment_ripper.rb +12 -4
- data/lib/solargraph/parser/flow_sensitive_typing.rb +32 -44
- data/lib/solargraph/parser/node_processor/base.rb +4 -4
- data/lib/solargraph/parser/node_processor.rb +1 -1
- data/lib/solargraph/parser/parser_gem/class_methods.rb +4 -6
- data/lib/solargraph/parser/parser_gem/flawed_builder.rb +19 -19
- data/lib/solargraph/parser/parser_gem/node_chainer.rb +20 -20
- data/lib/solargraph/parser/parser_gem/node_methods.rb +66 -65
- data/lib/solargraph/parser/parser_gem/node_processors/args_node.rb +12 -12
- data/lib/solargraph/parser/parser_gem/node_processors/block_node.rb +1 -2
- data/lib/solargraph/parser/parser_gem/node_processors/def_node.rb +3 -3
- data/lib/solargraph/parser/parser_gem/node_processors/defs_node.rb +38 -37
- data/lib/solargraph/parser/parser_gem/node_processors/if_node.rb +3 -3
- data/lib/solargraph/parser/parser_gem/node_processors/ivasgn_node.rb +2 -1
- data/lib/solargraph/parser/parser_gem/node_processors/opasgn_node.rb +1 -1
- data/lib/solargraph/parser/parser_gem/node_processors/sclass_node.rb +3 -5
- data/lib/solargraph/parser/parser_gem/node_processors/send_node.rb +118 -112
- data/lib/solargraph/parser/parser_gem/node_processors/until_node.rb +29 -29
- data/lib/solargraph/parser/parser_gem/node_processors/when_node.rb +1 -1
- data/lib/solargraph/parser/parser_gem/node_processors/while_node.rb +1 -1
- data/lib/solargraph/parser/parser_gem.rb +14 -12
- data/lib/solargraph/parser/snippet.rb +2 -0
- data/lib/solargraph/parser.rb +25 -23
- data/lib/solargraph/pin/base.rb +76 -64
- data/lib/solargraph/pin/base_variable.rb +28 -71
- data/lib/solargraph/pin/block.rb +3 -2
- data/lib/solargraph/pin/breakable.rb +2 -0
- data/lib/solargraph/pin/callable.rb +23 -26
- data/lib/solargraph/pin/closure.rb +5 -4
- data/lib/solargraph/pin/common.rb +5 -2
- data/lib/solargraph/pin/compound_statement.rb +3 -3
- data/lib/solargraph/pin/constant.rb +43 -45
- data/lib/solargraph/pin/conversions.rb +9 -4
- data/lib/solargraph/pin/delegated_method.rb +4 -4
- data/lib/solargraph/pin/documenting.rb +3 -2
- data/lib/solargraph/pin/local_variable.rb +4 -4
- data/lib/solargraph/pin/method.rb +74 -70
- data/lib/solargraph/pin/namespace.rb +13 -12
- data/lib/solargraph/pin/parameter.rb +28 -27
- data/lib/solargraph/pin/proxy_type.rb +2 -0
- data/lib/solargraph/pin/reference/type_alias.rb +16 -0
- data/lib/solargraph/pin/reference.rb +18 -0
- data/lib/solargraph/pin/search.rb +2 -2
- data/lib/solargraph/pin/signature.rb +9 -14
- data/lib/solargraph/pin/symbol.rb +1 -0
- data/lib/solargraph/pin/until.rb +1 -3
- data/lib/solargraph/pin/while.rb +1 -3
- data/lib/solargraph/pin_cache.rb +71 -488
- data/lib/solargraph/position.rb +38 -17
- data/lib/solargraph/range.rb +10 -9
- data/lib/solargraph/rbs_map/conversions.rb +327 -221
- data/lib/solargraph/rbs_map/core_fills.rb +91 -84
- data/lib/solargraph/rbs_map/stdlib_map.rb +0 -1
- data/lib/solargraph/rbs_map.rb +5 -15
- data/lib/solargraph/server_methods.rb +16 -16
- data/lib/solargraph/shell.rb +224 -66
- data/lib/solargraph/source/chain/array.rb +39 -37
- data/lib/solargraph/source/chain/call.rb +49 -44
- data/lib/solargraph/source/chain/class_variable.rb +13 -13
- data/lib/solargraph/source/chain/constant.rb +3 -1
- data/lib/solargraph/source/chain/global_variable.rb +13 -13
- data/lib/solargraph/source/chain/hash.rb +8 -6
- data/lib/solargraph/source/chain/if.rb +11 -10
- data/lib/solargraph/source/chain/instance_variable.rb +3 -1
- data/lib/solargraph/source/chain/link.rb +99 -109
- data/lib/solargraph/source/chain/literal.rb +4 -6
- data/lib/solargraph/source/chain/or.rb +2 -4
- data/lib/solargraph/source/chain/q_call.rb +13 -11
- data/lib/solargraph/source/chain/variable.rb +15 -13
- data/lib/solargraph/source/chain/z_super.rb +28 -30
- data/lib/solargraph/source/chain.rb +26 -16
- data/lib/solargraph/source/change.rb +3 -3
- data/lib/solargraph/source/cursor.rb +18 -18
- data/lib/solargraph/source/encoding_fixes.rb +6 -7
- data/lib/solargraph/source/source_chainer.rb +46 -32
- data/lib/solargraph/source/updater.rb +1 -1
- data/lib/solargraph/source.rb +27 -29
- data/lib/solargraph/source_map/clip.rb +38 -30
- data/lib/solargraph/source_map/mapper.rb +51 -47
- data/lib/solargraph/source_map.rb +8 -4
- data/lib/solargraph/type_checker/rules.rb +8 -8
- data/lib/solargraph/type_checker.rb +95 -102
- data/lib/solargraph/version.rb +1 -1
- data/lib/solargraph/workspace/config.rb +11 -10
- data/lib/solargraph/workspace/gemspecs.rb +3 -3
- data/lib/solargraph/workspace.rb +45 -165
- data/lib/solargraph/yard_map/helpers.rb +6 -2
- data/lib/solargraph/yard_map/mapper/to_method.rb +8 -6
- data/lib/solargraph/yard_map/mapper/to_namespace.rb +1 -1
- data/lib/solargraph/yard_map/mapper.rb +12 -12
- data/lib/solargraph/yard_map.rb +17 -18
- data/lib/solargraph/yard_tags.rb +20 -20
- data/lib/solargraph/yardoc.rb +26 -33
- data/lib/solargraph.rb +7 -5
- data/solargraph.gemspec +36 -35
- metadata +33 -38
|
@@ -11,10 +11,6 @@ module Solargraph
|
|
|
11
11
|
|
|
12
12
|
attr_reader :all_params, :subtypes, :key_types
|
|
13
13
|
|
|
14
|
-
protected def equality_fields
|
|
15
|
-
[@name, @all_params, @subtypes, @key_types]
|
|
16
|
-
end
|
|
17
|
-
|
|
18
14
|
# Create a UniqueType with the specified name and an optional substring.
|
|
19
15
|
# The substring is the parameter section of a parametrized type, e.g.,
|
|
20
16
|
# for the type `Array<String>`, the name is `Array` and the substring is
|
|
@@ -25,11 +21,9 @@ module Solargraph
|
|
|
25
21
|
# @param make_rooted [Boolean, nil]
|
|
26
22
|
# @return [UniqueType]
|
|
27
23
|
def self.parse name, substring = '', make_rooted: nil
|
|
28
|
-
if name.start_with?(':::')
|
|
29
|
-
raise ComplexTypeError, "Illegal prefix: #{name}"
|
|
30
|
-
end
|
|
24
|
+
raise ComplexTypeError, "Illegal prefix: #{name}" if name.start_with?(':::')
|
|
31
25
|
if name.start_with?('::')
|
|
32
|
-
name = name[2
|
|
26
|
+
name = name[2..]
|
|
33
27
|
rooted = true
|
|
34
28
|
elsif !can_root_name?(name)
|
|
35
29
|
rooted = true
|
|
@@ -48,13 +42,17 @@ module Solargraph
|
|
|
48
42
|
# @sg-ignore Need to add nil check here
|
|
49
43
|
parameters_type = PARAMETERS_TYPE_BY_STARTING_TAG.fetch(substring[0])
|
|
50
44
|
if parameters_type == :hash
|
|
51
|
-
|
|
45
|
+
unless !subs.is_a?(ComplexType) && (subs.length == 2) && !subs[0].is_a?(UniqueType) && !subs[1].is_a?(UniqueType)
|
|
46
|
+
raise ComplexTypeError,
|
|
47
|
+
"Bad hash type: name=#{name}, substring=#{substring}"
|
|
48
|
+
end
|
|
52
49
|
key_types.concat(subs[0].map { |u| ComplexType.new([u]) })
|
|
53
50
|
subtypes.concat(subs[1].map { |u| ComplexType.new([u]) })
|
|
54
51
|
elsif parameters_type == :list && name == 'Hash'
|
|
55
52
|
# Treat Hash<A, B> as Hash{A => B}
|
|
56
53
|
if subs.length != 2
|
|
57
|
-
raise ComplexTypeError,
|
|
54
|
+
raise ComplexTypeError,
|
|
55
|
+
"Bad hash type: name=#{name}, substring=#{substring} - must have exactly two parameters"
|
|
58
56
|
end
|
|
59
57
|
key_types.concat(subs[0].map { |u| ComplexType.new([u]) })
|
|
60
58
|
subtypes.concat(subs[1].map { |u| ComplexType.new([u]) })
|
|
@@ -71,9 +69,9 @@ module Solargraph
|
|
|
71
69
|
# @param subtypes [Array<ComplexType>]
|
|
72
70
|
# @param rooted [Boolean]
|
|
73
71
|
# @param parameters_type [Symbol, nil]
|
|
74
|
-
def initialize
|
|
75
|
-
if parameters_type.nil?
|
|
76
|
-
raise
|
|
72
|
+
def initialize name, key_types = [], subtypes = [], rooted:, parameters_type: nil
|
|
73
|
+
if parameters_type.nil? && !(key_types.empty? && subtypes.empty?)
|
|
74
|
+
raise 'You must supply parameters_type if you provide parameters'
|
|
77
75
|
end
|
|
78
76
|
raise "Please remove leading :: and set rooted instead - #{name.inspect}" if name.start_with?('::')
|
|
79
77
|
@name = name
|
|
@@ -95,7 +93,7 @@ module Solargraph
|
|
|
95
93
|
# @todo use api_map to establish number of generics in type;
|
|
96
94
|
# if only one is allowed but multiple are passed in, treat
|
|
97
95
|
# those as implicit unions
|
|
98
|
-
[
|
|
96
|
+
%w[Hash Array Set _ToAry Enumerable _Each].include?(name) && parameters_type != :fixed
|
|
99
97
|
end
|
|
100
98
|
|
|
101
99
|
def to_s
|
|
@@ -175,15 +173,14 @@ module Solargraph
|
|
|
175
173
|
# | `false`
|
|
176
174
|
return name if name.empty?
|
|
177
175
|
return 'NilClass' if name == 'nil'
|
|
178
|
-
return 'Boolean' if [
|
|
176
|
+
return 'Boolean' if %w[true false].include?(name)
|
|
179
177
|
return 'Symbol' if name[0] == ':'
|
|
180
|
-
# @sg-ignore Need to add nil check here
|
|
181
178
|
return 'String' if ['"', "'"].include?(name[0])
|
|
182
179
|
return 'Integer' if name.match?(/^-?\d+$/)
|
|
183
180
|
name
|
|
184
181
|
end
|
|
185
182
|
|
|
186
|
-
def eql?
|
|
183
|
+
def eql? other
|
|
187
184
|
self.class == other.class &&
|
|
188
185
|
# @sg-ignore flow sensitive typing should support .class == .class
|
|
189
186
|
@name == other.name &&
|
|
@@ -199,7 +196,7 @@ module Solargraph
|
|
|
199
196
|
@parameters_type == other.parameters_type
|
|
200
197
|
end
|
|
201
198
|
|
|
202
|
-
def ==
|
|
199
|
+
def == other
|
|
203
200
|
eql?(other)
|
|
204
201
|
end
|
|
205
202
|
|
|
@@ -231,20 +228,20 @@ module Solargraph
|
|
|
231
228
|
# covariant
|
|
232
229
|
# contravariant?: Proc - can be changed, so we can pass
|
|
233
230
|
# in less specific super types
|
|
234
|
-
if [
|
|
231
|
+
if %w[Hash Tuple Array Set Enumerable].include?(name) && fixed_parameters?
|
|
235
232
|
:covariant
|
|
236
233
|
else
|
|
237
234
|
default
|
|
238
235
|
end
|
|
239
236
|
end
|
|
240
237
|
|
|
241
|
-
# Whether this is an RBS interface like _ToAry or
|
|
238
|
+
# Whether this is an RBS interface like _ToAry or Hash::_Key.
|
|
242
239
|
def interface?
|
|
243
|
-
name.start_with?('_')
|
|
240
|
+
name.start_with?('_') || name.include?('::_')
|
|
244
241
|
end
|
|
245
242
|
|
|
246
243
|
# @param other [UniqueType]
|
|
247
|
-
def erased_version_of?
|
|
244
|
+
def erased_version_of? other
|
|
248
245
|
name == other.name && (all_params.empty? || all_params.all?(&:undefined?))
|
|
249
246
|
end
|
|
250
247
|
|
|
@@ -253,8 +250,8 @@ module Solargraph
|
|
|
253
250
|
# @param situation [:method_call, :assignment, :return_type]
|
|
254
251
|
# @param rules [Array<:allow_subtype_skew, :allow_empty_params, :allow_reverse_match, :allow_any_match, :allow_undefined, :allow_unresolved_generic>]
|
|
255
252
|
# @param variance [:invariant, :covariant, :contravariant]
|
|
256
|
-
def conforms_to?
|
|
257
|
-
variance: erased_variance(situation)
|
|
253
|
+
def conforms_to? api_map, expected, situation, rules = [],
|
|
254
|
+
variance: erased_variance(situation)
|
|
258
255
|
return true if undefined? && rules.include?(:allow_undefined)
|
|
259
256
|
|
|
260
257
|
# @todo teach this to validate duck types as inferred type
|
|
@@ -315,9 +312,9 @@ module Solargraph
|
|
|
315
312
|
'nil'
|
|
316
313
|
elsif name == GENERIC_TAG_NAME
|
|
317
314
|
all_params.first&.name
|
|
318
|
-
elsif [
|
|
315
|
+
elsif %w[Class Module].include?(name)
|
|
319
316
|
rbs_name
|
|
320
|
-
elsif [
|
|
317
|
+
elsif %w[Tuple Array].include?(name) && fixed_parameters?
|
|
321
318
|
# tuples don't have a name; they're just [foo, bar, baz].
|
|
322
319
|
if substring == '()'
|
|
323
320
|
# but there are no zero element tuples, so we go with an array
|
|
@@ -342,7 +339,7 @@ module Solargraph
|
|
|
342
339
|
|
|
343
340
|
# @param types [Array<UniqueType, ComplexType>]
|
|
344
341
|
# @return [String]
|
|
345
|
-
def rbs_union
|
|
342
|
+
def rbs_union types
|
|
346
343
|
if types.length == 1
|
|
347
344
|
types.first.to_rbs
|
|
348
345
|
else
|
|
@@ -387,7 +384,6 @@ module Solargraph
|
|
|
387
384
|
def resolve_generics_from_context generics_to_resolve, context_type, resolved_generic_values: {}
|
|
388
385
|
if name == ComplexType::GENERIC_TAG_NAME
|
|
389
386
|
type_param = subtypes.first&.name
|
|
390
|
-
# @sg-ignore flow sensitive typing needs to eliminate literal from union with [:bar].include?(foo)
|
|
391
387
|
return self unless generics_to_resolve.include? type_param
|
|
392
388
|
# @sg-ignore flow sensitive typing needs to eliminate literal from union with [:bar].include?(foo)
|
|
393
389
|
unless context_type.nil? || !resolved_generic_values[type_param].nil?
|
|
@@ -397,7 +393,8 @@ module Solargraph
|
|
|
397
393
|
end
|
|
398
394
|
if new_binding
|
|
399
395
|
resolved_generic_values.transform_values! do |complex_type|
|
|
400
|
-
complex_type.resolve_generics_from_context(generics_to_resolve, nil,
|
|
396
|
+
complex_type.resolve_generics_from_context(generics_to_resolve, nil,
|
|
397
|
+
resolved_generic_values: resolved_generic_values)
|
|
401
398
|
end
|
|
402
399
|
end
|
|
403
400
|
# @sg-ignore flow sensitive typing needs to eliminate literal from union with [:bar].include?(foo)
|
|
@@ -405,8 +402,10 @@ module Solargraph
|
|
|
405
402
|
end
|
|
406
403
|
|
|
407
404
|
# @todo typechecking should complain when the method being called has no @yieldparam tag
|
|
408
|
-
new_key_types = resolve_param_generics_from_context(generics_to_resolve, context_type, resolved_generic_values,
|
|
409
|
-
|
|
405
|
+
new_key_types = resolve_param_generics_from_context(generics_to_resolve, context_type, resolved_generic_values,
|
|
406
|
+
&:key_types)
|
|
407
|
+
new_subtypes = resolve_param_generics_from_context(generics_to_resolve, context_type, resolved_generic_values,
|
|
408
|
+
&:subtypes)
|
|
410
409
|
recreate(new_key_types: new_key_types, new_subtypes: new_subtypes)
|
|
411
410
|
end
|
|
412
411
|
|
|
@@ -415,7 +414,7 @@ module Solargraph
|
|
|
415
414
|
# @param resolved_generic_values [Hash{String => ComplexType}]
|
|
416
415
|
# @yieldreturn [Array<ComplexType>]
|
|
417
416
|
# @return [Array<ComplexType>]
|
|
418
|
-
def resolve_param_generics_from_context
|
|
417
|
+
def resolve_param_generics_from_context generics_to_resolve, context_type, resolved_generic_values
|
|
419
418
|
types = yield self
|
|
420
419
|
types.each_with_index.flat_map do |ct, i|
|
|
421
420
|
ct.items.flat_map do |ut|
|
|
@@ -423,10 +422,12 @@ module Solargraph
|
|
|
423
422
|
if context_params && context_params[i]
|
|
424
423
|
type_arg = context_params[i]
|
|
425
424
|
type_arg.map do |new_unique_context_type|
|
|
426
|
-
ut.resolve_generics_from_context generics_to_resolve, new_unique_context_type,
|
|
425
|
+
ut.resolve_generics_from_context generics_to_resolve, new_unique_context_type,
|
|
426
|
+
resolved_generic_values: resolved_generic_values
|
|
427
427
|
end
|
|
428
428
|
else
|
|
429
|
-
ut.resolve_generics_from_context generics_to_resolve, nil,
|
|
429
|
+
ut.resolve_generics_from_context generics_to_resolve, nil,
|
|
430
|
+
resolved_generic_values: resolved_generic_values
|
|
430
431
|
end
|
|
431
432
|
end
|
|
432
433
|
end
|
|
@@ -448,7 +449,7 @@ module Solargraph
|
|
|
448
449
|
idx = definitions.generics.index(generic_name)
|
|
449
450
|
next t if idx.nil?
|
|
450
451
|
if context_type.parameters_type == :hash
|
|
451
|
-
if idx
|
|
452
|
+
if idx.zero?
|
|
452
453
|
next ComplexType.new(context_type.key_types)
|
|
453
454
|
elsif idx == 1
|
|
454
455
|
next ComplexType.new(context_type.subtypes)
|
|
@@ -456,7 +457,7 @@ module Solargraph
|
|
|
456
457
|
next ComplexType::UNDEFINED
|
|
457
458
|
end
|
|
458
459
|
elsif context_type.all?(&:implicit_union?)
|
|
459
|
-
if idx
|
|
460
|
+
if idx.zero? && !context_type.all_params.empty?
|
|
460
461
|
ComplexType.new(context_type.all_params)
|
|
461
462
|
else
|
|
462
463
|
ComplexType::UNDEFINED
|
|
@@ -482,7 +483,7 @@ module Solargraph
|
|
|
482
483
|
# @yieldreturn [self]
|
|
483
484
|
# @return [Enumerable<self>]
|
|
484
485
|
def each &block
|
|
485
|
-
[self].each
|
|
486
|
+
[self].each(&block)
|
|
486
487
|
end
|
|
487
488
|
|
|
488
489
|
# @return [Array<UniqueType>]
|
|
@@ -493,10 +494,10 @@ module Solargraph
|
|
|
493
494
|
# @param new_name [String, nil]
|
|
494
495
|
# @param make_rooted [Boolean, nil]
|
|
495
496
|
# @param new_key_types [Array<ComplexType>, nil]
|
|
496
|
-
# @param
|
|
497
|
+
# @param make_rooted [Boolean, nil]
|
|
497
498
|
# @param new_subtypes [Array<ComplexType>, nil]
|
|
498
499
|
# @return [self]
|
|
499
|
-
def recreate
|
|
500
|
+
def recreate new_name: nil, make_rooted: nil, new_key_types: nil, new_subtypes: nil
|
|
500
501
|
raise "Please remove leading :: and set rooted instead - #{new_name}" if new_name&.start_with?('::')
|
|
501
502
|
|
|
502
503
|
new_name ||= name
|
|
@@ -530,8 +531,10 @@ module Solargraph
|
|
|
530
531
|
# @yieldparam t [UniqueType]
|
|
531
532
|
# @yieldreturn [self]
|
|
532
533
|
# @return [self]
|
|
533
|
-
def transform
|
|
534
|
-
|
|
534
|
+
def transform new_name = nil, &transform_type
|
|
535
|
+
if new_name&.start_with?('::')
|
|
536
|
+
raise "Please remove leading :: and set rooted with recreate() instead - #{new_name}"
|
|
537
|
+
end
|
|
535
538
|
if name == ComplexType::GENERIC_TAG_NAME
|
|
536
539
|
# doesn't make sense to manipulate the name of the generic
|
|
537
540
|
new_key_types = @key_types
|
|
@@ -540,14 +543,15 @@ module Solargraph
|
|
|
540
543
|
new_key_types = @key_types.flat_map { |ct| ct.items.map { |ut| ut.transform(&transform_type) } }
|
|
541
544
|
new_subtypes = @subtypes.flat_map { |ct| ct.items.map { |ut| ut.transform(&transform_type) } }
|
|
542
545
|
end
|
|
543
|
-
new_type = recreate(new_name: new_name || name, new_key_types: new_key_types, new_subtypes: new_subtypes,
|
|
546
|
+
new_type = recreate(new_name: new_name || name, new_key_types: new_key_types, new_subtypes: new_subtypes,
|
|
547
|
+
make_rooted: @rooted)
|
|
544
548
|
yield new_type
|
|
545
549
|
end
|
|
546
550
|
|
|
547
551
|
# Generate a ComplexType that fully qualifies this type's namespaces.
|
|
548
552
|
#
|
|
549
553
|
# @param api_map [ApiMap] The ApiMap that performs qualification
|
|
550
|
-
# @param
|
|
554
|
+
# @param gates [Array<String>] The namespaces from which to resolve names
|
|
551
555
|
# @return [self, ComplexType, UniqueType] The generated ComplexType
|
|
552
556
|
def qualify api_map, *gates
|
|
553
557
|
transform do |t|
|
|
@@ -585,7 +589,7 @@ module Solargraph
|
|
|
585
589
|
# @return [ComplexType]
|
|
586
590
|
def reduce_class_type
|
|
587
591
|
new_items = items.flat_map do |type|
|
|
588
|
-
next type unless [
|
|
592
|
+
next type unless %w[Module Class].include?(type.name)
|
|
589
593
|
next type if type.all_params.empty?
|
|
590
594
|
|
|
591
595
|
type.all_params
|
|
@@ -603,12 +607,12 @@ module Solargraph
|
|
|
603
607
|
end
|
|
604
608
|
|
|
605
609
|
# @param name_to_check [String]
|
|
606
|
-
def can_root_name?
|
|
610
|
+
def can_root_name? name_to_check = name
|
|
607
611
|
self.class.can_root_name?(name_to_check)
|
|
608
612
|
end
|
|
609
613
|
|
|
610
614
|
# @param name [String]
|
|
611
|
-
def self.can_root_name?
|
|
615
|
+
def self.can_root_name? name
|
|
612
616
|
# name is not lowercase
|
|
613
617
|
!name.empty? && name != name.downcase
|
|
614
618
|
end
|
|
@@ -625,8 +629,13 @@ module Solargraph
|
|
|
625
629
|
'::NilClass' => UniqueType::NIL
|
|
626
630
|
}.freeze
|
|
627
631
|
|
|
628
|
-
|
|
629
632
|
include Logging
|
|
633
|
+
|
|
634
|
+
protected
|
|
635
|
+
|
|
636
|
+
def equality_fields
|
|
637
|
+
[@name, @all_params, @subtypes, @key_types]
|
|
638
|
+
end
|
|
630
639
|
end
|
|
631
640
|
end
|
|
632
641
|
end
|
|
@@ -4,7 +4,7 @@ module Solargraph
|
|
|
4
4
|
# A container for type data based on YARD type tags.
|
|
5
5
|
#
|
|
6
6
|
class ComplexType
|
|
7
|
-
GENERIC_TAG_NAME = 'generic'
|
|
7
|
+
GENERIC_TAG_NAME = 'generic'
|
|
8
8
|
# @!parse
|
|
9
9
|
# include TypeMethods
|
|
10
10
|
include Equality
|
|
@@ -19,7 +19,7 @@ module Solargraph
|
|
|
19
19
|
# @type [Array<UniqueType>]
|
|
20
20
|
items = types.flat_map(&:items).uniq(&:to_s)
|
|
21
21
|
if items.any? { |i| i.name == 'false' } && items.any? { |i| i.name == 'true' }
|
|
22
|
-
items.delete_if { |i|
|
|
22
|
+
items.delete_if { |i| %w[false true].include?(i.name) }
|
|
23
23
|
items.unshift(UniqueType::BOOLEAN)
|
|
24
24
|
end
|
|
25
25
|
# @type [Array<UniqueType>]
|
|
@@ -29,19 +29,16 @@ module Solargraph
|
|
|
29
29
|
@items = items
|
|
30
30
|
end
|
|
31
31
|
|
|
32
|
-
protected def equality_fields
|
|
33
|
-
[self.class, items]
|
|
34
|
-
end
|
|
35
|
-
|
|
36
32
|
# @param api_map [ApiMap]
|
|
37
|
-
# @param
|
|
33
|
+
# @param gates [Array<String>]
|
|
34
|
+
#
|
|
38
35
|
# @return [ComplexType]
|
|
39
36
|
def qualify api_map, *gates
|
|
40
37
|
red = reduce_object
|
|
41
38
|
types = red.items.map do |t|
|
|
42
|
-
next t if [
|
|
39
|
+
next t if %w[nil void undefined].include?(t.name)
|
|
43
40
|
next t if ['::Boolean'].include?(t.rooted_name)
|
|
44
|
-
t.qualify
|
|
41
|
+
api_map.unalias(t.name) || t.qualify(api_map, *gates)
|
|
45
42
|
end
|
|
46
43
|
ComplexType.new(types).reduce_object
|
|
47
44
|
end
|
|
@@ -53,7 +50,10 @@ module Solargraph
|
|
|
53
50
|
def resolve_generics_from_context generics_to_resolve, context_type, resolved_generic_values: {}
|
|
54
51
|
return self unless generic?
|
|
55
52
|
|
|
56
|
-
ComplexType.new(@items.map
|
|
53
|
+
ComplexType.new(@items.map do |i|
|
|
54
|
+
i.resolve_generics_from_context(generics_to_resolve, context_type,
|
|
55
|
+
resolved_generic_values: resolved_generic_values)
|
|
56
|
+
end)
|
|
57
57
|
end
|
|
58
58
|
|
|
59
59
|
# @return [UniqueType]
|
|
@@ -84,14 +84,14 @@ module Solargraph
|
|
|
84
84
|
# @sg-ignore Declared return type
|
|
85
85
|
# ::Array<::Solargraph::ComplexType::UniqueType> does not match
|
|
86
86
|
# inferred type ::Array<::Proc> for Solargraph::ComplexType#map
|
|
87
|
-
def map
|
|
87
|
+
def map &block
|
|
88
88
|
@items.map(&block)
|
|
89
89
|
end
|
|
90
90
|
|
|
91
91
|
# @yieldparam [UniqueType]
|
|
92
92
|
# @return [Enumerable<UniqueType>]
|
|
93
93
|
def each &block
|
|
94
|
-
@items.each
|
|
94
|
+
@items.each(&block)
|
|
95
95
|
end
|
|
96
96
|
|
|
97
97
|
# @yieldparam [UniqueType]
|
|
@@ -102,17 +102,17 @@ module Solargraph
|
|
|
102
102
|
return enum_for(__method__) unless block_given?
|
|
103
103
|
|
|
104
104
|
@items.each do |item|
|
|
105
|
-
item.each_unique_type
|
|
105
|
+
item.each_unique_type(&block)
|
|
106
106
|
end
|
|
107
107
|
end
|
|
108
108
|
|
|
109
109
|
# @param new_name [String, nil]
|
|
110
110
|
# @param make_rooted [Boolean, nil]
|
|
111
111
|
# @param new_key_types [Array<ComplexType>, nil]
|
|
112
|
-
# @param
|
|
112
|
+
# @param make_rooted [Boolean, nil]
|
|
113
113
|
# @param new_subtypes [Array<ComplexType>, nil]
|
|
114
114
|
# @return [self]
|
|
115
|
-
def recreate
|
|
115
|
+
def recreate new_name: nil, make_rooted: nil, new_key_types: nil, new_subtypes: nil
|
|
116
116
|
ComplexType.new(map do |ut|
|
|
117
117
|
ut.recreate(new_name: new_name,
|
|
118
118
|
make_rooted: make_rooted,
|
|
@@ -133,13 +133,13 @@ module Solargraph
|
|
|
133
133
|
|
|
134
134
|
# @param index [Integer]
|
|
135
135
|
# @return [UniqueType]
|
|
136
|
-
def []
|
|
136
|
+
def [] index
|
|
137
137
|
@items[index]
|
|
138
138
|
end
|
|
139
139
|
|
|
140
140
|
# @return [Array<UniqueType>]
|
|
141
141
|
def select &block
|
|
142
|
-
@items.select
|
|
142
|
+
@items.select(&block)
|
|
143
143
|
end
|
|
144
144
|
|
|
145
145
|
# @return [String]
|
|
@@ -154,7 +154,9 @@ module Solargraph
|
|
|
154
154
|
end
|
|
155
155
|
|
|
156
156
|
# @param name [Symbol]
|
|
157
|
+
#
|
|
157
158
|
# @return [Object, nil]
|
|
159
|
+
# @param [Array<Object>] args
|
|
158
160
|
def method_missing name, *args, &block
|
|
159
161
|
return if @items.first.nil?
|
|
160
162
|
return @items.first.send(name, *args, &block) if respond_to_missing?(name)
|
|
@@ -163,7 +165,7 @@ module Solargraph
|
|
|
163
165
|
|
|
164
166
|
# @param name [Symbol]
|
|
165
167
|
# @param include_private [Boolean]
|
|
166
|
-
def respond_to_missing?
|
|
168
|
+
def respond_to_missing? name, include_private = false
|
|
167
169
|
TypeMethods.public_instance_methods.include?(name) || super
|
|
168
170
|
end
|
|
169
171
|
|
|
@@ -198,24 +200,29 @@ module Solargraph
|
|
|
198
200
|
# @param api_map [ApiMap]
|
|
199
201
|
# @param expected [ComplexType, ComplexType::UniqueType]
|
|
200
202
|
# @param situation [:method_call, :return_type, :assignment]
|
|
201
|
-
# @param allow_subtype_skew [Boolean] if false, check if any
|
|
202
|
-
# subtypes of the expected type match the inferred type
|
|
203
|
-
# @param allow_reverse_match [Boolean] if true, check if any subtypes
|
|
204
|
-
# of the expected type match the inferred type
|
|
205
|
-
# @param allow_empty_params [Boolean] if true, allow a general
|
|
206
|
-
# inferred type without parameters to conform to a more specific
|
|
207
|
-
# expected type
|
|
208
|
-
# @param allow_any_match [Boolean] if true, any unique type
|
|
209
|
-
# matched in the inferred qualifies as a match
|
|
210
|
-
# @param allow_undefined [Boolean] if true, treat undefined as a
|
|
211
|
-
# wildcard that matches anything
|
|
212
203
|
# @param rules [Array<:allow_subtype_skew, :allow_empty_params, :allow_reverse_match, :allow_any_match, :allow_undefined, :allow_unresolved_generic, :allow_unmatched_interface>]
|
|
204
|
+
#
|
|
205
|
+
# allow_subtype_skew: if not provided, check if any subtypes of
|
|
206
|
+
# the expected type match the inferred type
|
|
207
|
+
#
|
|
208
|
+
# allow_reverse_match: check if any subtypes
|
|
209
|
+
# of the expected type match the inferred type
|
|
210
|
+
#
|
|
211
|
+
# allow_empty_params: allow a general inferred type without
|
|
212
|
+
# parameters to conform to a more specific expected type
|
|
213
|
+
#
|
|
214
|
+
# allow_any_match: any unique type matched in the inferred
|
|
215
|
+
# qualifies as a match
|
|
216
|
+
#
|
|
217
|
+
# allow_undefined: treat undefined as a wildcard that matches
|
|
218
|
+
# anything
|
|
219
|
+
#
|
|
213
220
|
# @param variance [:invariant, :covariant, :contravariant]
|
|
214
221
|
# @return [Boolean]
|
|
215
|
-
def conforms_to?
|
|
222
|
+
def conforms_to? api_map, expected,
|
|
216
223
|
situation,
|
|
217
224
|
rules = [],
|
|
218
|
-
variance: erased_variance(situation)
|
|
225
|
+
variance: erased_variance(situation)
|
|
219
226
|
expected = expected.downcast_to_literal_if_possible
|
|
220
227
|
inferred = downcast_to_literal_if_possible
|
|
221
228
|
|
|
@@ -256,14 +263,14 @@ module Solargraph
|
|
|
256
263
|
|
|
257
264
|
# @yieldparam [UniqueType]
|
|
258
265
|
def all? &block
|
|
259
|
-
@items.all?
|
|
266
|
+
@items.all?(&block)
|
|
260
267
|
end
|
|
261
268
|
|
|
262
269
|
# @yieldparam [UniqueType]
|
|
263
270
|
# @yieldreturn [Boolean]
|
|
264
271
|
# @return [Boolean]
|
|
265
272
|
def any? &block
|
|
266
|
-
@items.compact.any?
|
|
273
|
+
@items.compact.any?(&block)
|
|
267
274
|
end
|
|
268
275
|
|
|
269
276
|
def selfy?
|
|
@@ -283,8 +290,10 @@ module Solargraph
|
|
|
283
290
|
# @yieldparam t [UniqueType]
|
|
284
291
|
# @yieldreturn [UniqueType]
|
|
285
292
|
# @return [ComplexType]
|
|
286
|
-
def transform
|
|
287
|
-
|
|
293
|
+
def transform new_name = nil, &transform_type
|
|
294
|
+
if new_name&.start_with?('::')
|
|
295
|
+
raise "Please remove leading :: and set rooted with recreate() instead - #{new_name}"
|
|
296
|
+
end
|
|
288
297
|
ComplexType.new(map { |ut| ut.transform(new_name, &transform_type) })
|
|
289
298
|
end
|
|
290
299
|
|
|
@@ -322,7 +331,7 @@ module Solargraph
|
|
|
322
331
|
# @return [ComplexType]
|
|
323
332
|
def reduce_class_type
|
|
324
333
|
new_items = items.flat_map do |type|
|
|
325
|
-
next type unless [
|
|
334
|
+
next type unless %w[Module Class].include?(type.name)
|
|
326
335
|
next type if type.all_params.empty?
|
|
327
336
|
|
|
328
337
|
type.all_params
|
|
@@ -337,7 +346,7 @@ module Solargraph
|
|
|
337
346
|
end
|
|
338
347
|
|
|
339
348
|
# @param other [ComplexType, UniqueType]
|
|
340
|
-
def erased_version_of?
|
|
349
|
+
def erased_version_of? other
|
|
341
350
|
return false if items.length != 1 || other.items.length != 1
|
|
342
351
|
|
|
343
352
|
@items.first.erased_version_of?(other.items.first)
|
|
@@ -351,10 +360,6 @@ module Solargraph
|
|
|
351
360
|
|
|
352
361
|
attr_reader :items
|
|
353
362
|
|
|
354
|
-
def rooted?
|
|
355
|
-
@items.all?(&:rooted?)
|
|
356
|
-
end
|
|
357
|
-
|
|
358
363
|
# @param exclude_types [ComplexType, nil]
|
|
359
364
|
# @param api_map [ApiMap]
|
|
360
365
|
# @return [ComplexType, self]
|
|
@@ -391,6 +396,10 @@ module Solargraph
|
|
|
391
396
|
|
|
392
397
|
protected
|
|
393
398
|
|
|
399
|
+
def equality_fields
|
|
400
|
+
[self.class, items]
|
|
401
|
+
end
|
|
402
|
+
|
|
394
403
|
# @return [ComplexType]
|
|
395
404
|
def reduce_object
|
|
396
405
|
new_items = items.flat_map do |ut|
|
|
@@ -410,13 +419,11 @@ module Solargraph
|
|
|
410
419
|
# @example
|
|
411
420
|
# ComplexType.parse 'String', 'Foo', 'nil' #=> [String, Foo, nil]
|
|
412
421
|
#
|
|
413
|
-
# @
|
|
414
|
-
#
|
|
415
|
-
#
|
|
416
|
-
#
|
|
417
|
-
#
|
|
418
|
-
# used internally.
|
|
419
|
-
#
|
|
422
|
+
# @param partial [Boolean] if true, method is receiving a string
|
|
423
|
+
# that will be used inside another ComplexType. It returns
|
|
424
|
+
# arrays of ComplexTypes instead of a single cohesive one.
|
|
425
|
+
# Consumers should not need to use this parameter; it should
|
|
426
|
+
# only be used internally.
|
|
420
427
|
# @param strings [Array<String>] The type definitions to parse
|
|
421
428
|
# @return [ComplexType]
|
|
422
429
|
# # @overload parse(*strings, partial: false)
|
|
@@ -446,14 +453,14 @@ module Solargraph
|
|
|
446
453
|
# @param char [String]
|
|
447
454
|
type_string&.each_char do |char|
|
|
448
455
|
if char == '='
|
|
449
|
-
#raise ComplexTypeError, "Invalid = in type #{type_string}" unless curly_stack > 0
|
|
456
|
+
# raise ComplexTypeError, "Invalid = in type #{type_string}" unless curly_stack > 0
|
|
450
457
|
elsif char == '<'
|
|
451
458
|
point_stack += 1
|
|
452
459
|
elsif char == '>'
|
|
453
|
-
if subtype_string.end_with?('=') && curly_stack
|
|
460
|
+
if subtype_string.end_with?('=') && curly_stack.positive?
|
|
454
461
|
subtype_string += char
|
|
455
462
|
elsif base.end_with?('=')
|
|
456
|
-
raise ComplexTypeError,
|
|
463
|
+
raise ComplexTypeError, 'Invalid hash thing' unless key_types.nil?
|
|
457
464
|
# types.push ComplexType.new([UniqueType.new(base[0..-2].strip)])
|
|
458
465
|
# @sg-ignore Need to add nil check here
|
|
459
466
|
types.push UniqueType.parse(base[0..-2].strip, subtype_string)
|
|
@@ -466,7 +473,7 @@ module Solargraph
|
|
|
466
473
|
subtype_string.clear
|
|
467
474
|
next
|
|
468
475
|
else
|
|
469
|
-
raise ComplexTypeError, "Invalid close in type #{type_string}" if point_stack
|
|
476
|
+
raise ComplexTypeError, "Invalid close in type #{type_string}" if point_stack.zero?
|
|
470
477
|
point_stack -= 1
|
|
471
478
|
subtype_string += char
|
|
472
479
|
end
|
|
@@ -476,34 +483,37 @@ module Solargraph
|
|
|
476
483
|
elsif char == '}'
|
|
477
484
|
curly_stack -= 1
|
|
478
485
|
subtype_string += char
|
|
479
|
-
raise ComplexTypeError, "Invalid close in type #{type_string}" if curly_stack
|
|
486
|
+
raise ComplexTypeError, "Invalid close in type #{type_string}" if curly_stack.negative?
|
|
480
487
|
next
|
|
481
488
|
elsif char == '('
|
|
482
489
|
paren_stack += 1
|
|
483
490
|
elsif char == ')'
|
|
484
491
|
paren_stack -= 1
|
|
485
492
|
subtype_string += char
|
|
486
|
-
raise ComplexTypeError, "Invalid close in type #{type_string}" if paren_stack
|
|
493
|
+
raise ComplexTypeError, "Invalid close in type #{type_string}" if paren_stack.negative?
|
|
487
494
|
next
|
|
488
|
-
elsif char == ',' && point_stack
|
|
495
|
+
elsif char == ',' && point_stack.zero? && curly_stack.zero? && paren_stack.zero?
|
|
489
496
|
# types.push ComplexType.new([UniqueType.new(base.strip, subtype_string.strip)])
|
|
490
497
|
types.push UniqueType.parse(base.strip, subtype_string.strip)
|
|
491
498
|
base.clear
|
|
492
499
|
subtype_string.clear
|
|
493
500
|
next
|
|
494
501
|
end
|
|
495
|
-
if point_stack
|
|
502
|
+
if point_stack.zero? && curly_stack.zero? && paren_stack.zero?
|
|
496
503
|
base.concat char
|
|
497
504
|
else
|
|
498
505
|
subtype_string.concat char
|
|
499
506
|
end
|
|
500
507
|
end
|
|
501
|
-
|
|
508
|
+
if point_stack != 0 || curly_stack != 0 || paren_stack != 0
|
|
509
|
+
raise ComplexTypeError,
|
|
510
|
+
"Unclosed subtype in #{type_string}"
|
|
511
|
+
end
|
|
502
512
|
# types.push ComplexType.new([UniqueType.new(base, subtype_string)])
|
|
503
513
|
types.push UniqueType.parse(base.strip, subtype_string.strip)
|
|
504
514
|
end
|
|
505
515
|
unless key_types.nil?
|
|
506
|
-
raise ComplexTypeError,
|
|
516
|
+
raise ComplexTypeError, 'Invalid use of key/value parameters' unless partial
|
|
507
517
|
return key_types if types.empty?
|
|
508
518
|
return [key_types, types]
|
|
509
519
|
end
|
|
@@ -515,7 +525,7 @@ module Solargraph
|
|
|
515
525
|
# @param strings [Array<String>]
|
|
516
526
|
# @return [ComplexType]
|
|
517
527
|
def try_parse *strings
|
|
518
|
-
parse
|
|
528
|
+
parse(*strings)
|
|
519
529
|
rescue ComplexTypeError => e
|
|
520
530
|
Solargraph.logger.info "Error parsing complex type `#{strings.join(', ')}`: #{e.message}"
|
|
521
531
|
ComplexType::UNDEFINED
|
|
@@ -540,9 +550,7 @@ module Solargraph
|
|
|
540
550
|
# @param dst [String]
|
|
541
551
|
# @return [String]
|
|
542
552
|
def reduce_class dst
|
|
543
|
-
while dst =~ /^(Class|Module)
|
|
544
|
-
dst = dst.sub(/^(Class|Module)\</, '').sub(/\>$/, '')
|
|
545
|
-
end
|
|
553
|
+
dst = dst.sub(/^(Class|Module)</, '').sub(/>$/, '') while dst =~ /^(Class|Module)<(.*?)>$/
|
|
546
554
|
dst
|
|
547
555
|
end
|
|
548
556
|
end
|