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)


99
100
101
# File 'lib/solargraph/pin/parameter.rb', line 99

def arg?
  decl == :arg
end

#arity_declString

Returns:

  • (String)


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

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)


119
120
121
# File 'lib/solargraph/pin/parameter.rb', line 119

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

#combine_return_type(other) ⇒ Object



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

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
# File 'lib/solargraph/pin/parameter.rb', line 33

def combine_with other, attrs = {}
  # Parameters can be combined with local variables
  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)


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

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



236
237
238
239
240
# File 'lib/solargraph/pin/parameter.rb', line 236

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

#fullString

Returns:

  • (String)


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

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.



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

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)


201
202
203
204
205
# File 'lib/solargraph/pin/parameter.rb', line 201

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

#keyword?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/solargraph/pin/parameter.rb', line 59

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

#kwrestarg?Boolean

Returns:

  • (Boolean)


63
64
65
66
# File 'lib/solargraph/pin/parameter.rb', line 63

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)


107
108
109
# File 'lib/solargraph/pin/parameter.rb', line 107

def mandatory_positional?
  decl == :arg
end

#needs_consistent_name?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/solargraph/pin/parameter.rb', line 68

def needs_consistent_name?
  keyword?
end

#positional?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/solargraph/pin/parameter.rb', line 111

def positional?
  !keyword?
end

#reset_generated!Object



158
159
160
161
# File 'lib/solargraph/pin/parameter.rb', line 158

def reset_generated!
  @return_type = nil if param_tag
  super
end

#rest?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/solargraph/pin/parameter.rb', line 115

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

#restarg?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/solargraph/pin/parameter.rb', line 103

def restarg?
  decl == :restarg
end

#return_typeComplexType

@sg-ignore super always sets @return_type to something

Returns:



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

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



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

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)


95
96
97
# File 'lib/solargraph/pin/parameter.rb', line 95

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:



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

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