Class: Solargraph::Pin::Parameter

Inherits:
LocalVariable show all
Defined in:
lib/solargraph/pin/parameter.rb

Constant Summary

Constants included from Logging

Logging::DEFAULT_LOG_LEVEL, Logging::LOG_LEVELS

Instance Attribute Summary collapse

Attributes inherited from BaseVariable

#assignments, #mass_assignment, #presence

Attributes inherited from Base

#code_object, #combine_priority, #directives, #docstring, #name, #path, #source

Attributes included from Common

#context

Instance Method Summary collapse

Methods inherited from LocalVariable

#probe

Methods inherited from BaseVariable

#==, #assignment, #combine_assignments, #combine_closure, #combine_mass_assignment, #combine_presence, #completion_item_kind, #downcast, #inner_desc, #nil_assignment?, #presence_certain?, #probe, #return_types_from_node, #starts_at?, #symbol_kind, #type_desc, #variable?, #visible_at?

Methods inherited from Base

#==, #all_location_text, #all_rooted?, #assert_location_provided, #assert_same, #assert_same_array_content, #assert_same_count, #assert_same_macros, #assert_source_provided, #best_location, #choose, #choose_longer, #choose_node, #choose_pin_attr, #choose_pin_attr_with_same_name, #choose_priority, #closure, #combine_closure, #combine_directives, #combine_name, #comments, #completion_item_kind, #deprecated?, #desc, #dodgy_return_type_source?, #erase_generics, #filename, #gates, #identity, #infer, #inner_desc, #inspect, #macros, #maybe_directives?, #nearly?, #prefer_rbs_location, #presence_certain?, #probe, #probed?, #proxied?, #proxy, #rbs_location?, #realize, #resolve_generics, #resolve_generics_from_context, #symbol_kind, #to_s, #transform_types, #type_desc, #variable?

Methods included from Logging

log_level, logger

Methods included from Documenting

normalize_indentation, strip_html_comments

Methods included from Conversions

#completion_item, #completion_item_kind, #deprecated?, #detail, #link_documentation, #probed?, #proxied?, #reset_conversions, #resolve_completion_item, #signature_help, #text_documentation

Methods included from Common

#binder, #closure, #comments, #name, #namespace, #path, #source

Constructor Details

#initialize(decl: :arg, asgn_code: nil, **splat) ⇒ Parameter

Returns a new instance of Parameter.

Parameters:

  • decl (::Symbol) (defaults to: :arg)

    :arg, :optarg, :kwarg, :kwoptarg, :restarg, :kwrestarg, :block, :blockarg

  • asgn_code (String, nil) (defaults to: nil)
  • splat (Hash{Symbol => Object})


19
20
21
22
23
# File 'lib/solargraph/pin/parameter.rb', line 19

def initialize decl: :arg, asgn_code: nil, **splat
  super(**splat)
  @asgn_code = asgn_code
  @decl = decl
end

Instance Attribute Details

#asgn_codeString (readonly)

Returns:

  • (String)


10
11
12
# File 'lib/solargraph/pin/parameter.rb', line 10

def asgn_code
  @asgn_code
end

#closure=(value) ⇒ Object (writeonly)

allow this to be set to the method after the method itself has been created



14
15
16
# File 'lib/solargraph/pin/parameter.rb', line 14

def closure=(value)
  @closure = value
end

#decl::Symbol (readonly)

Returns:

  • (::Symbol)


7
8
9
# File 'lib/solargraph/pin/parameter.rb', line 7

def decl
  @decl
end

Instance Method Details

#arg?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/solargraph/pin/parameter.rb', line 101

def arg?
  decl == :arg
end

#arity_declString

Returns:

  • (String)


75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/solargraph/pin/parameter.rb', line 75

def arity_decl
  name = self.name || '(anon)'
  return_type&.to_rbs || 'untyped'
  case decl
  when :arg
    ''
  when :optarg
    '?'
  when :kwarg
    "#{name}:"
  when :kwoptarg
    "?#{name}:"
  when :restarg
    '*'
  when :kwrestarg
    '**'
  else
    "(unknown decl: #{decl})"
  end
end

#block?Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/solargraph/pin/parameter.rb', line 121

def block?
  %i[block blockarg].include?(decl)
end

#combine_return_type(other) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/solargraph/pin/parameter.rb', line 51

def combine_return_type other
  out = super
  if out&.undefined?
    # allow our return_type method to provide a better type
    # using :param tag
    out = nil
  end
  out
end

#combine_with(other, attrs = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/solargraph/pin/parameter.rb', line 33

def combine_with other, attrs = {}
  # Parameters can only be combined with local variables in the same closure
  return self unless other.closure == closure

  new_attrs = if other.is_a?(Parameter)
                {
                  decl: assert_same(other, :decl),
                  asgn_code: choose(other, :asgn_code)
                }
              else
                {
                  decl: decl,
                  asgn_code: asgn_code
                }
              end
  super(other, new_attrs.merge(attrs))
end

#compatible_arg?(atype, api_map) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)


224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/solargraph/pin/parameter.rb', line 224

