Module: Solargraph::Parser::Rubyvm::NodeMethods

Defined Under Namespace

Modules: DeepInference

Class Method Summary collapse

Class Method Details

.any_splatted_call?(nodes) ⇒ Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/solargraph/parser/rubyvm/node_methods.rb', line 126

def any_splatted_call?(nodes)
  nodes.any? { |n| splatted_call?(n) }
end

.call_nodes_from(node) ⇒ Object



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

def call_nodes_from node
  return [] unless Parser.is_ast_node?(node)
  result = []
  if node.type == :ITER
    result.push node.children[0]
    node.children[1..-1].each { |child| result.concat call_nodes_from(child) }
  elsif node.type == :MASGN
    # @todo We're treating a mass assignment as a call node, but the
    #   type checker still needs the logic to handle it.
    result.push node
  elsif [:CALL, :VCALL, :FCALL, :ATTRASGN, :OPCALL].include?(node.type)
    result.push node
    node.children.each { |child| result.concat call_nodes_from(child) }
  else
    node.children.each { |child| result.concat call_nodes_from(child) }
  end
  result
end

.const_nodes_from(node) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/solargraph/parser/rubyvm/node_methods.rb', line 64

def const_nodes_from node
  return [] unless Parser.is_ast_node?(node)
  result = []
  if [:CONST, :COLON2, :COLON3].include?(node.type)
    result.push node
  else
    node.children.each { |child| result.concat const_nodes_from(child) }
  end
  result
end

.convert_hash(node) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/solargraph/parser/rubyvm/node_methods.rb', line 94

def convert_hash node
  return {} unless node?(node) && node.type == :HASH
  return convert_hash(node.children[0].children[1]) if splatted_hash?(node)
  return {} unless node?(node.children[0])
  result = {}
  index = 0
  until index > node.children[0].children.length - 2
    k = node.children[0].children[index]
    return {} unless node?(k)
    v = node.children[0].children[index + 1]
    result[k.children[0]] = Solargraph::Parser.chain(v)
    index += 2
  end
  result
end

.find_recipient_node(cursor) ⇒ Object

Parameters:



135
136
137
138
139
140
141
# File 'lib/solargraph/parser/rubyvm/node_methods.rb', line 135

def find_recipient_node cursor
  if cursor.source.synchronized?
    NodeMethods.synchronized_find_recipient_node cursor
  else
    NodeMethods.unsynchronized_find_recipient_node cursor
  end
end

.infer_literal_node_type(node) ⇒ String?

Parameters:

Returns:

  • (String, nil)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/solargraph/parser/rubyvm/node_methods.rb', line 32

def infer_literal_node_type node
  return nil unless Parser.is_ast_node?(node)
  case node.type
  when :LIT, :STR, :SYM
    "::#{node.children.first.class.to_s}"
  when :DSTR
    "::String"
  when :INTEGER
    '::Integer'
  when :ARRAY, :ZARRAY, :LIST, :ZLIST
    '::Array'
  when :HASH
    '::Hash'
  when :DOT2, :DOT3
    '::Range'
  when :TRUE, :FALSE
    '::Boolean'
  when :SCOPE
    infer_literal_node_type(node.children[2])
  end
end

.node?(node) ⇒ Boolean

Returns:

  • (Boolean)


130
131
132
# File 'lib/solargraph/parser/rubyvm/node_methods.rb', line 130

def node? node
  node.is_a?(RubyVM::AbstractSyntaxTree::Node)
end

.pack_name(node) ⇒ Array<String>

Parameters:

Returns:

  • (Array<String>)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/solargraph/parser/rubyvm/node_methods.rb', line 15

def pack_name(node)
  parts = []
  if node.is_a?(RubyVM::AbstractSyntaxTree::Node)
    parts.push '' if node.type == :COLON3
    node.children.each { |n|
      if n.is_a?(RubyVM::AbstractSyntaxTree::Node)
        parts += pack_name(n)
      else
        parts.push n unless n.nil?
      end
    }
  end
  parts
end

.returns_from(node) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/solargraph/parser/rubyvm/node_methods.rb', line 54

def returns_from node
  return [] unless Parser.is_ast_node?(node)
  if node.type == :SCOPE
    # node.children.select { |n| n.is_a?(RubyVM::AbstractSyntaxTree::Node) }.map { |n| DeepInference.get_return_nodes(n) }.flatten
    DeepInference.get_return_nodes(node.children[2])
  else
    DeepInference.get_return_nodes(node)
  end
end

.splatted_call?(node) ⇒ Boolean

Returns:

  • (Boolean)


121
122
123
124
# File 'lib/solargraph/parser/rubyvm/node_methods.rb', line 121

def splatted_call? node
  return false unless Parser.is_ast_node?(node)
  splatted_node?(node) && node.children[0].children[1].type != :HASH
end

.splatted_hash?(node) ⇒ Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/solargraph/parser/rubyvm/node_methods.rb', line 110

def splatted_hash? node
  splatted_node?(node) && node.children[0].children[1].type == :HASH
end

.splatted_node?(node) ⇒ Boolean

Returns:

  • (Boolean)


114
115
116
117
118
119
# File 'lib/solargraph/parser/rubyvm/node_methods.rb', line 114

def splatted_node? node
  node?(node.children[0]) &&
    [:ARRAY, :LIST].include?(node.children[0].type) &&
    node.children[0].children[0].nil? &&
    node?(node.children[0].children[1])
end

.unpack_name(node) ⇒ String

Parameters:

Returns:

  • (String)


9
10
11
# File 'lib/solargraph/parser/rubyvm/node_methods.rb', line 9

def unpack_name node
  pack_name(node).join('::')
end