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()

Methods for accessing type data available from both ComplexType and UniqueType.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#nameString (readonly)

Returns:

  • (String)


23
24
25
# File 'lib/solargraph/complex_type/type_methods.rb', line 23

def name
  @name
end

#substringString (readonly)

Returns:

  • (String)


26
27
28
# File 'lib/solargraph/complex_type/type_methods.rb', line 26

def substring
  @substring
end

#subtypesArray<ComplexType> (readonly)

Returns:



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

def subtypes
  @subtypes
end

#tagString (readonly)

Returns:

  • (String)


29
30
31
# File 'lib/solargraph/complex_type/type_methods.rb', line 29

def tag
  @tag
end

Instance Method Details

#==(other) ⇒ Object

Parameters:

  • other (Object)


131
132
133
134
# File 'lib/solargraph/complex_type/type_methods.rb', line 131

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

#defined?Boolean

Returns:

  • (Boolean)


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

def defined?
  !undefined?
end

#duck_type?Boolean

Returns:

  • (Boolean)


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

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

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

Yield Parameters:

Returns:



173
174
175
176
# File 'lib/solargraph/complex_type/type_methods.rb', line 173

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)


63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/solargraph/complex_type/type_methods.rb', line 63

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)


83
84
85
# File 'lib/solargraph/complex_type/type_methods.rb', line 83

def fixed_parameters?
  substring.start_with?('(')
end

#hash_parameters?Boolean

Returns:

  • (Boolean)


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

def hash_parameters?
  substring.start_with?('{')
end

#key_typesArray<ComplexType>

Returns:



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

def key_types
  @key_types
end

#list_parameters?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/solargraph/complex_type/type_methods.rb', line 78

def list_parameters?
  substring.start_with?('<')
end

#namespaceString

Returns:

  • (String)


103
104
105
106
107
108
109
110
# File 'lib/solargraph/complex_type/type_methods.rb', line 103

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

#nil_type?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/solargraph/complex_type/type_methods.rb', line 40

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

#parameters?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/solargraph/complex_type/type_methods.rb', line 45

def parameters?
  !substring.empty?
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:



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/solargraph/complex_type/type_methods.rb', line 145

def qualify api_map, context = ''
  return self if name == GENERIC_TAG_NAME
  return ComplexType.new([self]) if duck_type? || void? || undefined?
  recon = (rooted? ? '' : context)
  fqns = api_map.qualify(name, recon)
  if fqns.nil?
    return UniqueType::BOOLEAN if tag == 'Boolean'
    return UniqueType::UNDEFINED
  end
  fqns = "::#{fqns}" # Ensure the resulting complex type is rooted
  all_ltypes = key_types.map { |t| t.qualify api_map, context }.uniq
  all_rtypes = value_types.map { |t| t.qualify api_map, context }
  if list_parameters?
    rtypes = all_rtypes.uniq
    Solargraph::ComplexType.parse("#{fqns}<#{rtypes.map(&:tag).join(', ')}>")
  elsif fixed_parameters?
    Solargraph::ComplexType.parse("#{fqns}(#{all_rtypes.map(&:tag).join(', ')})")
  elsif hash_parameters?
    ltypes = all_ltypes.uniq
    rtypes = all_rtypes.uniq
    Solargraph::ComplexType.parse("#{fqns}{#{ltypes.map(&:tag).join(', ')} => #{rtypes.map(&:tag).join(', ')}}")
  else
    Solargraph::ComplexType.parse(fqns)
  end
end

#rooted?Boolean

Returns:

  • (Boolean)


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

def rooted?
  @rooted
end

#rooted_nameString

Returns:

  • (String)


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

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

#rooted_namespaceString

Returns:

  • (String)


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

def rooted_namespace
  return namespace unless rooted?
  "::#{namespace}"
end

#scope::Symbol

Returns :class or :instance.

Returns:

  • (::Symbol)

    :class or :instance



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

def scope
  @scope ||= :instance if duck_type? || nil_type?
  @scope ||= (name == 'Class' || name == 'Module') && !subtypes.empty? ? :class : :instance
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 16

#undefined?Boolean

Returns:

  • (Boolean)


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

def undefined?
  name == 'undefined'
end

#value_typesArray<ComplexType>

Returns:



93
94
95
# File 'lib/solargraph/complex_type/type_methods.rb', line 93

def value_types
  @subtypes
end

#void?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/solargraph/complex_type/type_methods.rb', line 49

def void?
  name == 'void'
end