Class: Solargraph::Source::Chain

Inherits:
Object
  • Object
show all
Defined in:
lib/solargraph/source/chain.rb,
lib/solargraph/source/chain/if.rb,
lib/solargraph/source/chain/or.rb,
lib/solargraph/source/chain/call.rb,
lib/solargraph/source/chain/hash.rb,
lib/solargraph/source/chain/head.rb,
lib/solargraph/source/chain/link.rb,
lib/solargraph/source/chain/array.rb,
lib/solargraph/source/chain/q_call.rb,
lib/solargraph/source/chain/literal.rb,
lib/solargraph/source/chain/z_super.rb,
lib/solargraph/source/chain/constant.rb,
lib/solargraph/source/chain/variable.rb,
lib/solargraph/source/chain/block_symbol.rb,
lib/solargraph/source/chain/block_variable.rb,
lib/solargraph/source/chain/class_variable.rb,
lib/solargraph/source/chain/global_variable.rb,
lib/solargraph/source/chain/instance_variable.rb

Overview

A chain of constants, variables, and method calls for inferring types of values.

Defined Under Namespace

Classes: Array, BlockSymbol, BlockVariable, Call, ClassVariable, Constant, GlobalVariable, Hash, Head, If, InstanceVariable, Link, Literal, Or, QCall, Variable, ZSuper

Constant Summary collapse

UNDEFINED_CALL =
Chain::Call.new('<undefined>')
UNDEFINED_CONSTANT =
Chain::Constant.new('<undefined>')
@@inference_stack =
[]
@@inference_depth =
0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(links, node = nil, splat = false) ⇒ Chain

Returns a new instance of Chain.

Parameters:

  • node (Parser::AST::Node, nil) (defaults to: nil)
  • links (::Array<Chain::Link>)
  • splat (Boolean) (defaults to: false)


44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/solargraph/source/chain.rb', line 44

def initialize links, node = nil, splat = false
  @links = links.clone
  @links.push UNDEFINED_CALL if @links.empty?
  head = true
  @links.map! do |link|
    result = (head ? link.clone_head : link.clone_body)
    head = false
    result
  end
  @node = node
  @splat = splat
end

Instance Attribute Details

Returns:



37
38
39
# File 'lib/solargraph/source/chain.rb', line 37

def links
  @links
end

#nodeObject (readonly)

Returns the value of attribute node.



39
40
41
# File 'lib/solargraph/source/chain.rb', line 39

def node
  @node
end

Instance Method Details

#baseChain

Returns:



58
59
60
# File 'lib/solargraph/source/chain.rb', line 58

def base
  @base ||= Chain.new(links[0..-2])
end

#constant?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/solargraph/source/chain.rb', line 108

def constant?
  links.last.is_a?(Chain::Constant)
end

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

Parameters:

Returns:



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/solargraph/source/chain.rb', line 67

def define api_map, name_pin, locals
  return [] if undefined?
  working_pin = name_pin
  links[0..-2].each do |link|
    pins = link.resolve(api_map, working_pin, locals)
    type = infer_first_defined(pins, working_pin, api_map, locals)
    return [] if type.undefined?
    working_pin = Pin::ProxyType.anonymous(type)
  end
  links.last.last_context = name_pin
  links.last.resolve(api_map, working_pin, locals)
end

#defined?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/solargraph/source/chain.rb', line 103

def defined?
  !undefined?
end

#infer(api_map, name_pin, locals) ⇒ ComplexType

Parameters:

Returns:



84
85
86
87
88
89
90
91
92
# File 'lib/solargraph/source/chain.rb', line 84

def infer api_map, name_pin, locals
  from_here = base.infer(api_map, name_pin, locals) unless links.length == 1
  if from_here
    name_pin = name_pin.proxy(from_here)
  end
  pins = define(api_map, name_pin, locals)
  type = infer_first_defined(pins, links.last.last_context, api_map, locals)
  maybe_nil(type)
end

#literal?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/solargraph/source/chain.rb', line 95

def literal?
  links.last.is_a?(Chain::Literal)
end

#nullable?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/solargraph/source/chain.rb', line 116

def nullable?
  links.any?(&:nullable?)
end

#splat?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/solargraph/source/chain.rb', line 112

def splat?
  @splat
end

#undefined?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/solargraph/source/chain.rb', line 99

def undefined?
  links.any?(&:undefined?)
end