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

Defined Under Namespace

Modules: DeepInference

Class Method Summary collapse

Class Method Details

.call_nodes_from(node) ⇒ Object



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

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



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

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



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

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:



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

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
# 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
    "::#{node.children.first.class.to_s}"
  when :DSTR
    "::String"
  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)


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

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

.pack_name(node) ⇒ Array<String>

Parameters:

Returns:



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



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

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)


119
120
121
# File 'lib/solargraph/parser/rubyvm/node_methods.rb', line 119

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

.splatted_hash?(node) ⇒ Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/solargraph/parser/rubyvm/node_methods.rb', line 108

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

.splatted_node?(node) ⇒ Boolean

Returns:

  • (Boolean)


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

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