solargraph 0.51.2 → 0.53.0

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 (162) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/plugins.yml +40 -0
  3. data/.github/workflows/rspec.yml +1 -3
  4. data/.github/workflows/typecheck.yml +34 -0
  5. data/.yardopts +2 -2
  6. data/CHANGELOG.md +55 -5
  7. data/README.md +13 -16
  8. data/SPONSORS.md +1 -7
  9. data/lib/solargraph/api_map/cache.rb +60 -20
  10. data/lib/solargraph/api_map/store.rb +47 -11
  11. data/lib/solargraph/api_map.rb +161 -95
  12. data/lib/solargraph/bench.rb +2 -2
  13. data/lib/solargraph/cache.rb +29 -5
  14. data/lib/solargraph/complex_type/type_methods.rb +54 -9
  15. data/lib/solargraph/complex_type/unique_type.rb +155 -58
  16. data/lib/solargraph/complex_type.rb +73 -16
  17. data/lib/solargraph/convention.rb +0 -1
  18. data/lib/solargraph/converters/dd.rb +5 -0
  19. data/lib/solargraph/converters/dl.rb +3 -0
  20. data/lib/solargraph/converters/dt.rb +3 -0
  21. data/lib/solargraph/diagnostics/rubocop.rb +8 -7
  22. data/lib/solargraph/diagnostics/rubocop_helpers.rb +1 -0
  23. data/lib/solargraph/diagnostics/type_check.rb +1 -0
  24. data/lib/solargraph/diagnostics.rb +2 -2
  25. data/lib/solargraph/doc_map.rb +146 -0
  26. data/lib/solargraph/gem_pins.rb +64 -0
  27. data/lib/solargraph/language_server/host/cataloger.rb +1 -0
  28. data/lib/solargraph/language_server/host/diagnoser.rb +2 -2
  29. data/lib/solargraph/language_server/host/dispatch.rb +10 -4
  30. data/lib/solargraph/language_server/host/message_worker.rb +4 -0
  31. data/lib/solargraph/language_server/host/sources.rb +7 -4
  32. data/lib/solargraph/language_server/host.rb +15 -6
  33. data/lib/solargraph/language_server/message/completion_item/resolve.rb +3 -1
  34. data/lib/solargraph/language_server/message/extended/check_gem_version.rb +13 -1
  35. data/lib/solargraph/language_server/message/initialize.rb +5 -2
  36. data/lib/solargraph/language_server/message/text_document/hover.rb +2 -0
  37. data/lib/solargraph/language_server/message/text_document.rb +0 -1
  38. data/lib/solargraph/language_server/message/workspace/did_change_configuration.rb +5 -0
  39. data/lib/solargraph/language_server/transport/adapter.rb +16 -1
  40. data/lib/solargraph/language_server/transport/data_reader.rb +2 -0
  41. data/lib/solargraph/library.rb +70 -16
  42. data/lib/solargraph/location.rb +1 -0
  43. data/lib/solargraph/parser/comment_ripper.rb +4 -0
  44. data/lib/solargraph/parser/node_methods.rb +47 -7
  45. data/lib/solargraph/parser/node_processor/base.rb +9 -0
  46. data/lib/solargraph/parser/{legacy → parser_gem}/class_methods.rb +31 -5
  47. data/lib/solargraph/parser/{legacy → parser_gem}/flawed_builder.rb +3 -1
  48. data/lib/solargraph/parser/{legacy → parser_gem}/node_chainer.rb +49 -36
  49. data/lib/solargraph/parser/parser_gem/node_methods.rb +499 -0
  50. data/lib/solargraph/parser/{rubyvm → parser_gem}/node_processors/alias_node.rb +1 -1
  51. data/lib/solargraph/parser/{legacy → parser_gem}/node_processors/args_node.rb +4 -1
  52. data/lib/solargraph/parser/{rubyvm → parser_gem}/node_processors/begin_node.rb +1 -1
  53. data/lib/solargraph/parser/{legacy → parser_gem}/node_processors/block_node.rb +3 -2
  54. data/lib/solargraph/parser/{legacy → parser_gem}/node_processors/casgn_node.rb +2 -2
  55. data/lib/solargraph/parser/{legacy → parser_gem}/node_processors/cvasgn_node.rb +1 -1
  56. data/lib/solargraph/parser/{legacy → parser_gem}/node_processors/def_node.rb +1 -1
  57. data/lib/solargraph/parser/{legacy → parser_gem}/node_processors/defs_node.rb +2 -2
  58. data/lib/solargraph/parser/{legacy → parser_gem}/node_processors/gvasgn_node.rb +1 -1
  59. data/lib/solargraph/parser/{legacy → parser_gem}/node_processors/ivasgn_node.rb +2 -2
  60. data/lib/solargraph/parser/{legacy → parser_gem}/node_processors/lvasgn_node.rb +2 -2
  61. data/lib/solargraph/parser/{legacy → parser_gem}/node_processors/namespace_node.rb +2 -2
  62. data/lib/solargraph/parser/{legacy → parser_gem}/node_processors/orasgn_node.rb +1 -1
  63. data/lib/solargraph/parser/{legacy → parser_gem}/node_processors/resbody_node.rb +3 -3
  64. data/lib/solargraph/parser/{legacy → parser_gem}/node_processors/sclass_node.rb +1 -1
  65. data/lib/solargraph/parser/{legacy → parser_gem}/node_processors/send_node.rb +2 -2
  66. data/lib/solargraph/parser/{rubyvm → parser_gem}/node_processors/sym_node.rb +1 -1
  67. data/lib/solargraph/parser/parser_gem/node_processors.rb +54 -0
  68. data/lib/solargraph/parser/parser_gem.rb +12 -0
  69. data/lib/solargraph/parser/region.rb +1 -1
  70. data/lib/solargraph/parser/snippet.rb +2 -0
  71. data/lib/solargraph/parser.rb +8 -9
  72. data/lib/solargraph/pin/base.rb +64 -9
  73. data/lib/solargraph/pin/base_variable.rb +6 -2
  74. data/lib/solargraph/pin/block.rb +13 -8
  75. data/lib/solargraph/pin/closure.rb +17 -2
  76. data/lib/solargraph/pin/common.rb +7 -3
  77. data/lib/solargraph/pin/conversions.rb +33 -3
  78. data/lib/solargraph/pin/delegated_method.rb +1 -1
  79. data/lib/solargraph/pin/documenting.rb +25 -34
  80. data/lib/solargraph/pin/instance_variable.rb +4 -0
  81. data/lib/solargraph/pin/local_variable.rb +13 -1
  82. data/lib/solargraph/pin/method.rb +169 -18
  83. data/lib/solargraph/pin/namespace.rb +18 -5
  84. data/lib/solargraph/pin/parameter.rb +44 -14
  85. data/lib/solargraph/pin/reference/override.rb +2 -2
  86. data/lib/solargraph/pin/reference.rb +8 -0
  87. data/lib/solargraph/pin/search.rb +3 -3
  88. data/lib/solargraph/pin/signature.rb +123 -3
  89. data/lib/solargraph/pin.rb +0 -1
  90. data/lib/solargraph/range.rb +2 -2
  91. data/lib/solargraph/rbs_map/conversions.rb +287 -45
  92. data/lib/solargraph/rbs_map/core_fills.rb +6 -29
  93. data/lib/solargraph/rbs_map/core_map.rb +2 -1
  94. data/lib/solargraph/rbs_map/core_signs.rb +2 -0
  95. data/lib/solargraph/rbs_map/stdlib_map.rb +2 -8
  96. data/lib/solargraph/rbs_map.rb +20 -11
  97. data/lib/solargraph/shell.rb +62 -59
  98. data/lib/solargraph/source/chain/array.rb +32 -0
  99. data/lib/solargraph/source/chain/block_symbol.rb +13 -0
  100. data/lib/solargraph/source/chain/call.rb +99 -46
  101. data/lib/solargraph/source/chain/constant.rb +15 -1
  102. data/lib/solargraph/source/chain/if.rb +23 -0
  103. data/lib/solargraph/source/chain/link.rb +8 -2
  104. data/lib/solargraph/source/chain/or.rb +1 -1
  105. data/lib/solargraph/source/chain/z_super.rb +3 -3
  106. data/lib/solargraph/source/chain.rb +29 -14
  107. data/lib/solargraph/source/change.rb +3 -0
  108. data/lib/solargraph/source/cursor.rb +2 -0
  109. data/lib/solargraph/source/source_chainer.rb +8 -5
  110. data/lib/solargraph/source.rb +18 -19
  111. data/lib/solargraph/source_map/clip.rb +11 -23
  112. data/lib/solargraph/source_map/mapper.rb +12 -1
  113. data/lib/solargraph/source_map.rb +15 -5
  114. data/lib/solargraph/type_checker/checks.rb +10 -2
  115. data/lib/solargraph/type_checker.rb +92 -26
  116. data/lib/solargraph/version.rb +1 -1
  117. data/lib/solargraph/workspace/config.rb +8 -6
  118. data/lib/solargraph/workspace.rb +3 -2
  119. data/lib/solargraph/yard_map/cache.rb +6 -0
  120. data/lib/solargraph/yard_map/helpers.rb +1 -1
  121. data/lib/solargraph/yard_map/mapper/to_method.rb +11 -1
  122. data/lib/solargraph/yard_map/mapper.rb +1 -1
  123. data/lib/solargraph/yard_map/to_method.rb +11 -4
  124. data/lib/solargraph/yard_map.rb +1 -292
  125. data/lib/solargraph/yard_tags.rb +20 -0
  126. data/lib/solargraph/yardoc.rb +52 -0
  127. data/lib/solargraph.rb +6 -4
  128. data/solargraph.gemspec +3 -2
  129. metadata +51 -58
  130. data/lib/solargraph/api_map/bundler_methods.rb +0 -22
  131. data/lib/solargraph/documentor.rb +0 -76
  132. data/lib/solargraph/parser/legacy/node_methods.rb +0 -325
  133. data/lib/solargraph/parser/legacy/node_processors/alias_node.rb +0 -23
  134. data/lib/solargraph/parser/legacy/node_processors/begin_node.rb +0 -15
  135. data/lib/solargraph/parser/legacy/node_processors/sym_node.rb +0 -18
  136. data/lib/solargraph/parser/legacy/node_processors.rb +0 -55
  137. data/lib/solargraph/parser/legacy.rb +0 -12
  138. data/lib/solargraph/parser/rubyvm/class_methods.rb +0 -153
  139. data/lib/solargraph/parser/rubyvm/node_chainer.rb +0 -160
  140. data/lib/solargraph/parser/rubyvm/node_methods.rb +0 -317
  141. data/lib/solargraph/parser/rubyvm/node_processors/args_node.rb +0 -85
  142. data/lib/solargraph/parser/rubyvm/node_processors/block_node.rb +0 -42
  143. data/lib/solargraph/parser/rubyvm/node_processors/casgn_node.rb +0 -33
  144. data/lib/solargraph/parser/rubyvm/node_processors/cvasgn_node.rb +0 -23
  145. data/lib/solargraph/parser/rubyvm/node_processors/def_node.rb +0 -75
  146. data/lib/solargraph/parser/rubyvm/node_processors/defs_node.rb +0 -68
  147. data/lib/solargraph/parser/rubyvm/node_processors/gvasgn_node.rb +0 -23
  148. data/lib/solargraph/parser/rubyvm/node_processors/ivasgn_node.rb +0 -38
  149. data/lib/solargraph/parser/rubyvm/node_processors/kw_arg_node.rb +0 -39
  150. data/lib/solargraph/parser/rubyvm/node_processors/lit_node.rb +0 -20
  151. data/lib/solargraph/parser/rubyvm/node_processors/lvasgn_node.rb +0 -27
  152. data/lib/solargraph/parser/rubyvm/node_processors/namespace_node.rb +0 -39
  153. data/lib/solargraph/parser/rubyvm/node_processors/opt_arg_node.rb +0 -26
  154. data/lib/solargraph/parser/rubyvm/node_processors/orasgn_node.rb +0 -15
  155. data/lib/solargraph/parser/rubyvm/node_processors/resbody_node.rb +0 -51
  156. data/lib/solargraph/parser/rubyvm/node_processors/sclass_node.rb +0 -32
  157. data/lib/solargraph/parser/rubyvm/node_processors/scope_node.rb +0 -15
  158. data/lib/solargraph/parser/rubyvm/node_processors/send_node.rb +0 -279
  159. data/lib/solargraph/parser/rubyvm/node_processors.rb +0 -64
  160. data/lib/solargraph/parser/rubyvm/node_wrapper.rb +0 -47
  161. data/lib/solargraph/parser/rubyvm.rb +0 -40
  162. data/lib/yard-solargraph.rb +0 -33
