| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
module Prism |
| 9 |
class Node |
| 10 |
def deprecated(*replacements) |
| 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} |
| 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 |
| 24 |
|
| 25 |
|
| 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 |
| 56 |
|
| 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 |
|
| 74 |
|
| 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 |
|
| 92 |
|
| 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 |
|
| 110 |
def value |
| 111 |
Complex(0, numeric.value) |
| 112 |
end |
| 113 |
end |
| 114 |
|
| 115 |
class RationalNode < Node |
| 116 |
|
| 117 |
def value |
| 118 |
Rational(numerator, denominator) |
| 119 |
end |
| 120 |
|
| 121 |
|
| 122 |
|
| 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 |
|
| 136 |
|
| 137 |
def full_name_parts |
| 138 |
[name] |
| 139 |
end |
| 140 |
|
| 141 |
|
| 142 |
def full_name |
| 143 |
name.to_s |
| 144 |
end |
| 145 |
end |
| 146 |
|
| 147 |
class ConstantWriteNode < Node |
| 148 |
|
| 149 |
|
| 150 |
def full_name_parts |
| 151 |
[name] |
| 152 |
end |
| 153 |
|
| 154 |
|
| 155 |
def full_name |
| 156 |
name.to_s |
| 157 |
end |
| 158 |
end |
| 159 |
|
| 160 |
class ConstantPathNode < Node |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
class DynamicPartsInConstantPathError < StandardError; end |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
class MissingNodesInConstantPathError < StandardError; end |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
def full_name_parts |
| 177 |
parts = [] |
| 178 |
current = self |
| 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 |
|
| 198 |
def full_name |
| 199 |
full_name_parts.join("::") |
| 200 |
end |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 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 |
|
| 218 |
|
| 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 |
|
| 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 |
|
| 239 |
def full_name |
| 240 |
full_name_parts.join("::") |
| 241 |
end |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 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 |
|
| 259 |
|
| 260 |
def full_name_parts |
| 261 |
[name] |
| 262 |
end |
| 263 |
|
| 264 |
|
| 265 |
def full_name |
| 266 |
name.to_s |
| 267 |
end |
| 268 |
end |
| 269 |
|
| 270 |
class ParametersNode < Node |
| 271 |
|
| 272 |
def signature |
| 273 |
names = [] |
| 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 |
|
| 291 |
raise "Invalid syntax" |
| 292 |
else |
| 293 |
names << [:req, param.name] |
| 294 |
end |
| 295 |
end |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
keyopt = [] |
| 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 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
def full_message_loc |
| 335 |
attribute_write? ? message_loc&.adjoin("=") : message_loc |
| 336 |
end |
| 337 |
end |
| 338 |
|
| 339 |
class CallOperatorWriteNode < Node |
| 340 |
|
| 341 |
|
| 342 |
def operator |
| 343 |
deprecated("binary_operator") |
| 344 |
binary_operator |
| 345 |
end |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
def operator_loc |
| 350 |
deprecated("binary_operator_loc") |
| 351 |
binary_operator_loc |
| 352 |
end |
| 353 |
end |
| 354 |
|
| 355 |
class ClassVariableOperatorWriteNode < Node |
| 356 |
|
| 357 |
|
| 358 |
def operator |
| 359 |
deprecated("binary_operator") |
| 360 |
binary_operator |
| 361 |
end |
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
def operator_loc |
| 366 |
deprecated("binary_operator_loc") |
| 367 |
binary_operator_loc |
| 368 |
end |
| 369 |
end |
| 370 |
|
| 371 |
class ConstantOperatorWriteNode < Node |
| 372 |
|
| 373 |
|
| 374 |
def operator |
| 375 |
deprecated("binary_operator") |
| 376 |
binary_operator |
| 377 |
end |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
def operator_loc |
| 382 |
deprecated("binary_operator_loc") |
| 383 |
binary_operator_loc |
| 384 |
end |
| 385 |
end |
| 386 |
|
| 387 |
class ConstantPathOperatorWriteNode < Node |
| 388 |
|
| 389 |
|
| 390 |
def operator |
| 391 |
deprecated("binary_operator") |
| 392 |
binary_operator |
| 393 |
end |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
def operator_loc |
| 398 |
deprecated("binary_operator_loc") |
| 399 |
binary_operator_loc |
| 400 |
end |
| 401 |
end |
| 402 |
|
| 403 |
class GlobalVariableOperatorWriteNode < Node |
| 404 |
|
| 405 |
|
| 406 |
def operator |
| 407 |
deprecated("binary_operator") |
| 408 |
binary_operator |
| 409 |
end |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
def operator_loc |
| 414 |
deprecated("binary_operator_loc") |
| 415 |
binary_operator_loc |
| 416 |
end |
| 417 |
end |
| 418 |
|
| 419 |
class IndexOperatorWriteNode < Node |
| 420 |
|
| 421 |
|
| 422 |
def operator |
| 423 |
deprecated("binary_operator") |
| 424 |
binary_operator |
| 425 |
end |
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
def operator_loc |
| 430 |
deprecated("binary_operator_loc") |
| 431 |
binary_operator_loc |
| 432 |
end |
| 433 |
end |
| 434 |
|
| 435 |
class InstanceVariableOperatorWriteNode < Node |
| 436 |
|
| 437 |
|
| 438 |
def operator |
| 439 |
deprecated("binary_operator") |
| 440 |
binary_operator |
| 441 |
end |
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
def operator_loc |
| 446 |
deprecated("binary_operator_loc") |
| 447 |
binary_operator_loc |
| 448 |
end |
| 449 |
end |
| 450 |
|
| 451 |
class LocalVariableOperatorWriteNode < Node |
| 452 |
|
| 453 |
|
| 454 |
def operator |
| 455 |
deprecated("binary_operator") |
| 456 |
binary_operator |
| 457 |
end |
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
def operator_loc |
| 462 |
deprecated("binary_operator_loc") |
| 463 |
binary_operator_loc |
| 464 |
end |
| 465 |
end |
| 466 |
|
| 467 |
class CaseMatchNode < Node |
| 468 |
|
| 469 |
|
| 470 |
def consequent |
| 471 |
deprecated("else_clause") |
| 472 |
else_clause |
| 473 |
end |
| 474 |
end |
| 475 |
|
| 476 |
class CaseNode < Node |
| 477 |
|
| 478 |
|
| 479 |
def consequent |
| 480 |
deprecated("else_clause") |
| 481 |
else_clause |
| 482 |
end |
| 483 |
end |
| 484 |
|
| 485 |
class IfNode < Node |
| 486 |
|
| 487 |
|
| 488 |
def consequent |
| 489 |
deprecated("subsequent") |
| 490 |
subsequent |
| 491 |
end |
| 492 |
end |
| 493 |
|
| 494 |
class RescueNode < Node |
| 495 |
|
| 496 |
|
| 497 |
def consequent |
| 498 |
deprecated("subsequent") |
| 499 |
subsequent |
| 500 |
end |
| 501 |
end |
| 502 |
|
| 503 |
class UnlessNode < Node |
| 504 |
|
| 505 |
|
| 506 |
def consequent |
| 507 |
deprecated("else_clause") |
| 508 |
else_clause |
| 509 |
end |
| 510 |
end |
| 511 |
end |