Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/solargraph.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class InvalidRubocopVersionError < RuntimeError; end
autoload :RbsMap, 'solargraph/rbs_map'
autoload :GemPins, 'solargraph/gem_pins'
autoload :PinCache, 'solargraph/pin_cache'
autoload :Type, 'solargraph/type'

dir = File.dirname(__FILE__)
VIEWS_PATH = File.join(dir, 'solargraph', 'views')
Expand Down
29 changes: 21 additions & 8 deletions lib/solargraph/complex_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@
module Solargraph
# A container for type data based on YARD type tags.
#
class ComplexType
class ComplexType < Type
GENERIC_TAG_NAME = 'generic'

# @!parse
# include TypeMethods
include Equality

autoload :Conformance, 'solargraph/complex_type/conformance'
autoload :TypeMethods, 'solargraph/complex_type/type_methods'
autoload :UniqueType, 'solargraph/complex_type/unique_type'

# @param types [Array<UniqueType, ComplexType>]
def initialize types = [UniqueType::UNDEFINED]
super()

# @todo @items here should not need an annotation
# @type [Array<UniqueType>]
items = types.flat_map(&:items).uniq(&:to_s)
Expand Down Expand Up @@ -44,7 +46,7 @@ def qualify api_map, *gates
end

# @param generics_to_resolve [Enumerable<String>]]
# @param context_type [ComplexType, ComplexType::UniqueType, nil]
# @param context_type [Type, nil]
# @param resolved_generic_values [Hash{String => ComplexType}] Added to as types are encountered or resolved
# @return [self]
def resolve_generics_from_context generics_to_resolve, context_type, resolved_generic_values: {}
Expand All @@ -68,7 +70,7 @@ def to_rbs
(@items.length > 1 ? ')' : ''))
end

# @param dst [ComplexType, ComplexType::UniqueType]
# @param dst [Type]
# @return [ComplexType]
def self_to_type dst
object_type_dst = dst.reduce_class_type
Expand Down Expand Up @@ -159,14 +161,25 @@ def namespaces
# @param [Array<Object>] args
def method_missing name, *args, &block
return if @items.first.nil?
return @items.first.send(name, *args, &block) if respond_to_missing?(name)
if @items.first.respond_to?(name)
if @items.count > 1
Solargraph.assert_or_log(:complex_type_method_missing,
"ComplexType being used as UniqueType: delegating #{name} to #{self.class} with items #{@items.map(&:to_s).join(', ')}")
end
return @items.first.send(name, *args, &block)
end
super
end

def undefined?
@items.all?(&:undefined?)
end

# @param name [Symbol]
# @param include_private [Boolean]
def respond_to_missing? name, include_private = false
TypeMethods.public_instance_methods.include?(name) || super
return false if @items.first.nil?
@items.first.respond_to?(name) || super
end

def to_s
Expand Down Expand Up @@ -198,7 +211,7 @@ def desc
end

# @param api_map [ApiMap]
# @param expected [ComplexType, ComplexType::UniqueType]
# @param expected [Type]
# @param situation [:method_call, :return_type, :assignment]
# @param rules [Array<:allow_subtype_skew, :allow_empty_params, :allow_reverse_match, :allow_any_match, :allow_undefined, :allow_unresolved_generic, :allow_unmatched_interface>]
#
Expand Down Expand Up @@ -373,7 +386,7 @@ def exclude exclude_types, api_map

# @see https://en.wikipedia.org/wiki/Intersection_type
#
# @param intersection_type [ComplexType, ComplexType::UniqueType, nil]
# @param intersection_type [Type, nil]
# @param api_map [ApiMap]
# @return [self, ComplexType::UniqueType]
def intersect_with intersection_type, api_map
Expand Down
239 changes: 0 additions & 239 deletions lib/solargraph/complex_type/type_methods.rb

This file was deleted.

20 changes: 15 additions & 5 deletions lib/solargraph/complex_type/unique_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ class ComplexType
# An individual type signature. A complex type can consist of multiple
# unique types.
#
class UniqueType
include TypeMethods
class UniqueType < Type
include Equality

attr_reader :all_params, :subtypes, :key_types
attr_reader :all_params, :subtypes, :key_types, :name

# Create a UniqueType with the specified name and an optional substring.
# The substring is the parameter section of a parametrized type, e.g.,
Expand Down Expand Up @@ -70,6 +69,8 @@ def self.parse name, substring = '', make_rooted: nil
# @param rooted [Boolean]
# @param parameters_type [Symbol, nil]
def initialize name, key_types = [], subtypes = [], rooted:, parameters_type: nil
super()

if parameters_type.nil? && !(key_types.empty? && subtypes.empty?)
raise 'You must supply parameters_type if you provide parameters'
end
Expand Down Expand Up @@ -100,6 +101,11 @@ def to_s
tag
end

# @return [Array<ComplexType>]
def value_types
@subtypes
end

# @return [self]
def simplify_literals
transform do |t|
Expand All @@ -121,8 +127,9 @@ def exclude exclude_types, api_map

# @see https://en.wikipedia.org/wiki/Intersection_type
#
# @param intersection_type [ComplexType, ComplexType::UniqueType, nil]
# @param intersection_type [Type, nil]
# @param api_map [ApiMap]
#
# @return [self, ComplexType]
def intersect_with intersection_type, api_map
return self if intersection_type.nil?
Expand Down Expand Up @@ -197,6 +204,9 @@ def eql? other
@parameters_type == other.parameters_type
end

# @return [Symbol, nil]
attr_reader :parameters_type

def == other
eql?(other)
end
Expand Down Expand Up @@ -380,7 +390,7 @@ def downcast_to_literal_if_possible

# @param generics_to_resolve [Enumerable<String>]
# @param context_type [ComplexType, UniqueType, nil]
# @param resolved_generic_values [Hash{String => ComplexType, ComplexType::UniqueType}] Added to as types are encountered or resolved
# @param resolved_generic_values [Hash{String => Type}] Added to as types are encountered or resolved
# @return [UniqueType, ComplexType]
def resolve_generics_from_context generics_to_resolve, context_type, resolved_generic_values: {}
if name == ComplexType::GENERIC_TAG_NAME
Expand Down
Loading
Loading