@@ -1,76 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'bundler'
4
- require 'json'
5
- require 'open3'
6
- require 'shellwords'
7
- require 'yard'
8
- require 'fileutils'
9
-
10
- module Solargraph
11
- class Documentor
12
- RDOC_GEMS = %w[
13
- actioncable actionmailbox actionmailer actionpack actiontext actionview
14
- activejob activemodel activerecord activestorage activesupport railties
15
- ]
16
-
17
- def initialize directory, rebuild: false, out: File.new(File::NULL, 'w')
18
- @directory = directory
19
- @rebuild = rebuild
20
- @out = out
21
- end
22
-
23
- # @return [Boolean] True if all specs were found and documented.
24
- def document
25
- failures = 0
26
- Documentor.specs_from_bundle(@directory).each_pair do |name, version|
27
- yd = YARD::Registry.yardoc_file_for_gem(name, "= #{version}")
28
- if !yd || @rebuild
29
- FileUtils.safe_unlink File.join(YardMap::CoreDocs.cache_dir, 'gems', "#{name}-#{version}.ser")
30
- @out.puts "Documenting #{name} #{version}"
31
- `yard gems #{name} #{version} #{@rebuild ? '--rebuild' : ''}`
32
- yd = YARD::Registry.yardoc_file_for_gem(name, "= #{version}")
33
- # HACK: Ignore errors documenting bundler
34
- if !yd && name != 'bundler'
35
- @out.puts "#{name} #{version} YARD documentation failed"
36
- failures += 1
37
- end
38
- end
39
- if yd && RDOC_GEMS.include?(name)
40
- cache = File.join(Solargraph::YardMap::CoreDocs.cache_dir, 'gems', "#{name}-#{version}", 'yardoc')
41
- if !File.exist?(cache) || @rebuild
42
- @out.puts "Caching custom documentation for #{name} #{version}"
43
- spec = Gem::Specification.find_by_name(name, "= #{version}")
44
- Solargraph::YardMap::RdocToYard.run(spec)
45
- end
46
- end
47
- end
48
- if failures > 0
49
- @out.puts "#{failures} gem#{failures == 1 ? '' : 's'} could not be documented. You might need to run `bundle install`."
50
- end
51
- failures == 0
52
- rescue Solargraph::BundleNotFoundError => e
53
- @out.puts "[#{e.class}] #{e.message}"
54
- @out.puts "No bundled gems are available in #{@directory}"
55
- false
56
- end
57
-
58
- # @param directory [String]
59
- # @return [Hash]
60
- def self.specs_from_bundle directory
61
- Solargraph.with_clean_env do
62
- cmd = [
63
- 'ruby', '-e',
64
- "require 'bundler'; require 'json'; Dir.chdir('#{directory}') { puts Bundler.definition.specs_for([:default]).map { |spec| [spec.name, spec.version] }.to_h.to_json }"
65
- ]
66
- o, e, s = Open3.capture3(*cmd)
67
- if s.success?
68
- o && !o.empty? ? JSON.parse(o.split("\n").last) : {}
69
- else
70
- Solargraph.logger.warn e
71
- raise BundleNotFoundError, "Failed to load gems from bundle at #{directory}"
72
- end
73
- end
74
- end
75
- end
76
- end
@@ -1,325 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'parser'
4
-
5
- module Solargraph
6
- module Parser
7
- module Legacy
8
- module NodeMethods
9
- module_function
10
-
11
- # @param node [Parser::AST::Node]
12
- # @return [String]
13
- def unpack_name(node)
14
- pack_name(node).join("::")
15
- end
16
-
17
- # @param node [Parser::AST::Node]
18
- # @return [Array<String>]
19
- def pack_name(node)
20
- parts = []
21
- if node.is_a?(AST::Node)
22
- node.children.each { |n|
23
- if n.is_a?(AST::Node)
24
- if n.type == :cbase
25
- parts = [''] + pack_name(n)
26
- else
27
- parts += pack_name(n)
28
- end
29
- else
30
- parts.push n unless n.nil?
31
- end
32
- }
33
- end
34
- parts
35
- end
36
-
37
- # @param node [Parser::AST::Node]
38
- # @return [String, nil]
39
- def infer_literal_node_type node
40
- return nil unless node.is_a?(AST::Node)
41
- if node.type == :str || node.type == :dstr
42
- return '::String'
43
- elsif node.type == :array
44
- return '::Array'
45
- elsif node.type == :hash
46
- return '::Hash'
47
- elsif node.type == :int
48
- return '::Integer'
49
- elsif node.type == :float
50
- return '::Float'
51
- elsif node.type == :sym
52
- return '::Symbol'
53
- elsif node.type == :regexp
54
- return '::Regexp'
55
- elsif node.type == :irange
56
- return '::Range'
57
- elsif node.type == :true || node.type == :false
58
- return '::Boolean'
59
- # @todo Support `nil` keyword in types
60
- # elsif node.type == :nil
61
- # return 'NilClass'
62
- end
63
- nil
64
- end
65
-
66
- # @param node [Parser::AST::Node]
67
- # @return [Position]
68
- def get_node_start_position(node)
69
- Position.new(node.loc.line, node.loc.column)
70
- end
71
-
72
- # @param node [Parser::AST::Node]
73
- # @return [Position]
74
- def get_node_end_position(node)
75
- Position.new(node.loc.last_line, node.loc.last_column)
76
- end
77
-
78
- def drill_signature node, signature
79
- return signature unless node.is_a?(AST::Node)
80
- if node.type == :const or node.type == :cbase
81
- unless node.children[0].nil?
82
- signature += drill_signature(node.children[0], signature)
83
- end
84
- signature += '::' unless signature.empty?
85
- signature += node.children[1].to_s
86
- elsif node.type == :lvar or node.type == :ivar or node.type == :cvar
87
- signature += '.' unless signature.empty?
88
- signature += node.children[0].to_s
89
- elsif node.type == :send
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
- end
96
- signature
97
- end
98
-
99
- def convert_hash node
100
- return {} unless Parser.is_ast_node?(node)
101
- return convert_hash(node.children[0]) if node.type == :kwsplat
102
- return convert_hash(node.children[0]) if Parser.is_ast_node?(node.children[0]) && node.children[0].type == :kwsplat
103
- return {} unless node.type == :hash
104
- result = {}
105
- node.children.each do |pair|
106
- result[pair.children[0].children[0]] = Solargraph::Parser.chain(pair.children[1])
107
- end
108
- result
109
- end
110
-
111
- NIL_NODE = ::Parser::AST::Node.new(:nil)
112
-
113
- def const_nodes_from node
114
- return [] unless Parser.is_ast_node?(node)
115
- result = []
116
- if node.type == :const
117
- result.push node
118
- else
119
- node.children.each { |child| result.concat const_nodes_from(child) }
120
- end
121
- result
122
- end
123
-
124
- def splatted_hash? node
125
- Parser.is_ast_node?(node.children[0]) && node.children[0].type == :kwsplat
126
- end
127
-
128
- def splatted_call? node
129
- return false unless Parser.is_ast_node?(node)
130
- Parser.is_ast_node?(node.children[0]) && node.children[0].type == :kwsplat && node.children[0].children[0].type != :hash
131
- end
132
-
133
- def any_splatted_call?(nodes)
134
- nodes.any? { |n| splatted_call?(n) }
135
- end
136
-
137
- # @todo Temporarily here for testing. Move to Solargraph::Parser.
138
- def call_nodes_from node
139
- return [] unless node.is_a?(::Parser::AST::Node)
140
- result = []
141
- if node.type == :block
142
- result.push node
143
- if Parser.is_ast_node?(node.children[0]) && node.children[0].children.length > 2
144
- node.children[0].children[2..-1].each { |child| result.concat call_nodes_from(child) }
145
- end
146
- node.children[1..-1].each { |child| result.concat call_nodes_from(child) }
147
- elsif node.type == :send
148
- result.push node
149
- node.children[2..-1].each { |child| result.concat call_nodes_from(child) }
150
- elsif [:super, :zsuper].include?(node.type)
151
- result.push node
152
- node.children.each { |child| result.concat call_nodes_from(child) }
153
- elsif node.type == :masgn
154
- # @todo We're treating a mass assignment as a call node, but the
155
- # type checker still needs the logic to handle it.
156
- result.push node
157
- else
158
- node.children.each { |child| result.concat call_nodes_from(child) }
159
- end
160
- result
161
- end
162
-
163
- # Find all the nodes within the provided node that potentially return a
164
- # value.
165
- #
166
- # The node parameter typically represents a method's logic, e.g., the
167
- # second child (after the :args node) of a :def node. A simple one-line
168
- # method would typically return itself, while a node with conditions
169
- # would return the resulting node from each conditional branch. Nodes
170
- # that follow a :return node are assumed to be unreachable. Nil values
171
- # are converted to nil node types.
172
- #
173
- # @param node [Parser::AST::Node]
174
- # @return [Array<Parser::AST::Node>]
175
- def returns_from node
176
- DeepInference.get_return_nodes(node).map { |n| n || NIL_NODE }
177
- end
178
-
179
- # @param cursor [Solargraph::Source::Cursor]
180
- def find_recipient_node cursor
181
- return repaired_find_recipient_node(cursor) if cursor.source.repaired? && cursor.source.code[cursor.offset - 1] == '('
182
- source = cursor.source
183
- position = cursor.position
184
- offset = cursor.offset
185
- tree = if source.synchronized?
186
- match = source.code[0..offset-1].match(/,\s*\z/)
187
- if match
188
- source.tree_at(position.line, position.column - match[0].length)
189
- else
190
- source.tree_at(position.line, position.column)
191
- end
192
- else
193
- source.tree_at(position.line, position.column - 1)
194
- end
195
- prev = nil
196
- tree.each do |node|
197
- if node.type == :send
198
- args = node.children[2..-1]
199
- if !args.empty?
200
- return node if prev && args.include?(prev)
201
- else
202
- if source.synchronized?
203
- return node if source.code[0..offset-1] =~ /\(\s*\z/ && source.code[offset..-1] =~ /^\s*\)/
204
- else
205
- return node if source.code[0..offset-1] =~ /\([^\(]*\z/
206
- end
207
- end
208
- end
209
- prev = node
210
- end
211
- nil
212
- end
213
-
214
- def repaired_find_recipient_node cursor
215
- cursor = cursor.source.cursor_at([cursor.position.line, cursor.position.column - 1])
216
- node = cursor.source.tree_at(cursor.position.line, cursor.position.column).first
217
- return node if node && node.type == :send
218
- end
219
-
220
- module DeepInference
221
- class << self
222
- CONDITIONAL = [:if, :unless]
223
- REDUCEABLE = [:begin, :kwbegin]
224
- SKIPPABLE = [:def, :defs, :class, :sclass, :module]
225
-
226
- # @param node [Parser::AST::Node]
227
- # @return [Array<Parser::AST::Node>]
228
- def get_return_nodes node
229
- return [] unless node.is_a?(::Parser::AST::Node)
230
- result = []
231
- if REDUCEABLE.include?(node.type)
232
- result.concat get_return_nodes_from_children(node)
233
- elsif CONDITIONAL.include?(node.type)
234
- result.concat reduce_to_value_nodes(node.children[1..-1])
235
- # result.push NIL_NODE unless node.children[2]
236
- elsif node.type == :or
237
- result.concat reduce_to_value_nodes(node.children)
238
- elsif node.type == :return
239
- result.concat reduce_to_value_nodes([node.children[0]])
240
- elsif node.type == :block
241
- result.push node
242
- result.concat get_return_nodes_only(node.children[2])
243
- elsif node.type == :case
244
- node.children[1..-1].each do |cc|
245
- if cc.nil?
246
- result.push NIL_NODE
247
- else
248
- result.concat reduce_to_value_nodes(cc.children[1..-2]) unless cc.children.length < 1
249
- result.concat reduce_to_value_nodes([cc.children.last])
250
- end
251
- end
252
- else
253
- result.push node
254
- end
255
- result
256
- end
257
-
258
- private
259
-
260
- def get_return_nodes_from_children parent
261
- result = []
262
- nodes = parent.children.select{|n| n.is_a?(AST::Node)}
263
- nodes.each_with_index do |node, idx|
264
- if node.type == :block
265
- result.concat get_return_nodes_only(node.children[2])
266
- elsif SKIPPABLE.include?(node.type)
267
- next
268
- elsif node.type == :return
269
- result.concat reduce_to_value_nodes([node.children[0]])
270
- # Return the result here because the rest of the code is
271
- # unreachable
272
- return result
273
- else
274
- result.concat get_return_nodes_only(node)
275
- end
276
- result.concat reduce_to_value_nodes([nodes.last]) if idx == nodes.length - 1
277
- end
278
- result
279
- end
280
-
281
- def get_return_nodes_only parent
282
- return [] unless parent.is_a?(::Parser::AST::Node)
283
- result = []
284
- nodes = parent.children.select{|n| n.is_a?(::Parser::AST::Node)}
285
- nodes.each do |node|
286
- next if SKIPPABLE.include?(node.type)
287
- if node.type == :return
288
- result.concat reduce_to_value_nodes([node.children[0]])
289
- # Return the result here because the rest of the code is
290
- # unreachable
291
- return result
292
- else
293
- result.concat get_return_nodes_only(node)
294
- end
295
- end
296
- result
297
- end
298
-
299
- def reduce_to_value_nodes nodes
300
- result = []
301
- nodes.each do |node|
302
- if !node.is_a?(::Parser::AST::Node)
303
- result.push nil
304
- elsif REDUCEABLE.include?(node.type)
305
- result.concat get_return_nodes_from_children(node)
306
- elsif CONDITIONAL.include?(node.type)
307
- result.concat reduce_to_value_nodes(node.children[1..-1])
308
- elsif node.type == :return
309
- result.concat reduce_to_value_nodes([node.children[0]])
310
- elsif node.type == :or
311
- result.concat reduce_to_value_nodes(node.children)
312
- elsif node.type == :block
313
- result.concat get_return_nodes_only(node.children[2])
314
- else
315
- result.push node
316
- end
317
- end
318
- result
319
- end
320
- end
321
- end
322
- end
323
- end
324
- end
325
- end
@@ -1,23 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Solargraph
4
- module Parser
5
- module Legacy
6
- module NodeProcessors
7
- class AliasNode < Parser::NodeProcessor::Base
8
- def process
9
- loc = get_node_location(node)
10
- pins.push Solargraph::Pin::MethodAlias.new(
11
- location: loc,
12
- closure: region.closure,
13
- name: node.children[0].children[0].to_s,
14
- original: node.children[1].children[0].to_s,
15
- scope: region.scope || :instance
16
- )
17
- process_children
18
- end
19
- end
20
- end
21
- end
22
- end
23
- end
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Solargraph
4
- module Parser
5
- module Legacy
6
- module NodeProcessors
7
- class BeginNode < Parser::NodeProcessor::Base
8
- def process
9
- process_children
10
- end
11
- end
12
- end
13
- end
14
- end
15
- end
@@ -1,18 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Solargraph
4
- module Parser
5
- module Legacy
6
- module NodeProcessors
7
- class SymNode < Parser::NodeProcessor::Base
8
- def process
9
- pins.push Solargraph::Pin::Symbol.new(
10
- get_node_location(node),
11
- ":#{node.children[0]}"
12
- )
13
- end
14
- end
15
- end
16
- end
17
- end
18
- end
@@ -1,55 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'solargraph/parser/node_processor'
4
-
5
- module Solargraph
6
- module Parser
7
- module Legacy
8
- module NodeProcessors
9
- autoload :BeginNode, 'solargraph/parser/legacy/node_processors/begin_node'
10
- autoload :DefNode, 'solargraph/parser/legacy/node_processors/def_node'
11
- autoload :DefsNode, 'solargraph/parser/legacy/node_processors/defs_node'
12
- autoload :SendNode, 'solargraph/parser/legacy/node_processors/send_node'
13
- autoload :NamespaceNode, 'solargraph/parser/legacy/node_processors/namespace_node'
14
- autoload :SclassNode, 'solargraph/parser/legacy/node_processors/sclass_node'
15
- autoload :ModuleNode, 'solargraph/parser/legacy/node_processors/module_node'
16
- autoload :IvasgnNode, 'solargraph/parser/legacy/node_processors/ivasgn_node'
17
- autoload :CvasgnNode, 'solargraph/parser/legacy/node_processors/cvasgn_node'
18
- autoload :LvasgnNode, 'solargraph/parser/legacy/node_processors/lvasgn_node'
19
- autoload :GvasgnNode, 'solargraph/parser/legacy/node_processors/gvasgn_node'
20
- autoload :CasgnNode, 'solargraph/parser/legacy/node_processors/casgn_node'
21
- autoload :AliasNode, 'solargraph/parser/legacy/node_processors/alias_node'
22
- autoload :ArgsNode, 'solargraph/parser/legacy/node_processors/args_node'
23
- autoload :BlockNode, 'solargraph/parser/legacy/node_processors/block_node'
24
- autoload :OrasgnNode, 'solargraph/parser/legacy/node_processors/orasgn_node'
25
- autoload :SymNode, 'solargraph/parser/legacy/node_processors/sym_node'
26
- autoload :ResbodyNode, 'solargraph/parser/legacy/node_processors/resbody_node'
27
- end
28
- end
29
-
30
- module NodeProcessor
31
- register :source, Legacy::NodeProcessors::BeginNode
32
- register :begin, Legacy::NodeProcessors::BeginNode
33
- register :kwbegin, Legacy::NodeProcessors::BeginNode
34
- register :rescue, Legacy::NodeProcessors::BeginNode
35
- register :resbody, Legacy::NodeProcessors::ResbodyNode
36
- register :def, Legacy::NodeProcessors::DefNode
37
- register :defs, Legacy::NodeProcessors::DefsNode
38
- register :send, Legacy::NodeProcessors::SendNode
39
- register :class, Legacy::NodeProcessors::NamespaceNode
40
- register :module, Legacy::NodeProcessors::NamespaceNode
41
- register :sclass, Legacy::NodeProcessors::SclassNode
42
- register :ivasgn, Legacy::NodeProcessors::IvasgnNode
43
- register :cvasgn, Legacy::NodeProcessors::CvasgnNode
44
- register :lvasgn, Legacy::NodeProcessors::LvasgnNode
45
- register :gvasgn, Legacy::NodeProcessors::GvasgnNode
46
- register :casgn, Legacy::NodeProcessors::CasgnNode
47
- register :alias, Legacy::NodeProcessors::AliasNode
48
- register :args, Legacy::NodeProcessors::ArgsNode
49
- register :forward_args, Legacy::NodeProcessors::ArgsNode
50
- register :block, Legacy::NodeProcessors::BlockNode
51
- register :or_asgn, Legacy::NodeProcessors::OrasgnNode
52
- register :sym, Legacy::NodeProcessors::SymNode
53
- end
54
- end
55
- end
@@ -1,12 +0,0 @@
1
- module Solargraph
2
- module Parser
3
- module Legacy
4
- autoload :FlawedBuilder, 'solargraph/parser/legacy/flawed_builder'
5
- autoload :ClassMethods, 'solargraph/parser/legacy/class_methods'
6
- autoload :NodeMethods, 'solargraph/parser/legacy/node_methods'
7
- autoload :NodeChainer, 'solargraph/parser/legacy/node_chainer'
8
- end
9
- end
10
- end
11
-
12
- require 'solargraph/parser/legacy/node_processors'
@@ -1,153 +0,0 @@
1
- require 'solargraph/parser/rubyvm/node_processors'
2
- require 'solargraph/parser/rubyvm/node_wrapper'
3
-
4
- module Solargraph
5
- module Parser
6
- module Rubyvm
7
- module ClassMethods
8
- # @param code [String]
9
- # @param filename [String]
10
- # @return [Array(Parser::AST::Node, Array<Parser::Source::Comment>)]
11
- # @sg-ignore
12
- def parse_with_comments code, filename = nil
13
- node = RubyVM::AbstractSyntaxTree.parse(code).children[2]
14
- node &&= RubyVM::AbstractSyntaxTree::NodeWrapper.from(node, code.lines)
15
- comments = CommentRipper.new(code).parse
16
- [node, comments]
17
- rescue ::SyntaxError => e
18
- raise Parser::SyntaxError, e.message
19
- end
20
-
21
- # @param code [String]
22
- # @param filename [String, nil]
23
- # @param line [Integer]
24
- # @return [Parser::AST::Node]
25
- # @sg-ignore
26
- def parse code, filename = nil, line = 0
27
- node = RubyVM::AbstractSyntaxTree.parse(code).children[2]
28
- node and RubyVM::AbstractSyntaxTree::NodeWrapper.from(node, code.lines)
29
- rescue ::SyntaxError => e
30
- raise Parser::SyntaxError, e.message
31
- end
32
-
33
- def map source
34
- NodeProcessor.process(source.node, Region.new(source: source))
35
- end
36
-
37
- def references source, name
38
- if name.end_with?("=")
39
- reg = /#{Regexp.escape name[0..-2]}\s*=/
40
- extract_offset = ->(code, offset) { reg.match(code, offset).offset(0) }
41
- else
42
- extract_offset = ->(code, offset) { [soff = code.index(name, offset), soff + name.length] }
43
- end
44
- inner_node_references(name, source.node).map do |n|
45
- rng = Range.from_node(n)
46
- offset = Position.to_offset(source.code, rng.start)
47
- soff, eoff = extract_offset[source.code, offset]
48
- Location.new(
49
- source.filename,
50
- Range.new(
51
- Position.from_offset(source.code, soff),
52
- Position.from_offset(source.code, eoff)
53
- )
54
- )
55
- end
56
- end
57
-
58
- # @param name [String]
59
- # @param top [AST::Node]
60
- # @return [Array<AST::Node>]
61
- def inner_node_references name, top
62
- result = []
63
- if Parser.rubyvm?
64
- if Parser.is_ast_node?(top)
65
- result.push top if match_rubyvm_node_to_ref(top, name)
66
- top.children.each { |c| result.concat inner_node_references(name, c) }
67
- end
68
- else
69
- if Parser.is_ast_node?(top) && top.to_s.include?(":#{name}")
70
- result.push top if top.children.any? { |c| c.to_s == name }
71
- top.children.each { |c| result.concat inner_node_references(name, c) }
72
- end
73
- end
74
- result
75
- end
76
-
77
- def match_rubyvm_node_to_ref(top, name)
78
- top.children.select { |c| c.is_a?(Symbol) }.any? { |c| c.to_s == name } ||
79
- top.children.select { |c| c.is_a?(Array) }.any? { |c| c.include?(name.to_sym) }
80
- end
81
-
82
- def chain *args
83
- NodeChainer.chain *args
84
- end
85
-
86
- def chain_string *args
87
- NodeChainer.load_string *args
88
- end
89
-
90
- def process_node *args
91
- Solargraph::Parser::NodeProcessor.process *args
92
- end
93
-
94
- def infer_literal_node_type node
95
- NodeMethods.infer_literal_node_type node
96
- end
97
-
98
- def version
99
- Ruby::VERSION
100
- end
101
-
102
- def is_ast_node? node
103
- if Parser.rubyvm?
104
- node.is_a?(RubyVM::AbstractSyntaxTree::Node)
105
- else
106
- node.is_a?(::Parser::AST::Node)
107
- end
108
- end
109
-
110
- def node_range node
111
- if node.nil?
112
- nil
113
- else
114
- st = Position.new(node.first_lineno - 1, node.first_column)
115
- en = Position.new(node.last_lineno - 1, node.last_column)
116
- Range.new(st, en)
117
- end
118
- end
119
-
120
- def recipient_node tree
121
- tree.each_with_index do |node, idx|
122
- return tree[idx + 1] if [:ARRAY, :ZARRAY, :LIST].include?(node.type) && tree[idx + 1] && [:FCALL, :VCALL, :CALL].include?(tree[idx + 1].type)
123
- end
124
- nil
125
- end
126
-
127
- def string_ranges node
128
- return [] unless is_ast_node?(node)
129
- result = []
130
- if node.type == :STR
131
- result.push Range.from_node(node)
132
- elsif node.type == :DSTR
133
- here = Range.from_node(node)
134
- there = Range.from_node(node.children[1])
135
- result.push Range.new(here.start, there&.start || here.ending)
136
- end
137
- node.children.each do |child|
138
- result.concat string_ranges(child)
139
- end
140
- if node.type == :DSTR && node.children.last.nil?
141
- last = node.children[-2]
142
- unless last.nil?
143
- rng = Range.from_node(last)
144
- pos = Position.new(rng.ending.line, rng.ending.column - 1)
145
- result.push Range.new(pos, pos)
146
- end
147
- end
148
- result
149
- end
150
- end
151
- end
152
- end
153
- end