solargraph 0.57.0 → 0.58.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (216) hide show
  1. checksums.yaml +4 -4
  2. data/.gitattributes +2 -0
  3. data/.github/workflows/linting.yml +4 -2
  4. data/.github/workflows/plugins.yml +63 -28
  5. data/.github/workflows/rspec.yml +19 -4
  6. data/.github/workflows/typecheck.yml +2 -2
  7. data/.gitignore +1 -0
  8. data/.rubocop.yml +1 -1
  9. data/.rubocop_todo.yml +90 -1438
  10. data/CHANGELOG.md +39 -0
  11. data/Rakefile +1 -1
  12. data/bin/solargraph +3 -0
  13. data/lib/solargraph/api_map/cache.rb +110 -110
  14. data/lib/solargraph/api_map/constants.rb +279 -218
  15. data/lib/solargraph/api_map/index.rb +193 -169
  16. data/lib/solargraph/api_map/source_to_yard.rb +97 -94
  17. data/lib/solargraph/api_map/store.rb +384 -374
  18. data/lib/solargraph/api_map.rb +945 -951
  19. data/lib/solargraph/bench.rb +45 -45
  20. data/lib/solargraph/complex_type/type_methods.rb +228 -223
  21. data/lib/solargraph/complex_type/unique_type.rb +482 -475
  22. data/lib/solargraph/complex_type.rb +444 -427
  23. data/lib/solargraph/convention/active_support_concern.rb +1 -1
  24. data/lib/solargraph/convention/data_definition/data_assignment_node.rb +61 -61
  25. data/lib/solargraph/convention/data_definition/data_definition_node.rb +91 -91
  26. data/lib/solargraph/convention/data_definition.rb +105 -105
  27. data/lib/solargraph/convention/gemfile.rb +15 -15
  28. data/lib/solargraph/convention/gemspec.rb +23 -23
  29. data/lib/solargraph/convention/rakefile.rb +17 -17
  30. data/lib/solargraph/convention/struct_definition/struct_assignment_node.rb +61 -61
  31. data/lib/solargraph/convention/struct_definition/struct_definition_node.rb +102 -102
  32. data/lib/solargraph/convention/struct_definition.rb +164 -164
  33. data/lib/solargraph/convention.rb +78 -78
  34. data/lib/solargraph/converters/dd.rb +17 -17
  35. data/lib/solargraph/converters/dl.rb +15 -15
  36. data/lib/solargraph/converters/dt.rb +15 -15
  37. data/lib/solargraph/converters/misc.rb +1 -1
  38. data/lib/solargraph/diagnostics/require_not_found.rb +53 -53
  39. data/lib/solargraph/diagnostics/rubocop.rb +118 -118
  40. data/lib/solargraph/diagnostics/rubocop_helpers.rb +68 -66
  41. data/lib/solargraph/diagnostics/type_check.rb +55 -55
  42. data/lib/solargraph/diagnostics/update_errors.rb +41 -41
  43. data/lib/solargraph/doc_map.rb +439 -436
  44. data/lib/solargraph/environ.rb +1 -1
  45. data/lib/solargraph/equality.rb +34 -33
  46. data/lib/solargraph/gem_pins.rb +98 -94
  47. data/lib/solargraph/language_server/error_codes.rb +20 -20
  48. data/lib/solargraph/language_server/host/diagnoser.rb +89 -89
  49. data/lib/solargraph/language_server/host/dispatch.rb +130 -130
  50. data/lib/solargraph/language_server/host/message_worker.rb +112 -112
  51. data/lib/solargraph/language_server/host/sources.rb +99 -99
  52. data/lib/solargraph/language_server/host.rb +878 -872
  53. data/lib/solargraph/language_server/message/base.rb +97 -97
  54. data/lib/solargraph/language_server/message/client/register_capability.rb +15 -15
  55. data/lib/solargraph/language_server/message/completion_item/resolve.rb +60 -60
  56. data/lib/solargraph/language_server/message/extended/check_gem_version.rb +114 -114
  57. data/lib/solargraph/language_server/message/extended/document.rb +23 -23
  58. data/lib/solargraph/language_server/message/extended/document_gems.rb +32 -32
  59. data/lib/solargraph/language_server/message/extended/download_core.rb +19 -19
  60. data/lib/solargraph/language_server/message/extended/search.rb +20 -20
  61. data/lib/solargraph/language_server/message/initialize.rb +191 -191
  62. data/lib/solargraph/language_server/message/text_document/completion.rb +56 -56
  63. data/lib/solargraph/language_server/message/text_document/definition.rb +40 -40
  64. data/lib/solargraph/language_server/message/text_document/document_highlight.rb +16 -16
  65. data/lib/solargraph/language_server/message/text_document/document_symbol.rb +26 -26
  66. data/lib/solargraph/language_server/message/text_document/formatting.rb +148 -145
  67. data/lib/solargraph/language_server/message/text_document/hover.rb +58 -58
  68. data/lib/solargraph/language_server/message/text_document/prepare_rename.rb +11 -11
  69. data/lib/solargraph/language_server/message/text_document/references.rb +16 -16
  70. data/lib/solargraph/language_server/message/text_document/rename.rb +19 -19
  71. data/lib/solargraph/language_server/message/text_document/signature_help.rb +24 -24
  72. data/lib/solargraph/language_server/message/text_document/type_definition.rb +25 -25
  73. data/lib/solargraph/language_server/message/workspace/did_change_configuration.rb +35 -35
  74. data/lib/solargraph/language_server/message/workspace/did_change_watched_files.rb +40 -40
  75. data/lib/solargraph/language_server/message/workspace/did_change_workspace_folders.rb +26 -26
  76. data/lib/solargraph/language_server/message/workspace/workspace_symbol.rb +23 -23
  77. data/lib/solargraph/language_server/message.rb +94 -94
  78. data/lib/solargraph/language_server/progress.rb +1 -1
  79. data/lib/solargraph/language_server/request.rb +27 -25
  80. data/lib/solargraph/language_server/transport/data_reader.rb +74 -74
  81. data/lib/solargraph/language_server/uri_helpers.rb +49 -49
  82. data/lib/solargraph/library.rb +683 -683
  83. data/lib/solargraph/location.rb +82 -81
  84. data/lib/solargraph/logging.rb +37 -37
  85. data/lib/solargraph/page.rb +92 -93
  86. data/lib/solargraph/parser/comment_ripper.rb +69 -69
  87. data/lib/solargraph/parser/flow_sensitive_typing.rb +255 -255
  88. data/lib/solargraph/parser/node_processor/base.rb +92 -92
  89. data/lib/solargraph/parser/node_processor.rb +62 -62
  90. data/lib/solargraph/parser/parser_gem/class_methods.rb +149 -159
  91. data/lib/solargraph/parser/parser_gem/flawed_builder.rb +19 -19
  92. data/lib/solargraph/parser/parser_gem/node_chainer.rb +166 -166
  93. data/lib/solargraph/parser/parser_gem/node_methods.rb +486 -499
  94. data/lib/solargraph/parser/parser_gem/node_processors/and_node.rb +22 -21
  95. data/lib/solargraph/parser/parser_gem/node_processors/args_node.rb +59 -59
  96. data/lib/solargraph/parser/parser_gem/node_processors/begin_node.rb +15 -15
  97. data/lib/solargraph/parser/parser_gem/node_processors/block_node.rb +46 -46
  98. data/lib/solargraph/parser/parser_gem/node_processors/def_node.rb +53 -53
  99. data/lib/solargraph/parser/parser_gem/node_processors/defs_node.rb +37 -37
  100. data/lib/solargraph/parser/parser_gem/node_processors/if_node.rb +23 -23
  101. data/lib/solargraph/parser/parser_gem/node_processors/ivasgn_node.rb +40 -40
  102. data/lib/solargraph/parser/parser_gem/node_processors/lvasgn_node.rb +29 -29
  103. data/lib/solargraph/parser/parser_gem/node_processors/masgn_node.rb +59 -59
  104. data/lib/solargraph/parser/parser_gem/node_processors/opasgn_node.rb +98 -42
  105. data/lib/solargraph/parser/parser_gem/node_processors/orasgn_node.rb +17 -17
  106. data/lib/solargraph/parser/parser_gem/node_processors/resbody_node.rb +38 -38
  107. data/lib/solargraph/parser/parser_gem/node_processors/sclass_node.rb +52 -43
  108. data/lib/solargraph/parser/parser_gem/node_processors/send_node.rb +291 -292
  109. data/lib/solargraph/parser/parser_gem/node_processors/until_node.rb +29 -29
  110. data/lib/solargraph/parser/parser_gem/node_processors/while_node.rb +29 -29
  111. data/lib/solargraph/parser/parser_gem/node_processors.rb +70 -70
  112. data/lib/solargraph/parser/parser_gem.rb +12 -12
  113. data/lib/solargraph/parser/region.rb +69 -69
  114. data/lib/solargraph/parser/snippet.rb +17 -17
  115. data/lib/solargraph/parser.rb +23 -23
  116. data/lib/solargraph/pin/base.rb +729 -708
  117. data/lib/solargraph/pin/base_variable.rb +126 -124
  118. data/lib/solargraph/pin/block.rb +104 -103
  119. data/lib/solargraph/pin/breakable.rb +9 -9
  120. data/lib/solargraph/pin/callable.rb +231 -227
  121. data/lib/solargraph/pin/closure.rb +72 -76
  122. data/lib/solargraph/pin/common.rb +79 -79
  123. data/lib/solargraph/pin/constant.rb +45 -45
  124. data/lib/solargraph/pin/conversions.rb +123 -123
  125. data/lib/solargraph/pin/delegated_method.rb +120 -121
  126. data/lib/solargraph/pin/documenting.rb +114 -114
  127. data/lib/solargraph/pin/instance_variable.rb +34 -34
  128. data/lib/solargraph/pin/keyword.rb +20 -20
  129. data/lib/solargraph/pin/local_variable.rb +75 -79
  130. data/lib/solargraph/pin/method.rb +672 -656
  131. data/lib/solargraph/pin/method_alias.rb +34 -34
  132. data/lib/solargraph/pin/namespace.rb +115 -115
  133. data/lib/solargraph/pin/parameter.rb +275 -271
  134. data/lib/solargraph/pin/proxy_type.rb +39 -36
  135. data/lib/solargraph/pin/reference/override.rb +47 -47
  136. data/lib/solargraph/pin/reference/superclass.rb +15 -15
  137. data/lib/solargraph/pin/reference.rb +39 -48
  138. data/lib/solargraph/pin/search.rb +61 -58
  139. data/lib/solargraph/pin/signature.rb +61 -61
  140. data/lib/solargraph/pin/symbol.rb +53 -53
  141. data/lib/solargraph/pin/until.rb +18 -18
  142. data/lib/solargraph/pin/while.rb +18 -18
  143. data/lib/solargraph/pin.rb +44 -44
  144. data/lib/solargraph/pin_cache.rb +245 -245
  145. data/lib/solargraph/position.rb +132 -118
  146. data/lib/solargraph/range.rb +112 -108
  147. data/lib/solargraph/rbs_map/conversions.rb +823 -802
  148. data/lib/solargraph/rbs_map/core_fills.rb +84 -66
  149. data/lib/solargraph/rbs_map/core_map.rb +58 -54
  150. data/lib/solargraph/rbs_map/stdlib_map.rb +43 -43
  151. data/lib/solargraph/rbs_map.rb +163 -163
  152. data/lib/solargraph/server_methods.rb +16 -16
  153. data/lib/solargraph/shell.rb +363 -271
  154. data/lib/solargraph/source/chain/array.rb +37 -37
  155. data/lib/solargraph/source/chain/call.rb +337 -333
  156. data/lib/solargraph/source/chain/class_variable.rb +13 -13
  157. data/lib/solargraph/source/chain/constant.rb +26 -89
  158. data/lib/solargraph/source/chain/global_variable.rb +13 -13
  159. data/lib/solargraph/source/chain/hash.rb +34 -34
  160. data/lib/solargraph/source/chain/if.rb +28 -28
  161. data/lib/solargraph/source/chain/instance_variable.rb +13 -13
  162. data/lib/solargraph/source/chain/link.rb +109 -109
  163. data/lib/solargraph/source/chain/literal.rb +48 -48
  164. data/lib/solargraph/source/chain/or.rb +23 -23
  165. data/lib/solargraph/source/chain/q_call.rb +11 -11
  166. data/lib/solargraph/source/chain/variable.rb +13 -13
  167. data/lib/solargraph/source/chain/z_super.rb +30 -30
  168. data/lib/solargraph/source/chain.rb +291 -289
  169. data/lib/solargraph/source/change.rb +82 -82
  170. data/lib/solargraph/source/cursor.rb +166 -166
  171. data/lib/solargraph/source/encoding_fixes.rb +23 -23
  172. data/lib/solargraph/source/source_chainer.rb +194 -194
  173. data/lib/solargraph/source/updater.rb +55 -55
  174. data/lib/solargraph/source.rb +498 -498
  175. data/lib/solargraph/source_map/clip.rb +226 -234
  176. data/lib/solargraph/source_map/data.rb +34 -34
  177. data/lib/solargraph/source_map/mapper.rb +259 -261
  178. data/lib/solargraph/source_map.rb +212 -207
  179. data/lib/solargraph/type_checker/checks.rb +124 -124
  180. data/lib/solargraph/type_checker/param_def.rb +37 -37
  181. data/lib/solargraph/type_checker/problem.rb +32 -32
  182. data/lib/solargraph/type_checker/rules.rb +84 -70
  183. data/lib/solargraph/type_checker.rb +814 -752
  184. data/lib/solargraph/version.rb +5 -5
  185. data/lib/solargraph/workspace/config.rb +255 -237
  186. data/lib/solargraph/workspace/require_paths.rb +97 -98
  187. data/lib/solargraph/workspace.rb +220 -225
  188. data/lib/solargraph/yard_map/helpers.rb +44 -44
  189. data/lib/solargraph/yard_map/mapper/to_method.rb +130 -129
  190. data/lib/solargraph/yard_map/mapper/to_namespace.rb +31 -30
  191. data/lib/solargraph/yard_map/mapper.rb +79 -79
  192. data/lib/solargraph/yard_map/to_method.rb +89 -88
  193. data/lib/solargraph/yard_tags.rb +20 -20
  194. data/lib/solargraph/yardoc.rb +87 -64
  195. data/lib/solargraph.rb +105 -105
  196. data/rbs/fills/bundler/0/bundler.rbs +4271 -0
  197. data/rbs/fills/open3/0/open3.rbs +172 -0
  198. data/rbs/fills/rubygems/0/basic_specification.rbs +326 -0
  199. data/rbs/fills/rubygems/0/errors.rbs +364 -0
  200. data/rbs/fills/rubygems/0/spec_fetcher.rbs +107 -0
  201. data/rbs/fills/rubygems/0/specification.rbs +1753 -0
  202. data/rbs/shims/ast/0/node.rbs +5 -0
  203. data/rbs/shims/ast/2.4/.rbs_meta.yaml +9 -0
  204. data/rbs/shims/ast/2.4/ast.rbs +73 -0
  205. data/rbs/shims/parser/3.2.0.1/manifest.yaml +7 -0
  206. data/rbs/shims/parser/3.2.0.1/parser.rbs +201 -0
  207. data/rbs/shims/parser/3.2.0.1/polyfill.rbs +4 -0
  208. data/rbs_collection.yaml +4 -4
  209. data/solargraph.gemspec +15 -4
  210. metadata +71 -16
  211. data/lib/solargraph/parser/node_methods.rb +0 -97
  212. /data/rbs/fills/{tuple.rbs → tuple/tuple.rbs} +0 -0
  213. /data/{sig → rbs}/shims/parser/3.2.0.1/builders/default.rbs +0 -0
  214. /data/{sig → rbs}/shims/thor/1.2.0.1/.rbs_meta.yaml +0 -0
  215. /data/{sig → rbs}/shims/thor/1.2.0.1/manifest.yaml +0 -0
  216. /data/{sig → rbs}/shims/thor/1.2.0.1/thor.rbs +0 -0
