Class: Solargraph::ComplexType
- Inherits:
-
Object
- Object
- Solargraph::ComplexType
- Includes:
- Equality
- Defined in:
- lib/solargraph/complex_type.rb,
lib/solargraph/complex_type/conformance.rb,
lib/solargraph/complex_type/unique_type.rb,
lib/solargraph/complex_type/type_methods.rb
Overview
A container for type data based on YARD type tags.
Defined Under Namespace
Modules: TypeMethods Classes: Conformance, UniqueType
Constant Summary collapse
- GENERIC_TAG_NAME =
'generic'
Instance Attribute Summary collapse
-
#items ⇒ Object
readonly
Returns the value of attribute items.
Class Method Summary collapse
-
.parse(*strings, partial: false) ⇒ ComplexType
Parse type strings into a ComplexType.
- .try_parse(*strings) ⇒ ComplexType
Instance Method Summary collapse
- #[](index) ⇒ UniqueType
- #all? {|| ... } ⇒ Boolean
- #all_params ⇒ Array<ComplexType>
-
#all_rooted? ⇒ Boolean
every type and subtype in this union have been resolved to be fully qualified.
- #any? {|| ... } ⇒ Boolean
- #conforms_to?(api_map, expected, situation, rules = [], variance: erased_variance(situation)) ⇒ Boolean
- #desc ⇒ String
- #downcast_to_literal_if_possible ⇒ ComplexType
- #duck_types_match?(api_map, expected, inferred, rules = []) ⇒ Boolean
- #each {|| ... } ⇒ Enumerable<UniqueType>
- #each_unique_type ⇒ Enumerator<UniqueType>
- #erased_version_of?(other) ⇒ Boolean
- #exclude(exclude_types, api_map) ⇒ ComplexType, self
- #expand(named_types) ⇒ Object
- #first ⇒ UniqueType
- #force_rooted ⇒ self
- #generic? ⇒ Boolean
-
#initialize(types = [UniqueType::UNDEFINED]) ⇒ ComplexType
constructor
A new instance of ComplexType.
- #intersect_with(intersection_type, api_map) ⇒ self, ComplexType::UniqueType
- #length ⇒ Integer
- #literal? ⇒ Boolean
-
#map {|| ... } ⇒ Array<UniqueType>
@sg-ignore Declared return type ::Array<::Solargraph::ComplexType::UniqueType> does not match inferred type ::Array<::Proc> for Solargraph::ComplexType#map.
- #method_missing(name, *args, &block) ⇒ Object?
- #namespace ⇒ String
- #namespaces ⇒ Array<String>
- #nullable? ⇒ Boolean
- #qualify(api_map, *gates) ⇒ ComplexType
- #recreate(new_name: nil, make_rooted: nil, new_key_types: nil, new_subtypes: nil) ⇒ self
- #reduce_class_type ⇒ ComplexType
- #resolve_generics(definitions, context_type) ⇒ ComplexType
- #resolve_generics_from_context(generics_to_resolve, context_type, resolved_generic_values: {}) ⇒ self
- #respond_to_missing?(name, include_private = false) ⇒ Boolean
-
#rooted? ⇒ Boolean
every top-level type has resolved to be fully qualified; see #all_rooted? to check their subtypes as well.
- #rooted_tags ⇒ String
- #select(&block) ⇒ Array<UniqueType>
- #self_to_type(dst) ⇒ ComplexType
- #selfy? ⇒ Boolean
- #simple_tags ⇒ String
- #simplify_literals ⇒ self
- #tags ⇒ String
- #to_a ⇒ Array<UniqueType>
- #to_rbs ⇒ String
- #to_s ⇒ Object
- #transform(new_name = nil) {|t| ... } ⇒ ComplexType
- #without_nil ⇒ ComplexType
Methods included from Equality
Constructor Details
#initialize(types = [UniqueType::UNDEFINED]) ⇒ ComplexType
Returns a new instance of ComplexType.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/solargraph/complex_type.rb', line 17 def initialize types = [UniqueType::UNDEFINED] # @todo @items here should not need an annotation # @type [Array<UniqueType>] items = types.flat_map(&:items).uniq(&:to_s) if items.any? { |i| i.name == 'false' } && items.any? { |i| i.name == 'true' } items.delete_if { |i| %w[false true].include?(i.name) } items.unshift(UniqueType::BOOLEAN) end # @type [Array<UniqueType>] items = [UniqueType::UNDEFINED] if items.any?(&:undefined?) # @todo shouldn't need this cast - if statement above adds an 'Array' type # @type [Array<UniqueType>] @items = items end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object?
161 162 163 164 165 |
# File 'lib/solargraph/complex_type.rb', line 161 def method_missing name, *args, &block return if @items.first.nil? return @items.first.send(name, *args, &block) if respond_to_missing?(name) super end |
Instance Attribute Details
#items ⇒ Object (readonly)
Returns the value of attribute items.
380 381 382 |
# File 'lib/solargraph/complex_type.rb', line 380 def items @items end |
Class Method Details
.parse(*strings, partial: false) ⇒ ComplexType
Parse type strings into a ComplexType.
@overload parse(*strings, partial: false)
@todo Need ability to use a literal true as a type below
@param partial [Boolean] True if the string is part of a another type
@return [Array]
@sg-ignore To be able to select the right signature above, Chain::Call needs to know the decl type (:arg, :optarg, :kwarg, etc) of the arguments given, instead of just having an array of Chains as the arguments.
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 |
# File 'lib/solargraph/complex_type.rb', line 456 def parse *strings, partial: false # @type [Hash{Array<String> => ComplexType, Array<ComplexType::UniqueType>}] @cache ||= {} unless partial cached = @cache[strings] return cached unless cached.nil? end # @types [Array<ComplexType::UniqueType>] types = [] key_types = nil strings.each do |type_string| point_stack = 0 curly_stack = 0 paren_stack = 0 base = String.new subtype_string = String.new # @param char [String] type_string&.each_char do |char| if char == '=' # raise ComplexTypeError, "Invalid = in type #{type_string}" unless curly_stack > 0 elsif char == '<' point_stack += 1 elsif char == '>' if subtype_string.end_with?('=') && curly_stack.positive? subtype_string += char elsif base.end_with?('=') raise ComplexTypeError, 'Invalid hash thing' unless key_types.nil? # types.push ComplexType.new([UniqueType.new(base[0..-2].strip)]) # @sg-ignore Need to add nil check here types.push UniqueType.parse(base[0..-2].strip, subtype_string) # @todo this should either expand key_type's type # automatically or complain about not being # compatible with key_type's type in type checking key_types = types types = [] base.clear subtype_string.clear next else raise ComplexTypeError, "Invalid close in type #{type_string}" if point_stack.zero? point_stack -= 1 subtype_string += char end next elsif char == '{' curly_stack += 1 elsif char == '}' curly_stack -= 1 subtype_string += char raise ComplexTypeError, "Invalid close in type #{type_string}" if curly_stack.negative? next elsif char == '(' paren_stack += 1 elsif char == ')' paren_stack -= 1 subtype_string += char raise ComplexTypeError, "Invalid close in type #{type_string}" if paren_stack.negative? next elsif char == ',' && point_stack.zero? && curly_stack.zero? && paren_stack.zero? # types.push ComplexType.new([UniqueType.new(base.strip, subtype_string.strip)]) types.push UniqueType.parse(base.strip, subtype_string.strip) base.clear subtype_string.clear next end if point_stack.zero? && curly_stack.zero? && paren_stack.zero? base.concat char else subtype_string.concat char end end if point_stack != 0 || curly_stack != 0 || paren_stack != 0 raise ComplexTypeError, "Unclosed subtype in #{type_string}" end # types.push ComplexType.new([UniqueType.new(base, subtype_string)]) types.push UniqueType.parse(base.strip, subtype_string.strip) end unless key_types.nil? raise ComplexTypeError, 'Invalid use of key/value parameters' unless partial return key_types if types.empty? return [key_types, types] end result = partial ? types : ComplexType.new(types) @cache[strings] = result unless partial result end |
.try_parse(*strings) ⇒ ComplexType
546 547 548 549 550 551 |
# File 'lib/solargraph/complex_type.rb', line 546 def try_parse *strings parse(*strings) rescue ComplexTypeError => e Solargraph.logger.info "Error parsing complex type `#{strings.join(', ')}`: #{e.}" ComplexType::UNDEFINED end |
Instance Method Details
#[](index) ⇒ UniqueType
137 138 139 |
# File 'lib/solargraph/complex_type.rb', line 137 def [] index @items[index] end |
#all? {|| ... } ⇒ Boolean
280 281 282 |
# File 'lib/solargraph/complex_type.rb', line 280 def all? &block @items.all?(&block) end |
#all_params ⇒ Array<ComplexType>
346 347 348 |
# File 'lib/solargraph/complex_type.rb', line 346 def all_params @items.first.all_params || [] end |
#all_rooted? ⇒ Boolean
every type and subtype in this union have been resolved to be fully qualified
363 364 365 |
# File 'lib/solargraph/complex_type.rb', line 363 def all_rooted? all?(&:all_rooted?) end |
#any? {|| ... } ⇒ Boolean
287 288 289 |
# File 'lib/solargraph/complex_type.rb', line 287 def any? &block @items.compact.any?(&block) end |
#conforms_to?(api_map, expected, situation, rules = [], variance: erased_variance(situation)) ⇒ Boolean
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/solargraph/complex_type.rb', line 224 def conforms_to? api_map, expected, situation, rules = [], variance: erased_variance(situation) expected = expected.downcast_to_literal_if_possible inferred = downcast_to_literal_if_possible return duck_types_match?(api_map, expected, inferred, rules) if expected.duck_type? if rules.include? :allow_any_match inferred.any? do |inf| inf.conforms_to?(api_map, expected, situation, rules, variance: variance) end else inferred.all? do |inf| inf.conforms_to?(api_map, expected, situation, rules, variance: variance) end end end |
#desc ⇒ String
198 199 200 |
# File 'lib/solargraph/complex_type.rb', line 198 def desc end |
#downcast_to_literal_if_possible ⇒ ComplexType
192 193 194 195 |
# File 'lib/solargraph/complex_type.rb', line 192 def downcast_to_literal_if_possible return self ComplexType.new(items.map(&:downcast_to_literal_if_possible)) end |
#duck_types_match?(api_map, expected, inferred, rules = []) ⇒ Boolean
251 252 253 254 255 256 257 258 259 260 261 |
# File 'lib/solargraph/complex_type.rb', line 251 def duck_types_match? api_map, expected, inferred, rules = [] raise ArgumentError, 'Expected type must be duck type' unless expected.duck_type? allow_any_match = rules.include?(:allow_any_match) expected.each do |exp| next unless exp.duck_type? quack = exp.to_s[1..] || '' matched = allow_any_match ? inferred.any? { |inf| duck_type_provides?(api_map, inf, quack) } : inferred.all? { |inf| duck_type_provides?(api_map, inf, quack) } return false unless matched end true end |
#each {|| ... } ⇒ Enumerable<UniqueType>
94 95 96 |
# File 'lib/solargraph/complex_type.rb', line 94 def each &block @items.each(&block) end |
#each_unique_type ⇒ Enumerator<UniqueType>
This method returns an undefined value.
102 103 104 105 106 107 108 |
# File 'lib/solargraph/complex_type.rb', line 102 def each_unique_type &block return enum_for(__method__) unless block_given? @items.each do |item| item.each_unique_type(&block) end end |
#erased_version_of?(other) ⇒ Boolean
368 369 370 371 372 |
# File 'lib/solargraph/complex_type.rb', line 368 def erased_version_of? other return false if items.length != 1 || other.items.length != 1 @items.first.erased_version_of?(other.items.first) end |
#exclude(exclude_types, api_map) ⇒ ComplexType, self
385 386 387 388 389 390 391 |
# File 'lib/solargraph/complex_type.rb', line 385 def exclude exclude_types, api_map return self if exclude_types.nil? types = items - exclude_types.items types = [ComplexType::UniqueType::UNDEFINED] if types.empty? ComplexType.new(types) end |
#expand(named_types) ⇒ Object
315 316 317 |
# File 'lib/solargraph/complex_type.rb', line 315 def named_types ComplexType.new(map { |ut| ut.(named_types) }) end |
#first ⇒ UniqueType
61 62 63 |
# File 'lib/solargraph/complex_type.rb', line 61 def first @items.first end |
#force_rooted ⇒ self
320 321 322 323 324 |
# File 'lib/solargraph/complex_type.rb', line 320 def force_rooted transform do |t| t.recreate(make_rooted: true) end end |
#generic? ⇒ Boolean
295 296 297 |
# File 'lib/solargraph/complex_type.rb', line 295 def generic? any?(&:generic?) end |
#intersect_with(intersection_type, api_map) ⇒ self, ComplexType::UniqueType
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 |
# File 'lib/solargraph/complex_type.rb', line 398 def intersect_with intersection_type, api_map return self if intersection_type.nil? return intersection_type if undefined? types = [] # try to find common types via conformance items.each do |ut| intersection_type.each do |int_type| if int_type.conforms_to?(api_map, ut, :assignment) types << int_type elsif ut.conforms_to?(api_map, int_type, :assignment) types << ut end end end types = [ComplexType::UniqueType::UNDEFINED] if types.empty? ComplexType.new(types) end |
#length ⇒ Integer
126 127 128 |
# File 'lib/solargraph/complex_type.rb', line 126 def length @items.length end |
#literal? ⇒ Boolean
187 188 189 |
# File 'lib/solargraph/complex_type.rb', line 187 def literal? @items.any?(&:literal?) end |
#map {|| ... } ⇒ Array<UniqueType>
@sg-ignore Declared return type ::Array<::Solargraph::ComplexType::UniqueType> does not match inferred type ::Array<::Proc> for Solargraph::ComplexType#map
88 89 90 |
# File 'lib/solargraph/complex_type.rb', line 88 def map &block @items.map(&block) end |
#namespace ⇒ String
147 148 149 150 |
# File 'lib/solargraph/complex_type.rb', line 147 def namespace # cache this attr for high frequency call @namespace ||= method_missing(:namespace).to_s end |
#namespaces ⇒ Array<String>
153 154 155 |
# File 'lib/solargraph/complex_type.rb', line 153 def namespaces @items.map(&:namespace) end |
#nullable? ⇒ Boolean
334 335 336 |
# File 'lib/solargraph/complex_type.rb', line 334 def nullable? @items.any?(&:nil_type?) end |
#qualify(api_map, *gates) ⇒ ComplexType
36 37 38 39 40 41 42 43 44 |
# File 'lib/solargraph/complex_type.rb', line 36 def qualify api_map, *gates red = reduce_object types = red.items.map do |t| next t if %w[nil void undefined].include?(t.name) next t if ['::Boolean'].include?(t.rooted_name) api_map.unalias(t.name) || t.qualify(api_map, *gates) end ComplexType.new(types).reduce_object end |
#recreate(new_name: nil, make_rooted: nil, new_key_types: nil, new_subtypes: nil) ⇒ self
116 117 118 119 120 121 122 123 |
# File 'lib/solargraph/complex_type.rb', line 116 def recreate new_name: nil, make_rooted: nil, new_key_types: nil, new_subtypes: nil ComplexType.new(map do |ut| ut.recreate(new_name: new_name, make_rooted: make_rooted, new_key_types: new_key_types, new_subtypes: new_subtypes) end) end |
#reduce_class_type ⇒ ComplexType
351 352 353 354 355 356 357 358 359 |
# File 'lib/solargraph/complex_type.rb', line 351 def reduce_class_type new_items = items.flat_map do |type| next type unless %w[Module Class].include?(type.name) next type if type.all_params.empty? type.all_params end ComplexType.new(new_items) end |
#resolve_generics(definitions, context_type) ⇒ ComplexType
329 330 331 332 |
# File 'lib/solargraph/complex_type.rb', line 329 def resolve_generics definitions, context_type result = @items.map { |i| i.resolve_generics(definitions, context_type) } ComplexType.new(result) end |
#resolve_generics_from_context(generics_to_resolve, context_type, resolved_generic_values: {}) ⇒ self
50 51 52 53 54 55 56 57 58 |
# File 'lib/solargraph/complex_type.rb', line 50 def resolve_generics_from_context generics_to_resolve, context_type, resolved_generic_values: {} return self unless generic? ComplexType.new(@items.map do |unique_type| unique_type.resolve_generics_from_context(generics_to_resolve, destructure_context_type(unique_type, context_type), resolved_generic_values: resolved_generic_values) end) end |
#respond_to_missing?(name, include_private = false) ⇒ Boolean
169 170 171 |
# File 'lib/solargraph/complex_type.rb', line 169 def respond_to_missing? name, include_private = false TypeMethods.public_method_defined?(name) || super end |
#rooted? ⇒ Boolean
every top-level type has resolved to be fully qualified; see #all_rooted? to check their subtypes as well
376 377 378 |
# File 'lib/solargraph/complex_type.rb', line 376 def rooted? all?(&:rooted?) end |
#rooted_tags ⇒ String
275 276 277 |
# File 'lib/solargraph/complex_type.rb', line 275 def map(&:rooted_tag).join(', ') end |
#select(&block) ⇒ Array<UniqueType>
142 143 144 |
# File 'lib/solargraph/complex_type.rb', line 142 def select &block @items.select(&block) end |
#self_to_type(dst) ⇒ ComplexType
74 75 76 77 78 79 80 |
# File 'lib/solargraph/complex_type.rb', line 74 def self_to_type dst object_type_dst = dst.reduce_class_type transform do |t| next t if t.name != 'self' object_type_dst end end |
#selfy? ⇒ Boolean
291 292 293 |
# File 'lib/solargraph/complex_type.rb', line 291 def selfy? @items.any?(&:selfy?) end |
#simple_tags ⇒ String
183 184 185 |
# File 'lib/solargraph/complex_type.rb', line 183 def simplify_literals. end |
#simplify_literals ⇒ self
300 301 302 |
# File 'lib/solargraph/complex_type.rb', line 300 def simplify_literals ComplexType.new(map(&:simplify_literals)) end |
#tags ⇒ String
178 179 180 |
# File 'lib/solargraph/complex_type.rb', line 178 def map(&:tag).join(', ') end |
#to_a ⇒ Array<UniqueType>
131 132 133 |
# File 'lib/solargraph/complex_type.rb', line 131 def to_a @items end |
#to_rbs ⇒ String
66 67 68 69 70 |
# File 'lib/solargraph/complex_type.rb', line 66 def to_rbs ((@items.length > 1 ? '(' : '') + @items.map(&:to_rbs).join(' | ') + (@items.length > 1 ? ')' : '')) end |
#to_s ⇒ Object
173 174 175 |
# File 'lib/solargraph/complex_type.rb', line 173 def to_s map(&:tag).join(', ') end |
#transform(new_name = nil) {|t| ... } ⇒ ComplexType
308 309 310 311 312 313 |
# File 'lib/solargraph/complex_type.rb', line 308 def transform new_name = nil, &transform_type if new_name&.start_with?('::') raise "Please remove leading :: and set rooted with recreate() instead - #{new_name}" end ComplexType.new(map { |ut| ut.transform(new_name, &transform_type) }) end |
#without_nil ⇒ ComplexType
339 340 341 342 343 |
# File 'lib/solargraph/complex_type.rb', line 339 def without_nil new_items = @items.reject(&:nil_type?) return ComplexType::UNDEFINED if new_items.empty? ComplexType.new(new_items) end |