Module: Solargraph::Parser::Legacy::NodeMethods
- Included in:
- NodeChainer, Solargraph::Parser::Legacy::NodeProcessors::BlockNode, Solargraph::Parser::Legacy::NodeProcessors::CasgnNode, Solargraph::Parser::Legacy::NodeProcessors::DefsNode, Solargraph::Parser::Legacy::NodeProcessors::IvasgnNode, Solargraph::Parser::Legacy::NodeProcessors::LvasgnNode, Solargraph::Parser::Legacy::NodeProcessors::NamespaceNode, Solargraph::Parser::Legacy::NodeProcessors::ResbodyNode, Solargraph::Parser::Legacy::NodeProcessors::SendNode
- Defined in:
- lib/solargraph/parser/legacy/node_methods.rb
Defined Under Namespace
Modules: DeepInference
Constant Summary collapse
- NIL_NODE =
::Parser::AST::Node.new(:nil)
Class Method Summary collapse
- .any_splatted_call?(nodes) ⇒ Boolean
- .call_nodes_from(node) ⇒ Object
- .const_nodes_from(node) ⇒ Object
- .convert_hash(node) ⇒ Object
- .drill_signature(node, signature) ⇒ Object
- .find_recipient_node(cursor) ⇒ Object
- .get_node_end_position(node) ⇒ Position
- .get_node_start_position(node) ⇒ Position
- .infer_literal_node_type(node) ⇒ String?
- .pack_name(node) ⇒ Array<String>
- .repaired_find_recipient_node(cursor) ⇒ Object
-
.returns_from(node) ⇒ Array<Parser::AST::Node>
Find all the nodes within the provided node that potentially return a value.
- .splatted_call?(node) ⇒ Boolean
- .splatted_hash?(node) ⇒ Boolean
- .unpack_name(node) ⇒ String
Class Method Details
.any_splatted_call?(nodes) ⇒ Boolean
133 134 135 |
# File 'lib/solargraph/parser/legacy/node_methods.rb', line 133 def any_splatted_call?(nodes) nodes.any? { |n| splatted_call?(n) } end |
.call_nodes_from(node) ⇒ Object
TODO:
Temporarily here for testing. Move to Solargraph::Parser.
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/solargraph/parser/legacy/node_methods.rb', line 138 def call_nodes_from node return [] unless node.is_a?(::Parser::AST::Node) result = [] if node.type == :block result.push node if Parser.is_ast_node?(node.children[0]) && node.children[0].children.length > 2 node.children[0].children[2..-1].each { |child| result.concat call_nodes_from(child) } end node.children[1..-1].each { |child| result.concat call_nodes_from(child) } elsif node.type == :send result.push node node.children[2..-1].each { |child| result.concat call_nodes_from(child) } elsif [:super, :zsuper].include?(node.type) result.push node node.children.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 else node.children.each { |child| result.concat call_nodes_from(child) } end result end |
.const_nodes_from(node) ⇒ Object
113 114 115 116 117 118 119 120 121 122 |
# File 'lib/solargraph/parser/legacy/node_methods.rb', line 113 def const_nodes_from node return [] unless Parser.is_ast_node?(node) result = [] if node.type == :const result.push node else node.children.each { |child| result.concat const_nodes_from(child) } end result end |
.convert_hash(node) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/solargraph/parser/legacy/node_methods.rb', line 99 def convert_hash node return {} unless Parser.is_ast_node?(node) return convert_hash(node.children[0]) if node.type == :kwsplat return convert_hash(node.children[0]) if Parser.is_ast_node?(node.children[0]) && node.children[0].type == :kwsplat return {} unless node.type == :hash result = {} node.children.each do |pair| result[pair.children[0].children[0]] = Solargraph::Parser.chain(pair.children[1]) end result end |
.drill_signature(node, signature) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/solargraph/parser/legacy/node_methods.rb', line 78 def drill_signature node, signature return signature unless node.is_a?(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 |
.find_recipient_node(cursor) ⇒ Object
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/solargraph/parser/legacy/node_methods.rb', line 180 def find_recipient_node cursor return repaired_find_recipient_node(cursor) if cursor.source.repaired? && cursor.source.code[cursor.offset - 1] == '(' source = cursor.source position = cursor.position offset = cursor.offset tree = if source.synchronized? match = source.code[0..offset-1].match(/,\s*\z/) if match source.tree_at(position.line, position.column - match[0].length) else source.tree_at(position.line, position.column) end else source.tree_at(position.line, position.column - 1) end prev = nil tree.each do |node| if node.type == :send args = node.children[2..-1] if !args.empty? return node if prev && args.include?(prev) else if source.synchronized? return node if source.code[0..offset-1] =~ /\(\s*\z/ && source.code[offset..-1] =~ /^\s*\)/ else return node if source.code[0..offset-1] =~ /\([^\(]*\z/ end end end prev = node end nil end |
.get_node_end_position(node) ⇒ Position
74 75 76 |
# File 'lib/solargraph/parser/legacy/node_methods.rb', line 74 def get_node_end_position(node) Position.new(node.loc.last_line, node.loc.last_column) end |
.get_node_start_position(node) ⇒ Position
68 69 70 |
# File 'lib/solargraph/parser/legacy/node_methods.rb', line 68 def get_node_start_position(node) Position.new(node.loc.line, node.loc.column) end |
.infer_literal_node_type(node) ⇒ String?
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/solargraph/parser/legacy/node_methods.rb', line 39 def infer_literal_node_type node return nil unless node.is_a?(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' elsif node.type == :irange return '::Range' elsif node.type == :true || node.type == :false return '::Boolean' # @todo Support `nil` keyword in types # elsif node.type == :nil # return 'NilClass' end nil end |
.pack_name(node) ⇒ Array<String>
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/solargraph/parser/legacy/node_methods.rb', line 19 def pack_name(node) parts = [] if node.is_a?(AST::Node) node.children.each { |n| if n.is_a?(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 |
.repaired_find_recipient_node(cursor) ⇒ Object
214 215 216 217 218 |
# File 'lib/solargraph/parser/legacy/node_methods.rb', line 214 def repaired_find_recipient_node cursor cursor = cursor.source.cursor_at([cursor.position.line, cursor.position.column - 1]) node = cursor.source.tree_at(cursor.position.line, cursor.position.column).first return node if node && node.type == :send end |
.returns_from(node) ⇒ Array<Parser::AST::Node>
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. Nil values are converted to nil node types.
175 176 177 |
# File 'lib/solargraph/parser/legacy/node_methods.rb', line 175 def returns_from node DeepInference.get_return_nodes(node).map { |n| n || NIL_NODE } end |
.splatted_call?(node) ⇒ Boolean
128 129 130 131 |
# File 'lib/solargraph/parser/legacy/node_methods.rb', line 128 def splatted_call? node return false unless Parser.is_ast_node?(node) Parser.is_ast_node?(node.children[0]) && node.children[0].type == :kwsplat && node.children[0].children[0].type != :hash end |
.splatted_hash?(node) ⇒ Boolean
124 125 126 |
# File 'lib/solargraph/parser/legacy/node_methods.rb', line 124 def splatted_hash? node Parser.is_ast_node?(node.children[0]) && node.children[0].type == :kwsplat end |
.unpack_name(node) ⇒ String
13 14 15 |
# File 'lib/solargraph/parser/legacy/node_methods.rb', line 13 def unpack_name(node) pack_name(node).join("::") end |