@@ -1,499 +1,486 @@
1
- # frozen_string_literal: true
2
-
3
- require 'parser'
4
- require 'ast'
5
-
6
- # Teach AST::Node#children about its generic type
7
- #
8
- # @todo contribute back to https://github.com/ruby/gem_rbs_collection/blob/main/gems/ast/2.4/ast.rbs
9
- #
10
- # @!parse
11
- # module ::AST
12
- # class Node
13
- # # New children
14
- #
15
- # # @return [Array<self, Integer, String, Symbol, nil>]
16
- # attr_reader :children
17
- # end
18
- # end
19
-
20
- # https://github.com/whitequark/parser
21
- module Solargraph
22
- module Parser
23
- module ParserGem
24
- module NodeMethods
25
- module_function
26
-
27
- # @param node [Parser::AST::Node]
28
- # @return [String]
29
- def unpack_name(node)
30
- pack_name(node).join("::")
31
- end
32
-
33
- # @param node [Parser::AST::Node]
34
- # @return [Array<String>]
35
- def pack_name(node)
36
- # @type [Array<String>]
37
- parts = []
38
- if node.is_a?(AST::Node)
39
- node.children.each { |n|
40
- if n.is_a?(AST::Node)
41
- if n.type == :cbase
42
- parts = [''] + pack_name(n)
43
- elsif n.type == :const
44
- parts += pack_name(n)
45
- end
46
- else
47
- parts.push n unless n.nil?
48
- end
49
- }
50
- end
51
- parts
52
- end
53
-
54
- # @param node [Parser::AST::Node]
55
- # @return [String, nil]
56
- def infer_literal_node_type node
57
- return nil unless node.is_a?(AST::Node)
58
- if node.type == :str || node.type == :dstr
59
- return '::String'
60
- elsif node.type == :array
61
- return '::Array'
62
- elsif node.type == :nil
63
- return '::NilClass'
64
- elsif node.type == :hash
65
- return '::Hash'
66
- elsif node.type == :int
67
- return '::Integer'
68
- elsif node.type == :float
69
- return '::Float'
70
- elsif node.type == :sym || node.type == :dsym
71
- return '::Symbol'
72
- elsif node.type == :regexp
73
- return '::Regexp'
74
- elsif node.type == :irange
75
- return '::Range'
76
- elsif node.type == :true || node.type == :false
77
- return '::Boolean'
78
- # @todo Support `nil` keyword in types
79
- # elsif node.type == :nil
80
- # return 'NilClass'
81
- end
82
- nil
83
- end
84
-
85
- # @param node [Parser::AST::Node]
86
- # @return [Position]
87
- def get_node_start_position(node)
88
- Position.new(node.loc.line, node.loc.column)
89
- end
90
-
91
- # @param node [Parser::AST::Node]
92
- # @return [Position]
93
- def get_node_end_position(node)
94
- Position.new(node.loc.last_line, node.loc.last_column)
95
- end
96
-
97
- # @param node [Parser::AST::Node]
98
- # @param signature [String]
99
- #
100
- # @return [String]
101
- def drill_signature node, signature
102
- return signature unless node.is_a?(AST::Node)
103
- if node.type == :const or node.type == :cbase
104
- unless node.children[0].nil?
105
- signature += drill_signature(node.children[0], signature)
106
- end
107
- signature += '::' unless signature.empty?
108
- signature += node.children[1].to_s
109
- elsif node.type == :lvar or node.type == :ivar or node.type == :cvar
110
- signature += '.' unless signature.empty?
111
- signature += node.children[0].to_s
112
- elsif node.type == :send
113
- unless node.children[0].nil?
114
- signature += drill_signature(node.children[0], signature)
115
- end
116
- signature += '.' unless signature.empty?
117
- signature += node.children[1].to_s
118
- end
119
- signature
120
- end
121
-
122
- # @param node [Parser::AST::Node]
123
- # @return [Hash{Symbol => Chain}]
124
- def convert_hash node
125
- return {} unless Parser.is_ast_node?(node)
126
- return convert_hash(node.children[0]) if node.type == :kwsplat
127
- return convert_hash(node.children[0]) if Parser.is_ast_node?(node.children[0]) && node.children[0].type == :kwsplat
128
- return {} unless node.type == :hash
129
- result = {}
130
- node.children.each do |pair|
131
- result[pair.children[0].children[0]] = Solargraph::Parser.chain(pair.children[1])
132
- end
133
- result
134
- end
135
-
136
- NIL_NODE = ::Parser::AST::Node.new(:nil)
137
-
138
- # @param node [Parser::AST::Node]
139
- #
140
- # @return [Array<Parser::AST::Node>]
141
- def const_nodes_from node
142
- return [] unless Parser.is_ast_node?(node)
143
- result = []
144
- if node.type == :const
145
- result.push node
146
- else
147
- node.children.each { |child| result.concat const_nodes_from(child) }
148
- end
149
- result
150
- end
151
-
152
- # @param node [Parser::AST::Node]
153
- def splatted_hash? node
154
- Parser.is_ast_node?(node.children[0]) && node.children[0].type == :kwsplat
155
- end
156
-
157
- # @param node [Parser::AST::Node]
158
- def splatted_call? node
159
- return false unless Parser.is_ast_node?(node)
160
- Parser.is_ast_node?(node.children[0]) && node.children[0].type == :kwsplat && node.children[0].children[0].type != :hash
161
- end
162
-
163
- # @param nodes [Enumerable<Parser::AST::Node>]
164
- def any_splatted_call?(nodes)
165
- nodes.any? { |n| splatted_call?(n) }
166
- end
167
-
168
- # @todo Temporarily here for testing. Move to Solargraph::Parser.
169
- # @param node [Parser::AST::Node]
170
- # @return [Array<Parser::AST::Node>]
171
- def call_nodes_from node
172
- return [] unless node.is_a?(::Parser::AST::Node)
173
- result = []
174
- if node.type == :block
175
- result.push node
176
- if Parser.is_ast_node?(node.children[0]) && node.children[0].children.length > 2
177
- node.children[0].children[2..-1].each { |child| result.concat call_nodes_from(child) }
178
- end
179
- node.children[1..-1].each { |child| result.concat call_nodes_from(child) }
180
- elsif node.type == :send
181
- result.push node
182
- result.concat call_nodes_from(node.children.first)
183
- node.children[2..-1].each { |child| result.concat call_nodes_from(child) }
184
- elsif [:super, :zsuper].include?(node.type)
185
- result.push node
186
- node.children.each { |child| result.concat call_nodes_from(child) }
187
- else
188
- node.children.each { |child| result.concat call_nodes_from(child) }
189
- end
190
- result
191
- end
192
-
193
- # Find all the nodes within the provided node that potentially return a
194
- # value.
195
- #
196
- # The node parameter typically represents a method's logic, e.g., the
197
- # second child (after the :args node) of a :def node. A simple one-line
198
- # method would typically return itself, while a node with conditions
199
- # would return the resulting node from each conditional branch. Nodes
200
- # that follow a :return node are assumed to be unreachable. Nil values
201
- # are converted to nil node types.
202
- #
203
- # @param node [Parser::AST::Node]
204
- # @return [Array<Parser::AST::Node>]
205
- def returns_from_method_body node
206
- # @todo is the || NIL_NODE necessary?
207
- # STDERR.puts("Evaluating expression: #{node.inspect}")
208
- DeepInference.from_method_body(node).map { |n| n || NIL_NODE }
209
- end
210
-
211
- # @param node [Parser::AST::Node]
212
- # @return [Array<AST::Node>] low-level value nodes in
213
- # value position. Does not include explicit return
214
- # statements
215
- def value_position_nodes_only(node)
216
- DeepInference.value_position_nodes_only(node).map { |n| n || NIL_NODE }
217
- end
218
-
219
- # @param cursor [Solargraph::Source::Cursor]
220
- # @return [Parser::AST::Node, nil]
221
- def find_recipient_node cursor
222
- return repaired_find_recipient_node(cursor) if cursor.source.repaired? && cursor.source.code[cursor.offset - 1] == '('
223
- source = cursor.source
224
- position = cursor.position
225
- offset = cursor.offset
226
- tree = if source.synchronized?
227
- match = source.code[0..offset-1].match(/,\s*\z/)
228
- if match
229
- source.tree_at(position.line, position.column - match[0].length)
230
- else
231
- source.tree_at(position.line, position.column)
232
- end
233
- else
234
- source.tree_at(position.line, position.column - 1)
235
- end
236
- # @type [AST::Node, nil]
237
- prev = nil
238
- tree.each do |node|
239
- if node.type == :send
240
- args = node.children[2..-1]
241
- if !args.empty?
242
- return node if prev && args.include?(prev)
243
- else
244
- if source.synchronized?
245
- return node if source.code[0..offset-1] =~ /\(\s*\z/ && source.code[offset..-1] =~ /^\s*\)/
246
- else
247
- return node if source.code[0..offset-1] =~ /\([^(]*\z/
248
- end
249
- end
250
- end
251
- prev = node
252
- end
253
- nil
254
- end
255
-
256
- # @param cursor [Solargraph::Source::Cursor]
257
- # @return [Parser::AST::Node, nil]
258
- def repaired_find_recipient_node cursor
259
- cursor = cursor.source.cursor_at([cursor.position.line, cursor.position.column - 1])
260
- node = cursor.source.tree_at(cursor.position.line, cursor.position.column).first
261
- return node if node && node.type == :send
262
- end
263
-
264
- #
265
- # Concepts:
266
- #
267
- # - statement - one single node in the AST. Generally used
268
- # synonymously with how the Parser gem uses the term
269
- # 'expression'. This may have side effects (e.g.,
270
- # registering a method in the namespace, modifying
271
- # variables or doing I/O). It may encapsulate multiple
272
- # other statements (see compound statement).
273
- #
274
- # - value - something that can be assigned to a variable by
275
- # evaluating a statement
276
- #
277
- # - value node - the 'lowest level' AST node whose return
278
- # type is a subset of the value type of the overall
279
- # statement. Might be a literal, a method call, etc - the
280
- # goal is to find the lowest level node, which we can use
281
- # Chains and Pins later on to determine the type of.
282
- #
283
- # e.g., if the node 'b ? 123 : 456' were a return value, we
284
- # know the actual return values possible are 123 and 456,
285
- # and can disregard the rest.
286
- #
287
- # - value type - the type representing the multiple possible
288
- # values that can result from evaluation of the statement.
289
- #
290
- # - return type - the type describing the values a statement
291
- # might evaluate to. When used with a method, the term
292
- # describes the values that may result from the method
293
- # being called, and includes explicit return statements
294
- # within the method body's closure.
295
- #
296
- # - method body - a compound statement with parameters whose
297
- # return value type must account both for the explicit
298
- # 'return' statemnts as well as the final statements
299
- # executed in any given control flow through the method.
300
- #
301
- # - explicit return statement - a statement which, when part of a
302
- # method body, is a possible value of a call to that method -
303
- # e.g., "return 123"
304
- #
305
- # - compound statement - a statement which can be expanded to
306
- # be multiple statements in a row, executed in the context
307
- # of a method which can be explicitly returned from.
308
- #
309
- # - value position - the positions in the AST where the
310
- # return type of the statement would be one of the return
311
- # types of any compound statements it is a part of. For a
312
- # compound statement, the last of the child statements
313
- # would be in return position. This concept can be applied
314
- # recursively through e.g. conditionals to find a list of
315
- # statements in value positions.
316
- module DeepInference
317
- class << self
318
- CONDITIONAL_ALL_BUT_FIRST = [:if, :unless]
319
- CONDITIONAL_ALL = [:or]
320
- ONLY_ONE_CHILD = [:return]
321
- FIRST_TWO_CHILDREN = [:rescue]
322
- COMPOUND_STATEMENTS = [:begin, :kwbegin]
323
- SKIPPABLE = [:def, :defs, :class, :sclass, :module]
324
- FUNCTION_VALUE = [:block]
325
- CASE_STATEMENT = [:case]
326
-
327
- # @param node [AST::Node] a method body compound statement
328
- # @param include_explicit_returns [Boolean] If true,
329
- # include the value nodes of the parameter of the
330
- # 'return' statements in the type returned.
331
- # @return [Array<AST::Node>] low-level value nodes from
332
- # both nodes in value position as well as explicit
333
- # return statements in the method's closure.
334
- def from_method_body node
335
- from_value_position_statement(node, include_explicit_returns: true)
336
- end
337
-
338
- # @param node [AST::Node] an individual statement, to be
339
- # evaluated outside the context of a containing method
340
- # @return [Array<AST::Node>] low-level value nodes in
341
- # value position. Does not include explicit return
342
- # statements
343
- def value_position_nodes_only(node)
344
- from_value_position_statement(node, include_explicit_returns: false)
345
- end
346
-
347
- # Look at known control statements and use them to find
348
- # more specific return nodes.
349
- #
350
- # @param node [Parser::AST::Node] Statement which is in
351
- # value position for a method body
352
- # @param include_explicit_returns [Boolean] If true,
353
- # include the value nodes of the parameter of the
354
- # 'return' statements in the type returned.
355
- # @return [Array<Parser::AST::Node>]
356
- def from_value_position_statement node, include_explicit_returns: true
357
- # STDERR.puts("from_expression called on #{node.inspect}")
358
- return [] unless node.is_a?(::Parser::AST::Node)
359
- # @type [Array<Parser::AST::Node>]
360
- result = []
361
- if COMPOUND_STATEMENTS.include?(node.type)
362
- result.concat from_value_position_compound_statement node
363
- elsif CONDITIONAL_ALL_BUT_FIRST.include?(node.type)
364
- result.concat reduce_to_value_nodes(node.children[1..-1])
365
- # result.push NIL_NODE unless node.children[2]
366
- elsif CONDITIONAL_ALL.include?(node.type)
367
- result.concat reduce_to_value_nodes(node.children)
368
- elsif ONLY_ONE_CHILD.include?(node.type)
369
- result.concat reduce_to_value_nodes([node.children[0]])
370
- elsif FIRST_TWO_CHILDREN.include?(node.type)
371
- result.concat reduce_to_value_nodes([node.children[0], node.children[1]])
372
- elsif FUNCTION_VALUE.include?(node.type)
373
- # the block itself is a first class value that could be returned
374
- result.push node
375
- # @todo any explicit returns actually return from
376
- # scope in which the proc is run. This asssumes
377
- # that the function is executed here.
378
- result.concat explicit_return_values_from_compound_statement(node.children[2]) if include_explicit_returns
379
- elsif CASE_STATEMENT.include?(node.type)
380
- node.children[1..-1].each do |cc|
381
- if cc.nil?
382
- result.push NIL_NODE
383
- elsif cc.type == :when
384
- result.concat reduce_to_value_nodes([cc.children.last])
385
- else
386
- # else clause in case
387
- result.concat reduce_to_value_nodes([cc])
388
- end
389
- end
390
- elsif node.type == :resbody
391
- result.concat reduce_to_value_nodes([node.children[2]])
392
- else
393
- result.push node
394
- end
395
- result
396
- end
397
-
398
- # Treat parent as as a begin block and use the last node's
399
- # return node plus any explicit return nodes' return nodes. e.g.,
400
- #
401
- # 123
402
- # 456
403
- # return 'a' if foo == bar
404
- # 789
405
- #
406
- # would return 'a' and 789.
407
- #
408
- # @param parent [Parser::AST::Node]
409
- #
410
- # @return [Array<Parser::AST::Node>]
411
- def from_value_position_compound_statement parent
412
- result = []
413
- nodes = parent.children.select{|n| n.is_a?(AST::Node)}
414
- nodes.each_with_index do |node, idx|
415
- if node.type == :block
416
- result.concat explicit_return_values_from_compound_statement(node.children[2])
417
- elsif node.type == :rescue
418
- # body statements
419
- result.concat from_value_position_statement(node.children[0])
420
- # rescue statements
421
- result.concat from_value_position_statement(node.children[1])
422
- elsif SKIPPABLE.include?(node.type)
423
- next
424
- elsif node.type == :resbody
425
- result.concat reduce_to_value_nodes([node.children[2]])
426
- elsif node.type == :return
427
- result.concat reduce_to_value_nodes([node.children[0]])
428
- # Return here because the rest of the code is
429
- # unreachable and shouldn't be looked at
430
- return result
431
- else
432
- result.concat explicit_return_values_from_compound_statement(node)
433
- end
434
- # handle last line of compound statements, which is in
435
- # value position. we already have the explicit values
436
- # from above; now we need to also gather the value
437
- # position nodes
438
- result.concat from_value_position_statement(nodes.last, include_explicit_returns: false) if idx == nodes.length - 1
439
- end
440
- result
441
- end
442
-
443
- private
444
-
445
- # Useful when this statement isn't in value position, but
446
- # we care explicit return statements nonetheless.
447
- #
448
- # @param parent [Parser::AST::Node]
449
- #
450
- # @return [Array<Parser::AST::Node>]
451
- def explicit_return_values_from_compound_statement parent
452
- return [] unless parent.is_a?(::Parser::AST::Node)
453
- result = []
454
- nodes = parent.children.select{|n| n.is_a?(::Parser::AST::Node)}
455
- nodes.each do |node|
456
- next if SKIPPABLE.include?(node.type)
457
- if node.type == :return
458
- result.concat reduce_to_value_nodes([node.children[0]])
459
- # Return the result here because the rest of the code is
460
- # unreachable
461
- return result
462
- else
463
- result.concat explicit_return_values_from_compound_statement(node)
464
- end
465
- end
466
- result
467
- end
468
-
469
- # @param nodes [Enumerable<Parser::AST::Node, BasicObject>]
470
- # @return [Array<Parser::AST::Node, nil>]
471
- def reduce_to_value_nodes nodes
472
- result = []
473
- nodes.each do |node|
474
- if !node.is_a?(::Parser::AST::Node)
475
- result.push nil
476
- elsif COMPOUND_STATEMENTS.include?(node.type)
477
- result.concat from_value_position_compound_statement(node)
478
- elsif CONDITIONAL_ALL_BUT_FIRST.include?(node.type)
479
- result.concat reduce_to_value_nodes(node.children[1..-1])
480
- elsif node.type == :return
481
- result.concat reduce_to_value_nodes([node.children[0]])
482
- elsif node.type == :or
483
- result.concat reduce_to_value_nodes(node.children)
484
- elsif node.type == :block
485
- result.concat explicit_return_values_from_compound_statement(node.children[2])
486
- elsif node.type == :resbody
487
- result.concat reduce_to_value_nodes([node.children[2]])
488
- else
489
- result.push node
490
- end
491
- end
492
- result
493
- end
494
- end
495
- end
496
- end
497
- end
498
- end
499
- end
1
+ # frozen_string_literal: true
2
+
3
+ require 'parser'
4
+ require 'ast'
5
+
6
+ # https://github.com/whitequark/parser
7
+ module Solargraph
8
+ module Parser
9
+ module ParserGem
10
+ module NodeMethods
11
+ module_function
12
+
13
+ # @param node [Parser::AST::Node]
14
+ # @return [String]
15
+ def unpack_name(node)
16
+ pack_name(node).join("::")
17
+ end
18
+
19
+ # @param node [Parser::AST::Node]
20
+ # @return [Array<String>]
21
+ def pack_name(node)
22
+ # @type [Array<String>]
23
+ parts = []
24
+ if node.is_a?(AST::Node)
25
+ node.children.each { |n|
26
+ if n.is_a?(AST::Node)
27
+ if n.type == :cbase
28
+ parts = [''] + pack_name(n)
29
+ elsif n.type == :const
30
+ parts += pack_name(n)
31
+ end
32
+ else
33
+ parts.push n unless n.nil?
34
+ end
35
+ }
36
+ end
37
+ parts
38
+ end
39
+
40
+ # @param node [Parser::AST::Node]
41
+ # @return [String, nil]
42
+ def infer_literal_node_type node
43
+ return nil unless node.is_a?(AST::Node)
44
+ if node.type == :str || node.type == :dstr
45
+ return '::String'
46
+ elsif node.type == :array
47
+ return '::Array'
48
+ elsif node.type == :nil
49
+ return '::NilClass'
50
+ elsif node.type == :hash
51
+ return '::Hash'
52
+ elsif node.type == :int
53
+ return '::Integer'
54
+ elsif node.type == :float
55
+ return '::Float'
56
+ elsif node.type == :sym || node.type == :dsym
57
+ return '::Symbol'
58
+ elsif node.type == :regexp
59
+ return '::Regexp'
60
+ elsif node.type == :irange
61
+ return '::Range'
62
+ elsif node.type == :true || node.type == :false
63
+ return '::Boolean'
64
+ # @todo Support `nil` keyword in types
65
+ # elsif node.type == :nil
66
+ # return 'NilClass'
67
+ end
68
+ nil
69
+ end
70
+
71
+ # @param node [Parser::AST::Node]
72
+ # @return [Position]
73
+ def get_node_start_position(node)
74
+ Position.new(node.loc.line, node.loc.column)
75
+ end
76
+
77
+ # @param node [Parser::AST::Node]
78
+ # @return [Position]
79
+ def get_node_end_position(node)
80
+ Position.new(node.loc.last_line, node.loc.last_column)
81
+ end
82
+
83
+ # @param node [Parser::AST::Node]
84
+ # @param signature [String]
85
+ #
86
+ # @return [String]
87
+ def drill_signature node, signature
88
+ return signature unless node.is_a?(AST::Node)
89
+ if node.type == :const or node.type == :cbase
90
+ unless node.children[0].nil?
91
+ signature += drill_signature(node.children[0], signature)
92
+ end
93
+ signature += '::' unless signature.empty?
94
+ signature += node.children[1].to_s
95
+ elsif node.type == :lvar or node.type == :ivar or node.type == :cvar
96
+ signature += '.' unless signature.empty?
97
+ signature += node.children[0].to_s
98
+ elsif node.type == :send
99
+ unless node.children[0].nil?
100
+ signature += drill_signature(node.children[0], signature)
101
+ end
102
+ signature += '.' unless signature.empty?
103
+ signature += node.children[1].to_s
104
+ end
105
+ signature
106
+ end
107
+
108
+ # @param node [Parser::AST::Node]
109
+ # @return [Hash{Symbol => Chain}]
110
+ def convert_hash node
111
+ return {} unless Parser.is_ast_node?(node)
112
+ return convert_hash(node.children[0]) if node.type == :kwsplat
113
+ return convert_hash(node.children[0]) if Parser.is_ast_node?(node.children[0]) && node.children[0].type == :kwsplat
114
+ return {} unless node.type == :hash
115
+ result = {}
116
+ node.children.each do |pair|
117
+ result[pair.children[0].children[0]] = Solargraph::Parser.chain(pair.children[1])
118
+ end
119
+ result
120
+ end
121
+
122
+ # @sg-ignore Wrong argument type for AST::Node.new: type expected AST::_ToSym, received :nil
123
+ NIL_NODE = ::Parser::AST::Node.new(:nil)
124
+
125
+ # @param node [Parser::AST::Node]
126
+ #
127
+ # @return [Array<Parser::AST::Node>]
128
+ def const_nodes_from node
129
+ return [] unless Parser.is_ast_node?(node)
130
+ result = []
131
+ if node.type == :const
132
+ result.push node
133
+ else
134
+ node.children.each { |child| result.concat const_nodes_from(child) }
135
+ end
136
+ result
137
+ end
138
+
139
+ # @param node [Parser::AST::Node]
140
+ def splatted_hash? node
141
+ Parser.is_ast_node?(node.children[0]) && node.children[0].type == :kwsplat
142
+ end
143
+
144
+ # @param node [Parser::AST::Node]
145
+ def splatted_call? node
146
+ return false unless Parser.is_ast_node?(node)
147
+ Parser.is_ast_node?(node.children[0]) && node.children[0].type == :kwsplat && node.children[0].children[0].type != :hash
148
+ end
149
+
150
+ # @param nodes [Enumerable<Parser::AST::Node>]
151
+ def any_splatted_call?(nodes)
152
+ nodes.any? { |n| splatted_call?(n) }
153
+ end
154
+
155
+ # @todo Temporarily here for testing. Move to Solargraph::Parser.
156
+ # @param node [Parser::AST::Node]
157
+ # @return [Array<Parser::AST::Node>]
158
+ def call_nodes_from node
159
+ return [] unless node.is_a?(::Parser::AST::Node)
160
+ result = []
161
+ if node.type == :block
162
+ result.push node
163
+ if Parser.is_ast_node?(node.children[0]) && node.children[0].children.length > 2
164
+ node.children[0].children[2..-1].each { |child| result.concat call_nodes_from(child) }
165
+ end
166
+ node.children[1..-1].each { |child| result.concat call_nodes_from(child) }
167
+ elsif node.type == :send
168
+ result.push node
169
+ result.concat call_nodes_from(node.children.first)
170
+ node.children[2..-1].each { |child| result.concat call_nodes_from(child) }
171
+ elsif [:super, :zsuper].include?(node.type)
172
+ result.push node
173
+ node.children.each { |child| result.concat call_nodes_from(child) }
174
+ else
175
+ node.children.each { |child| result.concat call_nodes_from(child) }
176
+ end
177
+ result
178
+ end
179
+
180
+ # Find all the nodes within the provided node that potentially return a
181
+ # value.
182
+ #
183
+ # The node parameter typically represents a method's logic, e.g., the
184
+ # second child (after the :args node) of a :def node. A simple one-line
185
+ # method would typically return itself, while a node with conditions
186
+ # would return the resulting node from each conditional branch. Nodes
187
+ # that follow a :return node are assumed to be unreachable. Nil values
188
+ # are converted to nil node types.
189
+ #
190
+ # @param node [Parser::AST::Node]
191
+ # @return [Array<Parser::AST::Node>]
192
+ def returns_from_method_body node
193
+ # @todo is the || NIL_NODE necessary?
194
+ # STDERR.puts("Evaluating expression: #{node.inspect}")
195
+ DeepInference.from_method_body(node).map { |n| n || NIL_NODE }
196
+ end
197
+
198
+ # @param node [Parser::AST::Node]
199
+ # @return [Array<AST::Node>] low-level value nodes in
200
+ # value position. Does not include explicit return
201
+ # statements
202
+ def value_position_nodes_only(node)
203
+ DeepInference.value_position_nodes_only(node).map { |n| n || NIL_NODE }
204
+ end
205
+
206
+ # @param cursor [Solargraph::Source::Cursor]
207
+ # @return [Parser::AST::Node, nil]
208
+ def find_recipient_node cursor
209
+ return repaired_find_recipient_node(cursor) if cursor.source.repaired? && cursor.source.code[cursor.offset - 1] == '('
210
+ source = cursor.source
211
+ position = cursor.position
212
+ offset = cursor.offset
213
+ tree = if source.synchronized?
214
+ match = source.code[0..offset-1].match(/,\s*\z/)
215
+ if match
216
+ source.tree_at(position.line, position.column - match[0].length)
217
+ else
218
+ source.tree_at(position.line, position.column)
219
+ end
220
+ else
221
+ source.tree_at(position.line, position.column - 1)
222
+ end
223
+ # @type [AST::Node, nil]
224
+ prev = nil
225
+ tree.each do |node|
226
+ if node.type == :send
227
+ args = node.children[2..-1]
228
+ if !args.empty?
229
+ return node if prev && args.include?(prev)
230
+ else
231
+ if source.synchronized?
232
+ return node if source.code[0..offset-1] =~ /\(\s*\z/ && source.code[offset..-1] =~ /^\s*\)/
233
+ else
234
+ return node if source.code[0..offset-1] =~ /\([^(]*\z/
235
+ end
236
+ end
237
+ end
238
+ prev = node
239
+ end
240
+ nil
241
+ end
242
+
243
+ # @param cursor [Solargraph::Source::Cursor]
244
+ # @return [Parser::AST::Node, nil]
245
+ def repaired_find_recipient_node cursor
246
+ cursor = cursor.source.cursor_at([cursor.position.line, cursor.position.column - 1])
247
+ node = cursor.source.tree_at(cursor.position.line, cursor.position.column).first
248
+ return node if node && node.type == :send
249
+ end
250
+
251
+ #
252
+ # Concepts:
253
+ #
254
+ # - statement - one single node in the AST. Generally used
255
+ # synonymously with how the Parser gem uses the term
256
+ # 'expression'. This may have side effects (e.g.,
257
+ # registering a method in the namespace, modifying
258
+ # variables or doing I/O). It may encapsulate multiple
259
+ # other statements (see compound statement).
260
+ #
261
+ # - value - something that can be assigned to a variable by
262
+ # evaluating a statement
263
+ #
264
+ # - value node - the 'lowest level' AST node whose return
265
+ # type is a subset of the value type of the overall
266
+ # statement. Might be a literal, a method call, etc - the
267
+ # goal is to find the lowest level node, which we can use
268
+ # Chains and Pins later on to determine the type of.
269
+ #
270
+ # e.g., if the node 'b ? 123 : 456' were a return value, we
271
+ # know the actual return values possible are 123 and 456,
272
+ # and can disregard the rest.
273
+ #
274
+ # - value type - the type representing the multiple possible
275
+ # values that can result from evaluation of the statement.
276
+ #
277
+ # - return type - the type describing the values a statement
278
+ # might evaluate to. When used with a method, the term
279
+ # describes the values that may result from the method
280
+ # being called, and includes explicit return statements
281
+ # within the method body's closure.
282
+ #
283
+ # - method body - a compound statement with parameters whose
284
+ # return value type must account both for the explicit
285
+ # 'return' statemnts as well as the final statements
286
+ # executed in any given control flow through the method.
287
+ #
288
+ # - explicit return statement - a statement which, when part of a
289
+ # method body, is a possible value of a call to that method -
290
+ # e.g., "return 123"
291
+ #
292
+ # - compound statement - a statement which can be expanded to
293
+ # be multiple statements in a row, executed in the context
294
+ # of a method which can be explicitly returned from.
295
+ #
296
+ # - value position - the positions in the AST where the
297
+ # return type of the statement would be one of the return
298
+ # types of any compound statements it is a part of. For a
299
+ # compound statement, the last of the child statements
300
+ # would be in return position. This concept can be applied
301
+ # recursively through e.g. conditionals to find a list of
302
+ # statements in value positions.
303
+ module DeepInference
304
+ class << self
305
+ CONDITIONAL_ALL_BUT_FIRST = [:if, :unless]
306
+ CONDITIONAL_ALL = [:or]
307
+ ONLY_ONE_CHILD = [:return]
308
+ FIRST_TWO_CHILDREN = [:rescue]
309
+ COMPOUND_STATEMENTS = [:begin, :kwbegin]
310
+ SKIPPABLE = [:def, :defs, :class, :sclass, :module]
311
+ FUNCTION_VALUE = [:block]
312
+ CASE_STATEMENT = [:case]
313
+
314
+ # @param node [AST::Node] a method body compound statement
315
+ # @param include_explicit_returns [Boolean] If true,
316
+ # include the value nodes of the parameter of the
317
+ # 'return' statements in the type returned.
318
+ # @return [Array<AST::Node>] low-level value nodes from
319
+ # both nodes in value position as well as explicit
320
+ # return statements in the method's closure.
321
+ def from_method_body node
322
+ from_value_position_statement(node, include_explicit_returns: true)
323
+ end
324
+
325
+ # @param node [AST::Node] an individual statement, to be
326
+ # evaluated outside the context of a containing method
327
+ # @return [Array<AST::Node>] low-level value nodes in
328
+ # value position. Does not include explicit return
329
+ # statements
330
+ def value_position_nodes_only(node)
331
+ from_value_position_statement(node, include_explicit_returns: false)
332
+ end
333
+
334
+ # Look at known control statements and use them to find
335
+ # more specific return nodes.
336
+ #
337
+ # @param node [Parser::AST::Node] Statement which is in
338
+ # value position for a method body
339
+ # @param include_explicit_returns [Boolean] If true,
340
+ # include the value nodes of the parameter of the
341
+ # 'return' statements in the type returned.
342
+ # @return [Array<Parser::AST::Node>]
343
+ def from_value_position_statement node, include_explicit_returns: true
344
+ # STDERR.puts("from_expression called on #{node.inspect}")
345
+ return [] unless node.is_a?(::Parser::AST::Node)
346
+ # @type [Array<Parser::AST::Node>]
347
+ result = []
348
+ if COMPOUND_STATEMENTS.include?(node.type)
349
+ result.concat from_value_position_compound_statement node
350
+ elsif CONDITIONAL_ALL_BUT_FIRST.include?(node.type)
351
+ result.concat reduce_to_value_nodes(node.children[1..-1])
352
+ # result.push NIL_NODE unless node.children[2]
353
+ elsif CONDITIONAL_ALL.include?(node.type)
354
+ result.concat reduce_to_value_nodes(node.children)
355
+ elsif ONLY_ONE_CHILD.include?(node.type)
356
+ result.concat reduce_to_value_nodes([node.children[0]])
357
+ elsif FIRST_TWO_CHILDREN.include?(node.type)
358
+ result.concat reduce_to_value_nodes([node.children[0], node.children[1]])
359
+ elsif FUNCTION_VALUE.include?(node.type)
360
+ # the block itself is a first class value that could be returned
361
+ result.push node
362
+ # @todo any explicit returns actually return from
363
+ # scope in which the proc is run. This asssumes
364
+ # that the function is executed here.
365
+ result.concat explicit_return_values_from_compound_statement(node.children[2]) if include_explicit_returns
366
+ elsif CASE_STATEMENT.include?(node.type)
367
+ node.children[1..-1].each do |cc|
368
+ if cc.nil?
369
+ result.push NIL_NODE
370
+ elsif cc.type == :when
371
+ result.concat reduce_to_value_nodes([cc.children.last])
372
+ else
373
+ # else clause in case
374
+ result.concat reduce_to_value_nodes([cc])
375
+ end
376
+ end
377
+ elsif node.type == :resbody
378
+ result.concat reduce_to_value_nodes([node.children[2]])
379
+ else
380
+ result.push node
381
+ end
382
+ result
383
+ end
384
+
385
+ # Treat parent as as a begin block and use the last node's
386
+ # return node plus any explicit return nodes' return nodes. e.g.,
387
+ #
388
+ # 123
389
+ # 456
390
+ # return 'a' if foo == bar
391
+ # 789
392
+ #
393
+ # would return 'a' and 789.
394
+ #
395
+ # @param parent [Parser::AST::Node]
396
+ #
397
+ # @return [Array<Parser::AST::Node>]
398
+ def from_value_position_compound_statement parent
399
+ result = []
400
+ nodes = parent.children.select{|n| n.is_a?(AST::Node)}
401
+ nodes.each_with_index do |node, idx|
402
+ if node.type == :block
403
+ result.concat explicit_return_values_from_compound_statement(node.children[2])
404
+ elsif node.type == :rescue
405
+ # body statements
406
+ result.concat from_value_position_statement(node.children[0])
407
+ # rescue statements
408
+ result.concat from_value_position_statement(node.children[1])
409
+ elsif SKIPPABLE.include?(node.type)
410
+ next
411
+ elsif node.type == :resbody
412
+ result.concat reduce_to_value_nodes([node.children[2]])
413
+ elsif node.type == :return
414
+ result.concat reduce_to_value_nodes([node.children[0]])
415
+ # Return here because the rest of the code is
416
+ # unreachable and shouldn't be looked at
417
+ return result
418
+ else
419
+ result.concat explicit_return_values_from_compound_statement(node)
420
+ end
421
+ # handle last line of compound statements, which is in
422
+ # value position. we already have the explicit values
423
+ # from above; now we need to also gather the value
424
+ # position nodes
425
+ result.concat from_value_position_statement(nodes.last, include_explicit_returns: false) if idx == nodes.length - 1
426
+ end
427
+ result
428
+ end
429
+
430
+ private
431
+
432
+ # Useful when this statement isn't in value position, but
433
+ # we care explicit return statements nonetheless.
434
+ #
435
+ # @param parent [Parser::AST::Node]
436
+ #
437
+ # @return [Array<Parser::AST::Node>]
438
+ def explicit_return_values_from_compound_statement parent
439
+ return [] unless parent.is_a?(::Parser::AST::Node)
440
+ result = []
441
+ nodes = parent.children.select{|n| n.is_a?(::Parser::AST::Node)}
442
+ nodes.each do |node|
443
+ next if SKIPPABLE.include?(node.type)
444
+ if node.type == :return
445
+ result.concat reduce_to_value_nodes([node.children[0]])
446
+ # Return the result here because the rest of the code is
447
+ # unreachable
448
+ return result
449
+ else
450
+ result.concat explicit_return_values_from_compound_statement(node)
451
+ end
452
+ end
453
+ result
454
+ end
455
+
456
+ # @param nodes [Enumerable<Parser::AST::Node, BasicObject>]
457
+ # @return [Array<Parser::AST::Node, nil>]
458
+ def reduce_to_value_nodes nodes
459
+ result = []
460
+ nodes.each do |node|
461
+ if !node.is_a?(::Parser::AST::Node)
462
+ result.push nil
463
+ elsif COMPOUND_STATEMENTS.include?(node.type)
464
+ result.concat from_value_position_compound_statement(node)
465
+ elsif CONDITIONAL_ALL_BUT_FIRST.include?(node.type)
466
+ result.concat reduce_to_value_nodes(node.children[1..-1])
467
+ elsif node.type == :return
468
+ result.concat reduce_to_value_nodes([node.children[0]])
469
+ elsif node.type == :or
470
+ result.concat reduce_to_value_nodes(node.children)
471
+ elsif node.type == :block
472
+ result.concat explicit_return_values_from_compound_statement(node.children[2])
473
+ elsif node.type == :resbody
474
+ result.concat reduce_to_value_nodes([node.children[2]])
475
+ else
476
+ result.push node
477
+ end
478
+ end
479
+ result
480
+ end
481
+ end
482
+ end
483
+ end
484
+ end
485
+ end
486
+ end