Module: Solargraph::Source::NodeMethods
- Included in:
- Pin::BaseVariable, Pin::Method, Solargraph::Source, NodeChainer, SourceChainer, Solargraph::SourceMap::Mapper, Solargraph::SourceMap::NodeProcessor::Base
- Defined in:
- lib/solargraph/source/node_methods.rb
Defined Under Namespace
Modules: DeepInference
Class Method Summary collapse
- .const_from(node) ⇒ String
- .drill_signature(node, signature) ⇒ Object
- .get_node_end_position(node) ⇒ Object
- .get_node_start_position(node) ⇒ Object
- .infer_literal_node_type(node) ⇒ String
- .pack_name(node) ⇒ Array<String>
-
.resolve_node_signature(node) ⇒ String
Get a call signature from a node.
-
.returns_from(node) ⇒ Array<AST::Node>
Find all the nodes within the provided node that potentially return a value.
- .unpack_name(node) ⇒ String
Class Method Details
.const_from(node) ⇒ String
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/solargraph/source/node_methods.rb', line 31 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
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/solargraph/source/node_methods.rb', line 91 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) ⇒ Object
87 88 89 |
# File 'lib/solargraph/source/node_methods.rb', line 87 def get_node_end_position(node) Position.new(node.loc.last_line, node.loc.last_column) end |
.get_node_start_position(node) ⇒ Object
83 84 85 |
# File 'lib/solargraph/source/node_methods.rb', line 83 def get_node_start_position(node) Position.new(node.loc.line, node.loc.column) end |
.infer_literal_node_type(node) ⇒ String
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/solargraph/source/node_methods.rb', line 49 def infer_literal_node_type node return nil unless node.kind_of?(AST::Node) if node.type == :str or 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 Maybe ignore nils # elsif node.type == :nil # return 'NilClass' end nil end |
.pack_name(node) ⇒ Array<String>
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/solargraph/source/node_methods.rb', line 12 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.
77 78 79 80 81 |
# File 'lib/solargraph/source/node_methods.rb', line 77 def resolve_node_signature node result = drill_signature node, '' return nil if result.empty? result end |
.returns_from(node) ⇒ Array<AST::Node>
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.
128 129 130 |
# File 'lib/solargraph/source/node_methods.rb', line 128 def returns_from node DeepInference.get_return_nodes(node) end |
.unpack_name(node) ⇒ String
7 8 9 |
# File 'lib/solargraph/source/node_methods.rb', line 7 def unpack_name(node) pack_name(node).join("::") end |