Module: Solargraph::ComplexType::TypeMethods Abstract
- Included in:
- UniqueType
- Defined in:
- lib/solargraph/complex_type/type_methods.rb
Overview
This module is abstract.
This mixin relies on these - instance variables:
@name: String
@subtypes: Array<ComplexType>
@rooted: boolish
methods:
transform()
all_params()
rooted?()
can_root_name?()
Methods for accessing type data available from both ComplexType and UniqueType.
Constant Summary collapse
- PARAMETERS_TYPE_BY_STARTING_TAG =
{ '{' => :hash, '(' => :fixed, '<' => :list }.freeze
Instance Attribute Summary collapse
- #name ⇒ String readonly
- #parameters_type ⇒ Symbol? readonly
- #subtypes ⇒ Array<ComplexType> readonly
Instance Method Summary collapse
- #==(other) ⇒ Object
- #all_params ⇒ Array<ComplexType>
- #can_root_name?(name_to_check = nil) ⇒ Object
- #defined? ⇒ Boolean
- #duck_type? ⇒ Boolean
- #each_unique_type ⇒ Enumerator<UniqueType>
- #erase_generics(generics_to_erase) ⇒ self
-
#erased_variance(situation = :method_call) ⇒ Symbol
Variance of the type ignoring any type parameters.
- #fixed_parameters? ⇒ Boolean
- #generate_substring_from(&to_str) ⇒ String
- #hash_parameters? ⇒ Boolean
-
#interface? ⇒ Boolean
Whether this is an RBS interface like _ToAry or Hash::_Key.
- #key_types ⇒ Array<ComplexType>
- #list_parameters? ⇒ Boolean
- #namespace ⇒ String
- #namespace_type ⇒ self
- #nil_type? ⇒ Boolean
-
#qualify(api_map, context = '') ⇒ self, ...
Generate a ComplexType that fully qualifies this type’s namespaces.
- #rooted? ⇒ Object
- #rooted_name ⇒ String
- #rooted_namespace ⇒ String
- #rooted_substring ⇒ String
- #rooted_tag ⇒ String
-
#scope ⇒ ::Symbol
:class or :instance.
- #substring ⇒ String
- #tag ⇒ String
- #transform(new_name = nil) {|t| ... } ⇒ UniqueType?
- #tuple? ⇒ Boolean
- #undefined? ⇒ Boolean
- #value_types ⇒ Array<ComplexType>
- #void? ⇒ Boolean
Instance Attribute Details
#name ⇒ String (readonly)
31 32 33 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 31 def name @name end |
#parameters_type ⇒ Symbol? (readonly)
107 108 109 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 107 def parameters_type @parameters_type end |
#subtypes ⇒ Array<ComplexType> (readonly)
34 35 36 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 34 def subtypes @subtypes end |
Instance Method Details
#==(other) ⇒ Object
206 207 208 209 210 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 206 def == other return false unless self.class == other.class # @sg-ignore flow sensitive typing should support .class == .class tag == other.tag end |
#can_root_name?(name_to_check = nil) ⇒ Object
|
|
# File 'lib/solargraph/complex_type/type_methods.rb', line 19
|
#defined? ⇒ Boolean
70 71 72 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 70 def defined? !undefined? end |
#duck_type? ⇒ Boolean
52 53 54 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 52 def duck_type? @duck_type ||= name.start_with?('#') end |
#each_unique_type ⇒ Enumerator<UniqueType>
This method returns an undefined value.
235 236 237 238 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 235 def each_unique_type &block return enum_for(__method__) unless block_given? yield self end |
#erase_generics(generics_to_erase) ⇒ self
92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 92 def erase_generics generics_to_erase transform do |type| if type.name == ComplexType::GENERIC_TAG_NAME if type.all_params.length == 1 && generics_to_erase.include?(type.all_params.first.to_s) ComplexType::UNDEFINED else type end else type end end end |
#erased_variance(situation = :method_call) ⇒ Symbol
Variance of the type ignoring any type parameters
81 82 83 84 85 86 87 88 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 81 def erased_variance situation = :method_call # :nocov: unless %i[method_call return_type assignment].include?(situation) raise "Unknown situation: #{situation.inspect}" end # :nocov: :covariant end |
#fixed_parameters? ⇒ Boolean
122 123 124 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 122 def fixed_parameters? parameters_type == :fixed end |
#generate_substring_from(&to_str) ⇒ String
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 182 def generate_substring_from &to_str key_types_str = key_types.map(&to_str).join(', ') subtypes_str = subtypes.map(&to_str).join(', ') if (key_types.none?(&:defined?) && subtypes.none?(&:defined?)) || (key_types.empty? && subtypes.empty?) '' elsif hash_parameters? "{#{key_types_str} => #{subtypes_str}}" elsif fixed_parameters? "(#{subtypes_str})" elsif name == 'Hash' "<#{key_types_str}, #{subtypes_str}>" else "<#{key_types_str}#{subtypes_str}>" end end |
#hash_parameters? ⇒ Boolean
127 128 129 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 127 def hash_parameters? parameters_type == :hash end |
#interface? ⇒ Boolean
Whether this is an RBS interface like _ToAry or Hash::_Key.
47 48 49 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 47 def interface? name.start_with?('_') || name.include?('::_') end |
#key_types ⇒ Array<ComplexType>
137 138 139 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 137 def key_types @key_types end |
#list_parameters? ⇒ Boolean
117 118 119 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 117 def list_parameters? parameters_type == :list end |
#namespace ⇒ String
142 143 144 145 146 147 148 149 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 142 def namespace # if priority higher than ||=, old implements cause unnecessary check @namespace ||= lambda do return 'Object' if duck_type? return 'NilClass' if nil_type? %w[Class Module].include?(name) && !subtypes.empty? ? subtypes.first.name : name end.call end |
#namespace_type ⇒ self
152 153 154 155 156 157 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 152 def namespace_type return ComplexType.parse('::Object') if duck_type? return ComplexType.parse('::NilClass') if nil_type? return subtypes.first if %w[Class Module].include?(name) && !subtypes.empty? self end |
#nil_type? ⇒ Boolean
57 58 59 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 57 def nil_type? @nil_type ||= name.casecmp('nil').zero? end |
#qualify(api_map, context = '') ⇒ self, ...
Generate a ComplexType that fully qualifies this type’s namespaces.
217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 217 def qualify api_map, context = '' transform do |t| next t if t.name == GENERIC_TAG_NAME next t if t.duck_type? || t.void? || t.undefined? recon = (t.rooted? ? '' : context) fqns = api_map.qualify(t.name, recon) if fqns.nil? next UniqueType::BOOLEAN if t.tag == 'Boolean' next UniqueType::UNDEFINED end t.recreate(new_name: fqns, make_rooted: true) end end |
#rooted? ⇒ Object
|
|
# File 'lib/solargraph/complex_type/type_methods.rb', line 19
|
#rooted_name ⇒ String
166 167 168 169 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 166 def rooted_name return name unless @rooted && can_root_name? "::#{name}" end |
#rooted_namespace ⇒ String
160 161 162 163 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 160 def rooted_namespace return namespace unless rooted? && can_root_name?(namespace) "::#{namespace}" end |
#rooted_substring ⇒ String
177 178 179 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 177 def rooted_substring @rooted_substring = generate_substring_from(&:rooted_tags) end |
#rooted_tag ⇒ String
42 43 44 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 42 def rooted_tag @rooted_tag ||= rooted_name + rooted_substring end |
#scope ⇒ ::Symbol
Returns :class or :instance.
200 201 202 203 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 200 def scope @scope ||= :instance if duck_type? || nil_type? @scope ||= %w[Class Module].include?(name) && !subtypes.empty? ? :class : :instance end |
#substring ⇒ String
172 173 174 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 172 def substring @substring ||= generate_substring_from(&:tags) end |
#tag ⇒ String
37 38 39 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 37 def tag @tag ||= "#{name}#{substring}" end |
#transform(new_name = nil) {|t| ... } ⇒ UniqueType?
|
|
# File 'lib/solargraph/complex_type/type_methods.rb', line 19
|
#tuple? ⇒ Boolean
61 62 63 64 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 61 def tuple? return false @tuple ||= (name == 'Tuple') || (name == 'Array' && subtypes.length >= 1 && fixed_parameters?) end |
#undefined? ⇒ Boolean
74 75 76 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 74 def undefined? name == 'undefined' end |
#value_types ⇒ Array<ComplexType>
132 133 134 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 132 def value_types @subtypes end |
#void? ⇒ Boolean
66 67 68 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 66 def void? name == 'void' end |