Class: Solargraph::Source::Chain::Parameter

Inherits:
Link
  • Object
show all
Defined in:
lib/solargraph/source/chain/parameter.rb

Overview

A method parameter represented as a link in a method call chain.

Examples:

some_method('literal_value')
             ^^^^^^^^^^^^^

Constant Summary

Constants included from Logging

Logging::DEFAULT_LOG_LEVEL, Logging::LOG_LEVELS

Instance Attribute Summary

Attributes inherited from Link

#last_context

Instance Method Summary collapse

Methods inherited from Link

#clone_body, #clone_head, #constant?, #desc, #head?, #inspect, #nullable?, #to_s, #undefined?

Methods included from Logging

log_level, logger

Methods included from Equality

#==, #eql?, #equality_fields, #freeze, #hash

Constructor Details

#initialize(literal, method_call_chain) ⇒ Parameter

Returns a new instance of Parameter.

Parameters:

  • literal (Chain::Literal) —
    • literal argument
  • method_call_chain (Chain) —

    method call that contains the argument



14
15
16
17
# File 'lib/solargraph/source/chain/parameter.rb', line 14

def initialize literal, method_call_chain # rubocop:disable Lint/MissingSuper
  @literal = literal
  @method_call_chain = method_call_chain
end

Instance Method Details

#require_parameter? ⇒ Boolean

Returns true if this is a parameter of Kernel#require.

Returns:

  • (Boolean) —

    true if this is a parameter of Kernel#require



44
45
46
# File 'lib/solargraph/source/chain/parameter.rb', line 44

def require_parameter?
  method_call_chain.links.last.word == 'require'
end

#resolve(api_map, name_pin, locals) ⇒ ::Array<Pin::Base>

Parameters:

Returns:



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/solargraph/source/chain/parameter.rb', line 27

def resolve api_map, name_pin, locals
  # @type [Pin::Method]
  method_pin = method_call_chain.define(api_map, name_pin, locals)&.first
  return [] unless method_pin

  # Use indexed lookup for better performance
  api_map.factory_parameters_for_method(method_pin).select do |fp|
    param_index = method_pin.parameters.find_index { |param| param.name == fp.param_name }
    next if param_index.nil?
    next false unless fp.value == literal_value

    # a splat takes every argument from its position on
    method_pin.parameters[param_index].restarg? ? current_index >= param_index : current_index == param_index
  end
end

#word ⇒ Object



19
20
21
# File 'lib/solargraph/source/chain/parameter.rb', line 19

def word
  @word ||= "#{method_name}(.., #{@literal.word}, ..)"
end