Debian Codegraph @

lib/prism/node_ext.rb

raw symbols SCIP
1 # frozen_string_literal: true
2 # :markup: markdown
3
4 #--
5 # Here we are reopening the prism module to provide methods on nodes that aren't
6 # templated and are meant as convenience methods.
7 #++
8 module Prism
9 class Node
10 def deprecated(*replacements) # :nodoc:
11 location = caller_locations(1, 1)
12 location = location[0].label if location
13 suggest = replacements.map { |replacement| "#{self.class}##{replacement}" }
14
15 warn(<<~MSG, uplevel: 1, category: :deprecated)
16 [deprecation]: #{self.class}##{location} is deprecated and will be \
17 removed in the next major version. Use #{suggest.join("/")} instead.
18 #{(caller(1, 3) || []).join("\n")}
19 MSG
20 end
21 end
22
23 module RegularExpressionOptions # :nodoc:
24 # Returns a numeric value that represents the flags that were used to create
25 # the regular expression.
26 def options
27 o = 0
28 o |= Regexp::IGNORECASE if flags.anybits?(RegularExpressionFlags::IGNORE_CASE)
29 o |= Regexp::EXTENDED if flags.anybits?(RegularExpressionFlags::EXTENDED)
30 o |= Regexp::MULTILINE if flags.anybits?(RegularExpressionFlags::MULTI_LINE)
31 o |= Regexp::FIXEDENCODING if flags.anybits?(RegularExpressionFlags::EUC_JP | RegularExpressionFlags::WINDOWS_31J | RegularExpressionFlags::UTF_8)
32 o |= Regexp::NOENCODING if flags.anybits?(RegularExpressionFlags::ASCII_8BIT)
33 o
34 end
35 end
36
37 class InterpolatedMatchLastLineNode < Node
38 include RegularExpressionOptions
39 end
40
41 class InterpolatedRegularExpressionNode < Node
42 include RegularExpressionOptions
43 end
44
45 class MatchLastLineNode < Node
46 include RegularExpressionOptions
47 end
48
49 class RegularExpressionNode < Node
50 include RegularExpressionOptions
51 end
52
53 private_constant :RegularExpressionOptions
54
55 module HeredocQuery # :nodoc:
56 # Returns true if this node was represented as a heredoc in the source code.
57 def heredoc?
58 opening&.start_with?("<<")
59 end
60 end
61
62 class InterpolatedStringNode < Node
63 include HeredocQuery
64 end
65
66 class InterpolatedXStringNode < Node
67 include HeredocQuery
68 end
69
70 class StringNode < Node
71 include HeredocQuery
72
73 # Occasionally it's helpful to treat a string as if it were interpolated so
74 # that there's a consistent interface for working with strings.
75 def to_interpolated
76 InterpolatedStringNode.new(
77 source,
78 -1,
79 location,
80 frozen? ? InterpolatedStringNodeFlags::FROZEN : 0,
81 opening_loc,
82 [copy(location: content_loc, opening_loc: nil, closing_loc: nil)],
83 closing_loc
84 )
85 end
86 end
87
88 class XStringNode < Node
89 include HeredocQuery
90
91 # Occasionally it's helpful to treat a string as if it were interpolated so
92 # that there's a consistent interface for working with strings.
93 def to_interpolated
94 InterpolatedXStringNode.new(
95 source,
96 -1,
97 location,
98 flags,
99 opening_loc,
100 [StringNode.new(source, node_id, content_loc, 0, nil, content_loc, nil, unescaped)],
101 closing_loc
102 )
103 end
104 end
105
106 private_constant :HeredocQuery
107
108 class ImaginaryNode < Node
109 # Returns the value of the node as a Ruby Complex.
110 def value
111 Complex(0, numeric.value)
112 end
113 end
114
115 class RationalNode < Node
116 # Returns the value of the node as a Ruby Rational.
117 def value
118 Rational(numerator, denominator)
119 end
120
121 # Returns the value of the node as an IntegerNode or a FloatNode. This
122 # method is deprecated in favor of #value or #numerator/#denominator.
123 def numeric
124 deprecated("value", "numerator", "denominator")
125
126 if denominator == 1
127 IntegerNode.new(source, -1, location.chop, flags, numerator)
128 else
129 FloatNode.new(source, -1, location.chop, 0, numerator.to_f / denominator)
130 end
131 end
132 end
133
134 class ConstantReadNode < Node
135 # Returns the list of parts for the full name of this constant.
136 # For example: [:Foo]
137 def full_name_parts
138 [name]
139 end
140
141 # Returns the full name of this constant. For example: "Foo"
142 def full_name
143 name.to_s
144 end
145 end
146
147 class ConstantWriteNode < Node
148 # Returns the list of parts for the full name of this constant.
149 # For example: [:Foo]
150 def full_name_parts
151 [name]
152 end
153
154 # Returns the full name of this constant. For example: "Foo"
155 def full_name
156 name.to_s
157 end
158 end
159
160 class ConstantPathNode < Node
161 # An error class raised when dynamic parts are found while computing a
162 # constant path's full name. For example:
163 # Foo::Bar::Baz -> does not raise because all parts of the constant path are
164 # simple constants
165 # var::Bar::Baz -> raises because the first part of the constant path is a
166 # local variable
167 class DynamicPartsInConstantPathError < StandardError; end
168
169 # An error class raised when missing nodes are found while computing a
170 # constant path's full name. For example:
171 # Foo:: -> raises because the constant path is missing the last part
172 class MissingNodesInConstantPathError < StandardError; end
173
174 # Returns the list of parts for the full name of this constant path.
175 # For example: [:Foo, :Bar]
176 def full_name_parts
177 parts = [] #: Array[Symbol]
178 current = self #: node?
179
180 while current.is_a?(ConstantPathNode)
181 name = current.name
182 if name.nil?
183 raise MissingNodesInConstantPathError, "Constant path contains missing nodes. Cannot compute full name"
184 end
185
186 parts.unshift(name)
187 current = current.parent
188 end
189
190 if !current.is_a?(ConstantReadNode) && !current.nil?
191 raise DynamicPartsInConstantPathError, "Constant path contains dynamic parts. Cannot compute full name"
192 end
193
194 parts.unshift(current&.name || :"")
195 end
196
197 # Returns the full name of this constant path. For example: "Foo::Bar"
198 def full_name
199 full_name_parts.join("::")
200 end
201
202 # Previously, we had a child node on this class that contained either a
203 # constant read or a missing node. To not cause a breaking change, we
204 # continue to supply that API.
205 def child
206 deprecated("name", "name_loc")
207
208 if name
209 ConstantReadNode.new(source, -1, name_loc, 0, name)
210 else
211 MissingNode.new(source, -1, location, 0)
212 end
213 end
214 end
215
216 class ConstantPathTargetNode < Node
217 # Returns the list of parts for the full name of this constant path.
218 # For example: [:Foo, :Bar]
219 def full_name_parts
220 parts =
221 case parent
222 when ConstantPathNode, ConstantReadNode
223 parent.full_name_parts
224 when nil
225 [:""]
226 else
227 # e.g. self::Foo, (var)::Bar = baz
228 raise ConstantPathNode::DynamicPartsInConstantPathError, "Constant target path contains dynamic parts. Cannot compute full name"
229 end
230
231 if name.nil?
232 raise ConstantPathNode::MissingNodesInConstantPathError, "Constant target path contains missing nodes. Cannot compute full name"
233 end
234
235 parts.push(name)
236 end
237
238 # Returns the full name of this constant path. For example: "Foo::Bar"
239 def full_name
240 full_name_parts.join("::")
241 end
242
243 # Previously, we had a child node on this class that contained either a
244 # constant read or a missing node. To not cause a breaking change, we
245 # continue to supply that API.
246 def child
247 deprecated("name", "name_loc")
248
249 if name
250 ConstantReadNode.new(source, -1, name_loc, 0, name)
251 else
252 MissingNode.new(source, -1, location, 0)
253 end
254 end
255 end
256
257 class ConstantTargetNode < Node
258 # Returns the list of parts for the full name of this constant.
259 # For example: [:Foo]
260 def full_name_parts
261 [name]
262 end
263
264 # Returns the full name of this constant. For example: "Foo"
265 def full_name
266 name.to_s
267 end
268 end
269
270 class ParametersNode < Node
271 # Mirrors the Method#parameters method.
272 def signature
273 names = [] #: Array[[Symbol, Symbol] | [Symbol]]
274
275 requireds.each do |param|
276 names << (param.is_a?(MultiTargetNode) ? [:req] : [:req, param.name])
277 end
278
279 optionals.each { |param| names << [:opt, param.name] }
280
281 if rest && rest.is_a?(RestParameterNode)
282 names << [:rest, rest.name || :*]
283 end
284
285 posts.each do |param|
286 case param
287 when MultiTargetNode
288 names << [:req]
289 when NoKeywordsParameterNode, KeywordRestParameterNode, ForwardingParameterNode
290 # Invalid syntax, e.g. "def f(**nil, ...)" moves the NoKeywordsParameterNode to posts
291 raise "Invalid syntax"
292 else
293 names << [:req, param.name]
294 end
295 end
296
297 # Regardless of the order in which the keywords were defined, the required
298 # keywords always come first followed by the optional keywords.
299 keyopt = [] #: Array[OptionalKeywordParameterNode]
300 keywords.each do |param|
301 if param.is_a?(OptionalKeywordParameterNode)
302 keyopt << param
303 else
304 names << [:keyreq, param.name]
305 end
306 end
307
308 keyopt.each { |param| names << [:key, param.name] }
309
310 case keyword_rest
311 when ForwardingParameterNode
312 names.concat([[:rest, :*], [:keyrest, :**], [:block, :&]])
313 when KeywordRestParameterNode
314 names << [:keyrest, keyword_rest.name || :**]
315 when NoKeywordsParameterNode
316 names << [:nokey]
317 end
318
319 names << [:block, block.name || :&] if block
320 names
321 end
322 end
323
324 class CallNode < Node
325 # When a call node has the attribute_write flag set, it means that the call
326 # is using the attribute write syntax. This is either a method call to []=
327 # or a method call to a method that ends with =. Either way, the = sign is
328 # present in the source.
329 #
330 # Prism returns the message_loc _without_ the = sign attached, because there
331 # can be any amount of space between the message and the = sign. However,
332 # sometimes you want the location of the full message including the inner
333 # space and the = sign. This method provides that.
334 def full_message_loc
335 attribute_write? ? message_loc&.adjoin("=") : message_loc
336 end
337 end
338
339 class CallOperatorWriteNode < Node
340 # Returns the binary operator used to modify the receiver. This method is
341 # deprecated in favor of #binary_operator.
342 def operator
343 deprecated("binary_operator")
344 binary_operator
345 end
346
347 # Returns the location of the binary operator used to modify the receiver.
348 # This method is deprecated in favor of #binary_operator_loc.
349 def operator_loc
350 deprecated("binary_operator_loc")
351 binary_operator_loc
352 end
353 end
354
355 class ClassVariableOperatorWriteNode < Node
356 # Returns the binary operator used to modify the receiver. This method is
357 # deprecated in favor of #binary_operator.
358 def operator
359 deprecated("binary_operator")
360 binary_operator
361 end
362
363 # Returns the location of the binary operator used to modify the receiver.
364 # This method is deprecated in favor of #binary_operator_loc.
365 def operator_loc
366 deprecated("binary_operator_loc")
367 binary_operator_loc
368 end
369 end
370
371 class ConstantOperatorWriteNode < Node
372 # Returns the binary operator used to modify the receiver. This method is
373 # deprecated in favor of #binary_operator.
374 def operator
375 deprecated("binary_operator")
376 binary_operator
377 end
378
379 # Returns the location of the binary operator used to modify the receiver.
380 # This method is deprecated in favor of #binary_operator_loc.
381 def operator_loc
382 deprecated("binary_operator_loc")
383 binary_operator_loc
384 end
385 end
386
387 class ConstantPathOperatorWriteNode < Node
388 # Returns the binary operator used to modify the receiver. This method is
389 # deprecated in favor of #binary_operator.
390 def operator
391 deprecated("binary_operator")
392 binary_operator
393 end
394
395 # Returns the location of the binary operator used to modify the receiver.
396 # This method is deprecated in favor of #binary_operator_loc.
397 def operator_loc
398 deprecated("binary_operator_loc")
399 binary_operator_loc
400 end
401 end
402
403 class GlobalVariableOperatorWriteNode < Node
404 # Returns the binary operator used to modify the receiver. This method is
405 # deprecated in favor of #binary_operator.
406 def operator
407 deprecated("binary_operator")
408 binary_operator
409 end
410
411 # Returns the location of the binary operator used to modify the receiver.
412 # This method is deprecated in favor of #binary_operator_loc.
413 def operator_loc
414 deprecated("binary_operator_loc")
415 binary_operator_loc
416 end
417 end
418
419 class IndexOperatorWriteNode < Node
420 # Returns the binary operator used to modify the receiver. This method is
421 # deprecated in favor of #binary_operator.
422 def operator
423 deprecated("binary_operator")
424 binary_operator
425 end
426
427 # Returns the location of the binary operator used to modify the receiver.
428 # This method is deprecated in favor of #binary_operator_loc.
429 def operator_loc
430 deprecated("binary_operator_loc")
431 binary_operator_loc
432 end
433 end
434
435 class InstanceVariableOperatorWriteNode < Node
436 # Returns the binary operator used to modify the receiver. This method is
437 # deprecated in favor of #binary_operator.
438 def operator
439 deprecated("binary_operator")
440 binary_operator
441 end
442
443 # Returns the location of the binary operator used to modify the receiver.
444 # This method is deprecated in favor of #binary_operator_loc.
445 def operator_loc
446 deprecated("binary_operator_loc")
447 binary_operator_loc
448 end
449 end
450
451 class LocalVariableOperatorWriteNode < Node
452 # Returns the binary operator used to modify the receiver. This method is
453 # deprecated in favor of #binary_operator.
454 def operator
455 deprecated("binary_operator")
456 binary_operator
457 end
458
459 # Returns the location of the binary operator used to modify the receiver.
460 # This method is deprecated in favor of #binary_operator_loc.
461 def operator_loc
462 deprecated("binary_operator_loc")
463 binary_operator_loc
464 end
465 end
466
467 class CaseMatchNode < Node
468 # Returns the else clause of the case match node. This method is deprecated
469 # in favor of #else_clause.
470 def consequent
471 deprecated("else_clause")
472 else_clause
473 end
474 end
475
476 class CaseNode < Node
477 # Returns the else clause of the case node. This method is deprecated in
478 # favor of #else_clause.
479 def consequent
480 deprecated("else_clause")
481 else_clause
482 end
483 end
484
485 class IfNode < Node
486 # Returns the subsequent if/elsif/else clause of the if node. This method is
487 # deprecated in favor of #subsequent.
488 def consequent
489 deprecated("subsequent")
490 subsequent
491 end
492 end
493
494 class RescueNode < Node
495 # Returns the subsequent rescue clause of the rescue node. This method is
496 # deprecated in favor of #subsequent.
497 def consequent
498 deprecated("subsequent")
499 subsequent
500 end
501 end
502
503 class UnlessNode < Node
504 # Returns the else clause of the unless node. This method is deprecated in
505 # favor of #else_clause.
506 def consequent
507 deprecated("else_clause")
508 else_clause
509 end
510 end
511 end