Class: Solargraph::ComplexType::UniqueType
- Inherits:
-
Object
- Object
- Solargraph::ComplexType::UniqueType
show all
- Includes:
- TypeMethods
- Defined in:
- lib/solargraph/complex_type/unique_type.rb
Overview
An individual type signature. A complex type can consist of multiple unique types.
Constant Summary
collapse
- UNDEFINED =
UniqueType.new('undefined')
- BOOLEAN =
UniqueType.new('Boolean')
Instance Attribute Summary collapse
Attributes included from TypeMethods
#name, #substring, #subtypes, #tag
Instance Method Summary
collapse
#==, #defined?, #duck_type?, #each_unique_type, #fixed_parameters?, #hash_parameters?, #key_types, #list_parameters?, #namespace, #nil_type?, #parameters?, #qualify, #rooted?, #scope, #undefined?, #value_types, #void?
Constructor Details
#initialize(name, substring = '') ⇒ UniqueType
Create a UniqueType with the specified name and an optional substring. The substring is the parameter section of a parametrized type, e.g., for the type ‘Array<String>`, the name is `Array` and the substring is `<String>`.
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
# File 'lib/solargraph/complex_type/unique_type.rb', line 20
def initialize name, substring = ''
if name.start_with?('::')
@name = name[2..-1]
@rooted = true
else
@name = name
@rooted = false
end
@substring = substring
@tag = @name + substring
@key_types = []
@subtypes = []
@all_params = []
return unless parameters?
if @substring.start_with?('<(') && @substring.end_with?(')>')
subs = ComplexType.parse(substring[2..-3], partial: true)
else
subs = ComplexType.parse(substring[1..-2], partial: true)
end
if hash_parameters?
raise ComplexTypeError, "Bad hash type" unless !subs.is_a?(ComplexType) and subs.length == 2 and !subs[0].is_a?(UniqueType) and !subs[1].is_a?(UniqueType)
@key_types.concat subs[0].map { |u| ComplexType.new([u]) }
@subtypes.concat subs[1].map { |u| ComplexType.new([u]) }
else
@subtypes.concat subs
end
@all_params.concat @key_types
@all_params.concat @subtypes
end
|
Instance Attribute Details
#all_params ⇒ Object
Returns the value of attribute all_params.
11
12
13
|
# File 'lib/solargraph/complex_type/unique_type.rb', line 11
def all_params
@all_params
end
|
Instance Method Details
#generic? ⇒ Boolean
62
63
64
|
# File 'lib/solargraph/complex_type/unique_type.rb', line 62
def generic?
name == GENERIC_TAG_NAME || all_params.any?(&:generic?)
end
|
#items ⇒ Object
54
55
56
|
# File 'lib/solargraph/complex_type/unique_type.rb', line 54
def items
[self]
end
|
#resolve_generics(definitions, context_type) ⇒ UniqueType
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
# File 'lib/solargraph/complex_type/unique_type.rb', line 69
def resolve_generics definitions, context_type
new_name = if name == GENERIC_TAG_NAME
idx = definitions.generics.index(subtypes.first&.name)
return ComplexType::UNDEFINED if idx.nil?
param_type = context_type.all_params[idx]
return ComplexType::UNDEFINED unless param_type
param_type.to_s
else
name
end
new_key_types = if name != GENERIC_TAG_NAME
@key_types.map { |t| t.resolve_generics(definitions, context_type) }.select(&:defined?)
else
[]
end
new_subtypes = if name != GENERIC_TAG_NAME
@subtypes.map { |t| t.resolve_generics(definitions, context_type) }.select(&:defined?)
else
[]
end
if name != GENERIC_TAG_NAME && !(new_key_types.empty? && new_subtypes.empty?)
if hash_parameters?
UniqueType.new(new_name, "{#{new_key_types.join(', ')} => #{new_subtypes.join(', ')}}")
elsif parameters?
if @substring.start_with?'<('
UniqueType.new(new_name, "<(#{new_subtypes.join(', ')})>")
else
UniqueType.new(new_name, "<#{new_subtypes.join(', ')}>")
end
else
UniqueType.new(new_name)
end
else
UniqueType.new(new_name)
end
end
|
#self_to(dst) ⇒ Object
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
# File 'lib/solargraph/complex_type/unique_type.rb', line 113
def self_to dst
return self unless selfy?
new_name = (@name == 'self' ? dst : @name)
new_key_types = @key_types.map { |t| t.self_to dst }
new_subtypes = @subtypes.map { |t| t.self_to dst }
if hash_parameters?
UniqueType.new(new_name, "{#{new_key_types.join(', ')} => #{new_subtypes.join(', ')}}")
elsif parameters?
if @substring.start_with?'<('
UniqueType.new(new_name, "<(#{new_subtypes.join(', ')})>")
else
UniqueType.new(new_name, "<#{new_subtypes.join(', ')}>")
end
else
UniqueType.new(new_name)
end
end
|
#selfy? ⇒ Boolean
131
132
133
|
# File 'lib/solargraph/complex_type/unique_type.rb', line 131
def selfy?
@name == 'self' || @key_types.any?(&:selfy?) || @subtypes.any?(&:selfy?)
end
|
#to_rbs ⇒ Object
58
59
60
|
# File 'lib/solargraph/complex_type/unique_type.rb', line 58
def to_rbs
"#{namespace}#{parameters? ? "[#{subtypes.map { |s| s.to_rbs }.join(', ')}]" : ''}"
end
|
#to_s ⇒ Object
50
51
52
|
# File 'lib/solargraph/complex_type/unique_type.rb', line 50
def to_s
tag
end
|