def compatible_arg? atype, api_map
  # make sure we get types from up the method
  # inheritance chain if we don't have them on this pin
  ptype = typify api_map
  return true if ptype.undefined?

  return true if atype.conforms_to?(api_map,
                                    ptype,
                                    :method_call,
                                    %i[allow_empty_params allow_undefined])
  ptype.generic?
end

#documentationObject

@sg-ignore flow sensitive typing needs to handle attrs



238
239
240
241
242
# File 'lib/solargraph/pin/parameter.rb', line 238

def documentation
  tag = param_tag
  return '' if tag.nil? || tag.text.nil?
  tag.text
end

#fullString

Returns:

  • (String)


166
167
168
169
170
171
172
173
174
175
# File 'lib/solargraph/pin/parameter.rb', line 166

def full
  full_name + case decl
              when :optarg
                " = #{asgn_code || '?'}"
              when :kwoptarg
                " #{asgn_code || '?'}"
              else
                ''
              end
end

#full_nameString

Returns the full name of the parameter, including any declarative symbols such as ‘*` or `:` indicating type of parameter. This is used in method signatures.

Returns:

  • (String)

    the full name of the parameter, including any declarative symbols such as ‘*` or `:` indicating type of parameter. This is used in method signatures.



145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/solargraph/pin/parameter.rb', line 145

def full_name
  case decl
  when :kwarg, :kwoptarg
    "#{name}:"
  when :restarg
    "*#{name}"
  when :kwrestarg
    "**#{name}"
  when :block, :blockarg
    "&#{name}"
  else
    name
  end
end

#indexInteger

The parameter’s zero-based location in the block’s signature.

@sg-ignore Need to add nil check here

Returns:

  • (Integer)


203
204
205
206
207
# File 'lib/solargraph/pin/parameter.rb', line 203

def index
  method_pin = closure
  # @sg-ignore Need to add nil check here
  method_pin.parameter_names.index(name)
end

#keyword?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/solargraph/pin/parameter.rb', line 61

def keyword?
  %i[kwarg kwoptarg].include?(decl)
end

#kwrestarg?Boolean

Returns:

  • (Boolean)


65
66
67
68
# File 'lib/solargraph/pin/parameter.rb', line 65

def kwrestarg?
  # @sg-ignore flow sensitive typing needs to handle attrs
  decl == :kwrestarg || (assignment && %i[HASH hash].include?(assignment.type))
end

#locationObject



29
30
31
# File 'lib/solargraph/pin/parameter.rb', line 29

def location
  super || closure&.type_location
end

#mandatory_positional?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/solargraph/pin/parameter.rb', line 109

def mandatory_positional?
  decl == :arg
end

#needs_consistent_name?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/solargraph/pin/parameter.rb', line 70

def needs_consistent_name?
  keyword?
end

#positional?Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/solargraph/pin/parameter.rb', line 113

def positional?
  !keyword?
end

#reset_generated!Object



160
161
162
163
# File 'lib/solargraph/pin/parameter.rb', line 160

def reset_generated!
  @return_type = nil if param_tag
  super
end

#rest?Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/solargraph/pin/parameter.rb', line 117

def rest?
  %i[restarg kwrestarg].include?(decl)
end

#restarg?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/solargraph/pin/parameter.rb', line 105

def restarg?
  decl == :restarg
end

#return_typeComplexType

@sg-ignore super always sets @return_type to something

Returns:



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/solargraph/pin/parameter.rb', line 179

def return_type
  if @return_type.nil?
    @return_type = ComplexType::UNDEFINED
    found = param_tag
    @return_type = ComplexType.try_parse(*found.types) unless found.nil? || found.types.nil?
    # @sg-ignore flow sensitive typing should be able to handle redefinition
    if @return_type.undefined?
      case decl
      when :restarg
        @return_type = ComplexType.try_parse('::Array')
      when :kwrestarg
        @return_type = ComplexType.try_parse('::Hash')
      when :blockarg
        @return_type = ComplexType.try_parse('::Proc')
      end
    end
  end
  super
end

#to_rbsObject



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/solargraph/pin/parameter.rb', line 125

def to_rbs
  case decl
  when :optarg
    "?#{super}"
  when :kwarg
    "#{name}: #{return_type.to_rbs}"
  when :kwoptarg
    "?#{name}: #{return_type.to_rbs}"
  when :restarg
    "*#{super}"
  when :kwrestarg
    "**#{super}"
  else
    super
  end
end

#type_arity_declString

Returns:

  • (String)


97
98
99
# File 'lib/solargraph/pin/parameter.rb', line 97

def type_arity_decl
  arity_decl + return_type.items.count.to_s
end

#type_locationObject



25
26
27
# File 'lib/solargraph/pin/parameter.rb', line 25

def type_location
  super || closure&.type_location
end

#typify(api_map) ⇒ Object

Parameters:



210
211
212
213
214
215
216
217
218
219
220
# File 'lib/solargraph/pin/parameter.rb', line 210

def typify api_map
  new_type = super
  return new_type if new_type.defined?

  # sniff based on param tags
  new_type = closure.is_a?(Pin::Block) ? typify_block_param(api_map) : typify_method_param(api_map)

  return adjust_type api_map, new_type.self_to_type(full_context) if new_type.defined?

  adjust_type api_map, super.self_to_type(full_context)
end