Module: Solargraph::Source::NodeMethods

Defined Under Namespace

Modules: DeepInference

Class Method Summary collapse

Class Method Details

.const_from(node) ⇒ String


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/solargraph/source/node_methods.rb', line 34

def const_from node
  if node.kind_of?(AST::Node) and node.type == :const
    result = ''
    unless node.children[0].nil?
      result = const_from(node.children[0])
    end
    if result == ''
      result = node.children[1].to_s
    else
      result = result + '::' + node.children[1].to_s
    end
    result
  else
    nil
  end
end

.drill_signature(node, signature) ⇒ Object


100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/solargraph/source/node_methods.rb', line 100

def drill_signature node, signature
  return signature unless node.kind_of?(AST::Node)
  if node.type == :const or node.type == :cbase
    unless node.children[0].nil?
      signature += drill_signature(node.children[0], signature)
    end
    signature += '::' unless signature.empty?
    signature += node.children[1].to_s
  elsif node.type == :lvar or node.type == :ivar or node.type == :cvar
    signature += '.' unless signature.empty?
    signature += node.children[0].to_s
  elsif node.type == :send
    unless node.children[0].nil?
      signature += drill_signature(node.children[0], signature)
    end
    signature += '.' unless signature.empty?
    signature += node.children[1].to_s
  end
  signature
end

.get_node_end_position(node) ⇒ Position


96
97
98
# File 'lib/solargraph/source/node_methods.rb', line 96

def get_node_end_position(node)
  Position.new(node.loc.last_line, node.loc.last_column)
end

.get_node_start_position(node) ⇒ Position


90
91
92
# File 'lib/solargraph/source/node_methods.rb', line 90

def get_node_start_position(node)
  Position.new(node.loc.line, node.loc.column)
end

.infer_literal_node_type(node) ⇒ String


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/solargraph/source/node_methods.rb', line 53

def infer_literal_node_type node
  return nil unless node.kind_of?(AST::Node)
  if node.type == :str || node.type == :dstr
    return 'String'
  elsif node.type == :array
    return 'Array'
  elsif node.type == :hash
    return 'Hash'
  elsif node.type == :int
    return 'Integer'
  elsif node.type == :float
    return 'Float'
  elsif node.type == :sym
    return 'Symbol'
  elsif node.type == :regexp
    return 'Regexp'
  # @todo Support `nil` keyword in types
  # elsif node.type == :nil
  #   return 'NilClass'
  end
  nil
end

.pack_name(node) ⇒ Array<String>


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

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

.resolve_node_signature(node) ⇒ String

Get a call signature from a node. The result should be a string in the form of a method path, e.g., String.new or variable.method.


82
83
84
85
86
# File 'lib/solargraph/source/node_methods.rb', line 82

def resolve_node_signature node
  result = drill_signature node, ''
  return nil if result.empty?
  result
end

.returns_from(node) ⇒ Array<AST::Node>

TODO:

Maybe this method should include implicit nil values in results. For example, a bare `return` would return a :nil node instead of an empty array.

Find all the nodes within the provided node that potentially return a value.

The node parameter typically represents a method's logic, e.g., the second child (after the :args node) of a :def node. A simple one-line method would typically return itself, while a node with conditions would return the resulting node from each conditional branch. Nodes that follow a :return node are assumed to be unreachable. Implicit nil values are ignored.


137
138
139
# File 'lib/solargraph/source/node_methods.rb', line 137

def returns_from node
  DeepInference.get_return_nodes(node)
end

.unpack_name(node) ⇒ String


8
9
10
# File 'lib/solargraph/source/node_methods.rb', line 8

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