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

Instance Method Summary collapse

Instance Attribute Details

#nameString (readonly)

Returns:

  • (String)


31
32
33
# File 'lib/solargraph/complex_type/type_methods.rb', line 31

def name
  @name
end

#parameters_typeSymbol? (readonly)

Returns:

  • (Symbol, nil)


89
90
91
# File 'lib/solargraph/complex_type/type_methods.rb', line 89

def parameters_type
  @parameters_type
end

#subtypesArray<ComplexType> (readonly)

Returns:



34
35
36
# File 'lib/solargraph/complex_type/type_methods.rb', line 34

def subtypes
  @subtypes
end

Instance Method Details

#==(other) ⇒ Object

Parameters:

  • other (Object)


191
192
193
194
# File 'lib/solargraph/complex_type/type_methods.rb', line 191

def == other
  return false unless self.class == other.class
  tag == other.tag
end

#all_paramsArray<ComplexType>

Returns:



# File 'lib/solargraph/complex_type/type_methods.rb', line 19

#can_root_name?(name_to_check = nil) ⇒ Object

Parameters:

  • name_to_check (String, nil) (defaults to: nil)


# File 'lib/solargraph/complex_type/type_methods.rb', line 19

#defined?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/solargraph/complex_type/type_methods.rb', line 64

def defined?
  !undefined?
end

#duck_type?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/solargraph/complex_type/type_methods.rb', line 47

def duck_type?
  @duck_type ||= name.start_with?('#')
end

#each_unique_type {|| ... } ⇒ Enumerator<UniqueType>

Yield Parameters:

Returns:



217
218
219
220
# File 'lib/solargraph/complex_type/type_methods.rb', line 217

def each_unique_type &block
  return enum_for(__method__) unless block_given?
  yield self
end

#erase_generics(generics_to_erase) ⇒ self

Parameters:

  • generics_to_erase (Enumerable<String>)

Returns:

  • (self)


74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/solargraph/complex_type/type_methods.rb', line 74

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

#fixed_parameters?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/solargraph/complex_type/type_methods.rb', line 104

def fixed_parameters?
  parameters_type == :fixed
end

#generate_substring_from(&to_str) ⇒ String

Returns:

  • (String)


164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/solargraph/complex_type/type_methods.rb', line 164

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?)
    ''
  elsif key_types.empty? && subtypes.empty?
    ''
  elsif hash_parameters?
    "{#{key_types_str} => #{subtypes_str}}"
  elsif fixed_parameters?
    "(#{subtypes_str})"
  else
    if name == 'Hash'
      "<#{key_types_str}, #{subtypes_str}>"
    else
      "<#{key_types_str}#{subtypes_str}>"
    end
  end
end

#hash_parameters?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/solargraph/complex_type/type_methods.rb', line 109

def hash_parameters?
  parameters_type == :hash
end

#key_typesArray<ComplexType>

Returns:



119
120
121
# File 'lib/solargraph/complex_type/type_methods.rb', line 119

def key_types
  @key_types
end

#list_parameters?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/solargraph/complex_type/type_methods.rb', line 99

def list_parameters?
  parameters_type == :list
end

#namespaceString

Returns:

  • (String)


124
125
126
127
128
129
130
131
# File 'lib/solargraph/complex_type/type_methods.rb', line 124

def namespace
  # if priority higher than ||=, old implements cause unnecessary check
  @namespace ||= lambda do
    return 'Object' if duck_type?
    return 'NilClass' if nil_type?
    return (name == 'Class' || name == 'Module') && !subtypes.empty? ? subtypes.first.name : name
  end.call
end

#namespace_typeself

Returns:

  • (self)


134
135
136
137
138
139
# File 'lib/solargraph/complex_type/type_methods.rb', line 134

def namespace_type
  return ComplexType.parse('::Object') if duck_type?
  return ComplexType.parse('::NilClass') if nil_type?
  return subtypes.first if (name == 'Class' || name == 'Module') && !subtypes.empty?
  self
end

#nil_type?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/solargraph/complex_type/type_methods.rb', line 52

def nil_type?
  @nil_type ||= (name.casecmp('nil') == 0)
end

#qualify(api_map, context = '') ⇒ self, ...

Generate a ComplexType that fully qualifies this type’s namespaces.

Parameters:

  • api_map (ApiMap)

    The ApiMap that performs qualification

  • context (String) (defaults to: '')

    The namespace from which to resolve names

Returns:



201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/solargraph/complex_type/type_methods.rb', line 201

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_nameString

Returns:

  • (String)


148
149
150
151
# File 'lib/solargraph/complex_type/type_methods.rb', line 148

def rooted_name
  return name unless @rooted && can_root_name?
  "::#{name}"
end

#rooted_namespaceString

Returns:

  • (String)


142
143
144
145
# File 'lib/solargraph/complex_type/type_methods.rb', line 142

def rooted_namespace
  return namespace unless rooted? && can_root_name?(namespace)
  "::#{namespace}"
end

#rooted_substringString

Returns:

  • (String)


159
160
161
# File 'lib/solargraph/complex_type/type_methods.rb', line 159

def rooted_substring
  @rooted_substring = generate_substring_from(&:rooted_tags)
end

#rooted_tagString

Returns:

  • (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.

Returns:

  • (::Symbol)

    :class or :instance



185
186
187
188
# File 'lib/solargraph/complex_type/type_methods.rb', line 185

def scope
  @scope ||= :instance if duck_type? || nil_type?
  @scope ||= (name == 'Class' || name == 'Module') && !subtypes.empty? ? :class : :instance
end

#substringString

Returns:

  • (String)


154
155
156
# File 'lib/solargraph/complex_type/type_methods.rb', line 154

def substring
  @substring ||= generate_substring_from(&:tags)
end

#tagString

Returns:

  • (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?

Parameters:

  • new_name (String, nil) (defaults to: nil)

Yield Parameters:

Yield Returns:

Returns:



# File 'lib/solargraph/complex_type/type_methods.rb', line 19

#tuple?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/solargraph/complex_type/type_methods.rb', line 56

def tuple?
  @tuple_type ||= (name == 'Tuple') || (name == 'Array' && subtypes.length >= 1 && fixed_parameters?)
end

#undefined?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/solargraph/complex_type/type_methods.rb', line 68

def undefined?
  name == 'undefined'
end

#value_typesArray<ComplexType>

Returns:



114
115
116
# File 'lib/solargraph/complex_type/type_methods.rb', line 114

def value_types
  @subtypes
end

#void?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/solargraph/complex_type/type_methods.rb', line 60

def void?
  name == 'void'
end