syntax_tree 2.3.0 → 2.4.1

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.
@@ -1,6 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SyntaxTree
4
+ # Parser is a subclass of the Ripper library that subscribes to the stream of
5
+ # tokens and nodes coming from the parser and builds up a syntax tree.
4
6
  class Parser < Ripper
5
7
  # A special parser error so that we can get nice syntax displays on the
6
8
  # error message when prettier prints out the results.
@@ -40,9 +42,11 @@ module SyntaxTree
40
42
  @start = start
41
43
  @indices = []
42
44
 
43
- line.each_char.with_index(start) do |char, index|
44
- char.bytesize.times { @indices << index }
45
- end
45
+ line
46
+ .each_char
47
+ .with_index(start) do |char, index|
48
+ char.bytesize.times { @indices << index }
49
+ end
46
50
  end
47
51
 
48
52
  # Technically it's possible for the column index to be a negative value if
@@ -130,10 +134,10 @@ module SyntaxTree
130
134
  last_index = 0
131
135
 
132
136
  @source.lines.each do |line|
133
- if line.size == line.bytesize
134
- @line_counts << SingleByteString.new(last_index)
137
+ @line_counts << if line.size == line.bytesize
138
+ SingleByteString.new(last_index)
135
139
  else
136
- @line_counts << MultiByteString.new(last_index, line)
140
+ MultiByteString.new(last_index, line)
137
141
  end
138
142
 
139
143
  last_index += line.size
@@ -239,7 +243,7 @@ module SyntaxTree
239
243
  # By finding the next non-space character, we can make sure that the bounds
240
244
  # of the statement list are correct.
241
245
  def find_next_statement_start(position)
242
- remaining = source[position..-1]
246
+ remaining = source[position..]
243
247
 
244
248
  if remaining.sub(/\A +/, "")[0] == "#"
245
249
  return position + remaining.index("\n")
@@ -264,7 +268,7 @@ module SyntaxTree
264
268
  start_char,
265
269
  start_char - line_counts[lbrace.location.start_line - 1].start,
266
270
  rbrace.location.start_char,
267
- rbrace.location.start_column,
271
+ rbrace.location.start_column
268
272
  )
269
273
 
270
274
  keyword = find_token(Kw, "BEGIN")
@@ -281,7 +285,13 @@ module SyntaxTree
281
285
  def on_CHAR(value)
282
286
  CHAR.new(
283
287
  value: value,
284
- location: Location.token(line: lineno, char: char_pos, column: current_column, size: value.size)
288
+ location:
289
+ Location.token(
290
+ line: lineno,
291
+ char: char_pos,
292
+ column: current_column,
293
+ size: value.size
294
+ )
285
295
  )
286
296
  end
287
297
 
@@ -313,8 +323,14 @@ module SyntaxTree
313
323
  def on___end__(value)
314
324
  @__end__ =
315
325
  EndContent.new(
316
- value: source[(char_pos + value.length)..-1],
317
- location: Location.token(line: lineno, char: char_pos, column: current_column, size: value.size)
326
+ value: source[(char_pos + value.length)..],
327
+ location:
328
+ Location.token(
329
+ line: lineno,
330
+ char: char_pos,
331
+ column: current_column,
332
+ size: value.size
333
+ )
318
334
  )
319
335
  end
320
336
 
@@ -423,7 +439,8 @@ module SyntaxTree
423
439
  # If there are any arguments and the operator we found from the list is
424
440
  # not after them, then we're going to return the arguments as-is because
425
441
  # we're looking at an & that occurs before the arguments are done.
426
- if arguments.parts.any? && operator.location.start_char < arguments.location.end_char
442
+ if arguments.parts.any? &&
443
+ operator.location.start_char < arguments.location.end_char
427
444
  return arguments
428
445
  end
429
446
 
@@ -478,7 +495,11 @@ module SyntaxTree
478
495
  # :call-seq:
479
496
  # on_args_new: () -> Args
480
497
  def on_args_new
481
- Args.new(parts: [], location: Location.fixed(line: lineno, column: current_column, char: char_pos))
498
+ Args.new(
499
+ parts: [],
500
+ location:
501
+ Location.fixed(line: lineno, column: current_column, char: char_pos)
502
+ )
482
503
  end
483
504
 
484
505
  # :call-seq:
@@ -529,7 +550,7 @@ module SyntaxTree
529
550
 
530
551
  # If there's the optional then keyword, then we'll delete that and use it
531
552
  # as the end bounds of the location.
532
- if token = find_token(Kw, "then", consume: false)
553
+ if (token = find_token(Kw, "then", consume: false))
533
554
  tokens.delete(token)
534
555
  location = location.to(token.location)
535
556
  end
@@ -538,10 +559,10 @@ module SyntaxTree
538
559
  # here because it currently doesn't have anything to use for its precise
539
560
  # location. If we hit a comma, then we've gone too far.
540
561
  if rest.is_a?(VarField) && rest.value.nil?
541
- tokens.rindex do |token|
542
- case token
562
+ tokens.rindex do |rtoken|
563
+ case rtoken
543
564
  in Op[value: "*"]
544
- rest = VarField.new(value: nil, location: token.location)
565
+ rest = VarField.new(value: nil, location: rtoken.location)
545
566
  break
546
567
  in Comma
547
568
  break
@@ -561,7 +582,13 @@ module SyntaxTree
561
582
 
562
583
  # :call-seq:
563
584
  # on_assign: (
564
- # (ARefField | ConstPathField | Field | TopConstField | VarField) target,
585
+ # (
586
+ # ARefField |
587
+ # ConstPathField |
588
+ # Field |
589
+ # TopConstField |
590
+ # VarField
591
+ # ) target,
565
592
  # untyped value
566
593
  # ) -> Assign
567
594
  def on_assign(target, value)
@@ -586,7 +613,10 @@ module SyntaxTree
586
613
  def on_assoc_splat(value)
587
614
  operator = find_token(Op, "**")
588
615
 
589
- AssocSplat.new(value: value, location: operator.location.to(value.location))
616
+ AssocSplat.new(
617
+ value: value,
618
+ location: operator.location.to(value.location)
619
+ )
590
620
  end
591
621
 
592
622
  # def on_assoclist_from_args(assocs)
@@ -598,7 +628,13 @@ module SyntaxTree
598
628
  def on_backref(value)
599
629
  Backref.new(
600
630
  value: value,
601
- location: Location.token(line: lineno, char: char_pos, column: current_column, size: value.size)
631
+ location:
632
+ Location.token(
633
+ line: lineno,
634
+ char: char_pos,
635
+ column: current_column,
636
+ size: value.size
637
+ )
602
638
  )
603
639
  end
604
640
 
@@ -608,7 +644,13 @@ module SyntaxTree
608
644
  node =
609
645
  Backtick.new(
610
646
  value: value,
611
- location: Location.token(line: lineno, char: char_pos, column: current_column, size: value.size)
647
+ location:
648
+ Location.token(
649
+ line: lineno,
650
+ char: char_pos,
651
+ column: current_column,
652
+ size: value.size
653
+ )
612
654
  )
613
655
 
614
656
  tokens << node
@@ -616,7 +658,9 @@ module SyntaxTree
616
658
  end
617
659
 
618
660
  # :call-seq:
619
- # on_bare_assoc_hash: (Array[AssocNew | AssocSplat] assocs) -> BareAssocHash
661
+ # on_bare_assoc_hash: (
662
+ # Array[AssocNew | AssocSplat] assocs
663
+ # ) -> BareAssocHash
620
664
  def on_bare_assoc_hash(assocs)
621
665
  BareAssocHash.new(
622
666
  assocs: assocs,
@@ -641,7 +685,7 @@ module SyntaxTree
641
685
  keyword = find_token(Kw, "begin")
642
686
  end_location =
643
687
  if bodystmt.rescue_clause || bodystmt.ensure_clause ||
644
- bodystmt.else_clause
688
+ bodystmt.else_clause
645
689
  bodystmt.location
646
690
  else
647
691
  find_token(Kw, "end").location
@@ -660,7 +704,11 @@ module SyntaxTree
660
704
  end
661
705
 
662
706
  # :call-seq:
663
- # on_binary: (untyped left, (Op | Symbol) operator, untyped right) -> Binary
707
+ # on_binary: (
708
+ # untyped left,
709
+ # (Op | Symbol) operator,
710
+ # untyped right
711
+ # ) -> Binary
664
712
  def on_binary(left, operator, right)
665
713
  if operator.is_a?(Symbol)
666
714
  # Here, we're going to search backward for the token that's between the
@@ -737,7 +785,8 @@ module SyntaxTree
737
785
  else_keyword: else_clause && find_token(Kw, "else"),
738
786
  else_clause: else_clause,
739
787
  ensure_clause: ensure_clause,
740
- location: Location.fixed(line: lineno, char: char_pos, column: current_column)
788
+ location:
789
+ Location.fixed(line: lineno, char: char_pos, column: current_column)
741
790
  )
742
791
  end
743
792
 
@@ -764,7 +813,10 @@ module SyntaxTree
764
813
  start_line: lbrace.location.start_line,
765
814
  start_char: lbrace.location.start_char,
766
815
  start_column: lbrace.location.start_column,
767
- end_line: [rbrace.location.end_line, statements.location.end_line].max,
816
+ end_line: [
817
+ rbrace.location.end_line,
818
+ statements.location.end_line
819
+ ].max,
768
820
  end_char: rbrace.location.end_char,
769
821
  end_column: rbrace.location.end_column
770
822
  )
@@ -816,7 +868,7 @@ module SyntaxTree
816
868
  # :call-seq:
817
869
  # on_case: (untyped value, untyped consequent) -> Case | RAssign
818
870
  def on_case(value, consequent)
819
- if keyword = find_token(Kw, "case", consume: false)
871
+ if (keyword = find_token(Kw, "case", consume: false))
820
872
  tokens.delete(keyword)
821
873
 
822
874
  Case.new(
@@ -870,7 +922,13 @@ module SyntaxTree
870
922
  node =
871
923
  Comma.new(
872
924
  value: value,
873
- location: Location.token(line: lineno, char: char_pos, column: current_column, size: value.size)
925
+ location:
926
+ Location.token(
927
+ line: lineno,
928
+ char: char_pos,
929
+ column: current_column,
930
+ size: value.size
931
+ )
874
932
  )
875
933
 
876
934
  tokens << node
@@ -915,7 +973,12 @@ module SyntaxTree
915
973
  value: value.chomp,
916
974
  inline: value.strip != lines[line - 1].strip,
917
975
  location:
918
- Location.token(line: line, char: char_pos, column: current_column, size: value.size - 1)
976
+ Location.token(
977
+ line: line,
978
+ char: char_pos,
979
+ column: current_column,
980
+ size: value.size - 1
981
+ )
919
982
  )
920
983
 
921
984
  @comments << comment
@@ -927,7 +990,13 @@ module SyntaxTree
927
990
  def on_const(value)
928
991
  Const.new(
929
992
  value: value,
930
- location: Location.token(line: lineno, char: char_pos, column: current_column, size: value.size)
993
+ location:
994
+ Location.token(
995
+ line: lineno,
996
+ char: char_pos,
997
+ column: current_column,
998
+ size: value.size
999
+ )
931
1000
  )
932
1001
  end
933
1002
 
@@ -962,7 +1031,13 @@ module SyntaxTree
962
1031
  def on_cvar(value)
963
1032
  CVar.new(
964
1033
  value: value,
965
- location: Location.token(line: lineno, char: char_pos, column: current_column, size: value.size)
1034
+ location:
1035
+ Location.token(
1036
+ line: lineno,
1037
+ char: char_pos,
1038
+ column: current_column,
1039
+ size: value.size
1040
+ )
966
1041
  )
967
1042
  end
968
1043
 
@@ -1047,7 +1122,10 @@ module SyntaxTree
1047
1122
  ending = find_token(RParen)
1048
1123
  end
1049
1124
 
1050
- Defined.new(value: value, location: beginning.location.to(ending.location))
1125
+ Defined.new(
1126
+ value: value,
1127
+ location: beginning.location.to(ending.location)
1128
+ )
1051
1129
  end
1052
1130
 
1053
1131
  # :call-seq:
@@ -1268,7 +1346,8 @@ module SyntaxTree
1268
1346
  @embdoc =
1269
1347
  EmbDoc.new(
1270
1348
  value: value,
1271
- location: Location.fixed(line: lineno, column: current_column, char: char_pos)
1349
+ location:
1350
+ Location.fixed(line: lineno, column: current_column, char: char_pos)
1272
1351
  )
1273
1352
  end
1274
1353
 
@@ -1302,7 +1381,13 @@ module SyntaxTree
1302
1381
  node =
1303
1382
  EmbExprBeg.new(
1304
1383
  value: value,
1305
- location: Location.token(line: lineno, char: char_pos, column: current_column, size: value.size)
1384
+ location:
1385
+ Location.token(
1386
+ line: lineno,
1387
+ char: char_pos,
1388
+ column: current_column,
1389
+ size: value.size
1390
+ )
1306
1391
  )
1307
1392
 
1308
1393
  tokens << node
@@ -1315,7 +1400,13 @@ module SyntaxTree
1315
1400
  node =
1316
1401
  EmbExprEnd.new(
1317
1402
  value: value,
1318
- location: Location.token(line: lineno, char: char_pos, column: current_column, size: value.size)
1403
+ location:
1404
+ Location.token(
1405
+ line: lineno,
1406
+ char: char_pos,
1407
+ column: current_column,
1408
+ size: value.size
1409
+ )
1319
1410
  )
1320
1411
 
1321
1412
  tokens << node
@@ -1328,7 +1419,13 @@ module SyntaxTree
1328
1419
  node =
1329
1420
  EmbVar.new(
1330
1421
  value: value,
1331
- location: Location.token(line: lineno, char: char_pos, column: current_column, size: value.size)
1422
+ location:
1423
+ Location.token(
1424
+ line: lineno,
1425
+ char: char_pos,
1426
+ column: current_column,
1427
+ size: value.size
1428
+ )
1332
1429
  )
1333
1430
 
1334
1431
  tokens << node
@@ -1395,7 +1492,13 @@ module SyntaxTree
1395
1492
  def on_float(value)
1396
1493
  FloatLiteral.new(
1397
1494
  value: value,
1398
- location: Location.token(line: lineno, char: char_pos, column: current_column, size: value.size)
1495
+ location:
1496
+ Location.token(
1497
+ line: lineno,
1498
+ char: char_pos,
1499
+ column: current_column,
1500
+ size: value.size
1501
+ )
1399
1502
  )
1400
1503
  end
1401
1504
 
@@ -1413,8 +1516,7 @@ module SyntaxTree
1413
1516
  # the location of the node.
1414
1517
  opening =
1415
1518
  find_token(LBracket, consume: false) ||
1416
- find_token(LParen, consume: false) ||
1417
- left
1519
+ find_token(LParen, consume: false) || left
1418
1520
 
1419
1521
  # The closing is based on the opening, which is either the matched
1420
1522
  # punctuation or the right splat.
@@ -1453,8 +1555,9 @@ module SyntaxTree
1453
1555
  # Consume the do keyword if it exists so that it doesn't get confused for
1454
1556
  # some other block
1455
1557
  keyword = find_token(Kw, "do", consume: false)
1456
- if keyword && keyword.location.start_char > collection.location.end_char &&
1457
- keyword.location.end_char < ending.location.start_char
1558
+ if keyword &&
1559
+ keyword.location.start_char > collection.location.end_char &&
1560
+ keyword.location.end_char < ending.location.start_char
1458
1561
  tokens.delete(keyword)
1459
1562
  end
1460
1563
 
@@ -1483,7 +1586,13 @@ module SyntaxTree
1483
1586
  def on_gvar(value)
1484
1587
  GVar.new(
1485
1588
  value: value,
1486
- location: Location.token(line: lineno, char: char_pos, column: current_column, size: value.size)
1589
+ location:
1590
+ Location.token(
1591
+ line: lineno,
1592
+ char: char_pos,
1593
+ column: current_column,
1594
+ size: value.size
1595
+ )
1487
1596
  )
1488
1597
  end
1489
1598
 
@@ -1504,7 +1613,12 @@ module SyntaxTree
1504
1613
  # on_heredoc_beg: (String value) -> HeredocBeg
1505
1614
  def on_heredoc_beg(value)
1506
1615
  location =
1507
- Location.token(line: lineno, char: char_pos, column: current_column, size: value.size + 1)
1616
+ Location.token(
1617
+ line: lineno,
1618
+ char: char_pos,
1619
+ column: current_column,
1620
+ size: value.size + 1
1621
+ )
1508
1622
 
1509
1623
  # Here we're going to artificially create an extra node type so that if
1510
1624
  # there are comments after the declaration of a heredoc, they get printed.
@@ -1545,7 +1659,7 @@ module SyntaxTree
1545
1659
  start_column: heredoc.location.start_column,
1546
1660
  end_line: lineno,
1547
1661
  end_char: char_pos,
1548
- end_column: current_column,
1662
+ end_column: current_column
1549
1663
  )
1550
1664
  )
1551
1665
  end
@@ -1557,30 +1671,44 @@ module SyntaxTree
1557
1671
  # (nil | VarField) keyword_rest
1558
1672
  # ) -> HshPtn
1559
1673
  def on_hshptn(constant, keywords, keyword_rest)
1560
- # Create an artificial VarField if we find an extra ** on the end
1561
- if !keyword_rest && (token = find_token(Op, "**", consume: false))
1674
+ if keyword_rest
1675
+ # We're doing this to delete the token from the list so that it doesn't
1676
+ # confuse future patterns by thinking they have an extra ** on the end.
1677
+ find_token(Op, "**")
1678
+ elsif (token = find_token(Op, "**", consume: false))
1562
1679
  tokens.delete(token)
1680
+
1681
+ # Create an artificial VarField if we find an extra ** on the end. This
1682
+ # means the formatting will be a little more consistent.
1563
1683
  keyword_rest = VarField.new(value: nil, location: token.location)
1564
1684
  end
1565
1685
 
1686
+ parts = [constant, *keywords&.flatten(1), keyword_rest].compact
1687
+
1688
+ # If there's no constant, there may be braces, so we're going to look for
1689
+ # those to get our bounds.
1690
+ unless constant
1691
+ lbrace = find_token(LBrace, consume: false)
1692
+ rbrace = find_token(RBrace, consume: false)
1693
+
1694
+ if lbrace && rbrace
1695
+ parts = [lbrace, *parts, rbrace]
1696
+ tokens.delete(lbrace)
1697
+ tokens.delete(rbrace)
1698
+ end
1699
+ end
1700
+
1566
1701
  # Delete the optional then keyword
1567
- if token = find_token(Kw, "then", consume: false)
1702
+ if (token = find_token(Kw, "then", consume: false))
1703
+ parts << token
1568
1704
  tokens.delete(token)
1569
1705
  end
1570
1706
 
1571
- parts = [constant, *keywords&.flatten(1), keyword_rest].compact
1572
- location =
1573
- if parts.any?
1574
- parts[0].location.to(parts[-1].location)
1575
- else
1576
- find_token(LBrace).location.to(find_token(RBrace).location)
1577
- end
1578
-
1579
1707
  HshPtn.new(
1580
1708
  constant: constant,
1581
1709
  keywords: keywords || [],
1582
1710
  keyword_rest: keyword_rest,
1583
- location: location
1711
+ location: parts[0].location.to(parts[-1].location)
1584
1712
  )
1585
1713
  end
1586
1714
 
@@ -1589,7 +1717,13 @@ module SyntaxTree
1589
1717
  def on_ident(value)
1590
1718
  Ident.new(
1591
1719
  value: value,
1592
- location: Location.token(line: lineno, char: char_pos, column: current_column, size: value.size)
1720
+ location:
1721
+ Location.token(
1722
+ line: lineno,
1723
+ char: char_pos,
1724
+ column: current_column,
1725
+ size: value.size
1726
+ )
1593
1727
  )
1594
1728
  end
1595
1729
 
@@ -1654,7 +1788,13 @@ module SyntaxTree
1654
1788
  def on_imaginary(value)
1655
1789
  Imaginary.new(
1656
1790
  value: value,
1657
- location: Location.token(line: lineno, char: char_pos, column: current_column, size: value.size)
1791
+ location:
1792
+ Location.token(
1793
+ line: lineno,
1794
+ char: char_pos,
1795
+ column: current_column,
1796
+ size: value.size
1797
+ )
1658
1798
  )
1659
1799
  end
1660
1800
 
@@ -1673,7 +1813,7 @@ module SyntaxTree
1673
1813
  ending = consequent || find_token(Kw, "end")
1674
1814
 
1675
1815
  statements_start = pattern
1676
- if token = find_token(Kw, "then", consume: false)
1816
+ if (token = find_token(Kw, "then", consume: false))
1677
1817
  tokens.delete(token)
1678
1818
  statements_start = token
1679
1819
  end
@@ -1681,7 +1821,8 @@ module SyntaxTree
1681
1821
  start_char = find_next_statement_start(statements_start.location.end_char)
1682
1822
  statements.bind(
1683
1823
  start_char,
1684
- start_char - line_counts[statements_start.location.start_line - 1].start,
1824
+ start_char -
1825
+ line_counts[statements_start.location.start_line - 1].start,
1685
1826
  ending.location.start_char,
1686
1827
  ending.location.start_column
1687
1828
  )
@@ -1699,7 +1840,13 @@ module SyntaxTree
1699
1840
  def on_int(value)
1700
1841
  Int.new(
1701
1842
  value: value,
1702
- location: Location.token(line: lineno, char: char_pos, column: current_column, size: value.size)
1843
+ location:
1844
+ Location.token(
1845
+ line: lineno,
1846
+ char: char_pos,
1847
+ column: current_column,
1848
+ size: value.size
1849
+ )
1703
1850
  )
1704
1851
  end
1705
1852
 
@@ -1708,7 +1855,13 @@ module SyntaxTree
1708
1855
  def on_ivar(value)
1709
1856
  IVar.new(
1710
1857
  value: value,
1711
- location: Location.token(line: lineno, char: char_pos, column: current_column, size: value.size)
1858
+ location:
1859
+ Location.token(
1860
+ line: lineno,
1861
+ char: char_pos,
1862
+ column: current_column,
1863
+ size: value.size
1864
+ )
1712
1865
  )
1713
1866
  end
1714
1867
 
@@ -1718,7 +1871,13 @@ module SyntaxTree
1718
1871
  node =
1719
1872
  Kw.new(
1720
1873
  value: value,
1721
- location: Location.token(line: lineno, char: char_pos, column: current_column, size: value.size)
1874
+ location:
1875
+ Location.token(
1876
+ line: lineno,
1877
+ char: char_pos,
1878
+ column: current_column,
1879
+ size: value.size
1880
+ )
1722
1881
  )
1723
1882
 
1724
1883
  tokens << node
@@ -1739,7 +1898,13 @@ module SyntaxTree
1739
1898
  def on_label(value)
1740
1899
  Label.new(
1741
1900
  value: value,
1742
- location: Location.token(line: lineno, char: char_pos, column: current_column, size: value.size)
1901
+ location:
1902
+ Location.token(
1903
+ line: lineno,
1904
+ char: char_pos,
1905
+ column: current_column,
1906
+ size: value.size
1907
+ )
1743
1908
  )
1744
1909
  end
1745
1910
 
@@ -1749,7 +1914,13 @@ module SyntaxTree
1749
1914
  node =
1750
1915
  LabelEnd.new(
1751
1916
  value: value,
1752
- location: Location.token(line: lineno, char: char_pos, column: current_column, size: value.size)
1917
+ location:
1918
+ Location.token(
1919
+ line: lineno,
1920
+ char: char_pos,
1921
+ column: current_column,
1922
+ size: value.size
1923
+ )
1753
1924
  )
1754
1925
 
1755
1926
  tokens << node
@@ -1763,11 +1934,13 @@ module SyntaxTree
1763
1934
  # ) -> Lambda
1764
1935
  def on_lambda(params, statements)
1765
1936
  beginning = find_token(TLambda)
1766
-
1767
- if tokens.any? { |token|
1937
+ braces =
1938
+ tokens.any? do |token|
1768
1939
  token.is_a?(TLamBeg) &&
1769
1940
  token.location.start_char > beginning.location.start_char
1770
- }
1941
+ end
1942
+
1943
+ if braces
1771
1944
  opening = find_token(TLamBeg)
1772
1945
  closing = find_token(RBrace)
1773
1946
  else
@@ -1795,7 +1968,13 @@ module SyntaxTree
1795
1968
  node =
1796
1969
  LBrace.new(
1797
1970
  value: value,
1798
- location: Location.token(line: lineno, char: char_pos, column: current_column, size: value.size)
1971
+ location:
1972
+ Location.token(
1973
+ line: lineno,
1974
+ char: char_pos,
1975
+ column: current_column,
1976
+ size: value.size
1977
+ )
1799
1978
  )
1800
1979
 
1801
1980
  tokens << node
@@ -1808,7 +1987,13 @@ module SyntaxTree
1808
1987
  node =
1809
1988
  LBracket.new(
1810
1989
  value: value,
1811
- location: Location.token(line: lineno, char: char_pos, column: current_column, size: value.size)
1990
+ location:
1991
+ Location.token(
1992
+ line: lineno,
1993
+ char: char_pos,
1994
+ column: current_column,
1995
+ size: value.size
1996
+ )
1812
1997
  )
1813
1998
 
1814
1999
  tokens << node
@@ -1821,7 +2006,13 @@ module SyntaxTree
1821
2006
  node =
1822
2007
  LParen.new(
1823
2008
  value: value,
1824
- location: Location.token(line: lineno, char: char_pos, column: current_column, size: value.size)
2009
+ location:
2010
+ Location.token(
2011
+ line: lineno,
2012
+ char: char_pos,
2013
+ column: current_column,
2014
+ size: value.size
2015
+ )
1825
2016
  )
1826
2017
 
1827
2018
  tokens << node
@@ -1920,7 +2111,11 @@ module SyntaxTree
1920
2111
  # :call-seq:
1921
2112
  # on_mlhs_new: () -> MLHS
1922
2113
  def on_mlhs_new
1923
- MLHS.new(parts: [], location: Location.fixed(line: lineno, char: char_pos, column: current_column))
2114
+ MLHS.new(
2115
+ parts: [],
2116
+ location:
2117
+ Location.fixed(line: lineno, char: char_pos, column: current_column)
2118
+ )
1924
2119
  end
1925
2120
 
1926
2121
  # :call-seq:
@@ -1965,18 +2160,18 @@ module SyntaxTree
1965
2160
  # :call-seq:
1966
2161
  # on_mrhs_new: () -> MRHS
1967
2162
  def on_mrhs_new
1968
- MRHS.new(parts: [], location: Location.fixed(line: lineno, char: char_pos, column: current_column))
2163
+ MRHS.new(
2164
+ parts: [],
2165
+ location:
2166
+ Location.fixed(line: lineno, char: char_pos, column: current_column)
2167
+ )
1969
2168
  end
1970
2169
 
1971
2170
  # :call-seq:
1972
2171
  # on_mrhs_add: (MRHS mrhs, untyped part) -> MRHS
1973
2172
  def on_mrhs_add(mrhs, part)
1974
2173
  location =
1975
- if mrhs.parts.empty?
1976
- mrhs.location
1977
- else
1978
- mrhs.location.to(part.location)
1979
- end
2174
+ (mrhs.parts.empty? ? mrhs.location : mrhs.location.to(part.location))
1980
2175
 
1981
2176
  MRHS.new(parts: mrhs.parts << part, location: location)
1982
2177
  end
@@ -2034,7 +2229,13 @@ module SyntaxTree
2034
2229
  node =
2035
2230
  Op.new(
2036
2231
  value: value,
2037
- location: Location.token(line: lineno, char: char_pos, column: current_column, size: value.size)
2232
+ location:
2233
+ Location.token(
2234
+ line: lineno,
2235
+ char: char_pos,
2236
+ column: current_column,
2237
+ size: value.size
2238
+ )
2038
2239
  )
2039
2240
 
2040
2241
  tokens << node
@@ -2043,7 +2244,13 @@ module SyntaxTree
2043
2244
 
2044
2245
  # :call-seq:
2045
2246
  # on_opassign: (
2046
- # (ARefField | ConstPathField | Field | TopConstField | VarField) target,
2247
+ # (
2248
+ # ARefField |
2249
+ # ConstPathField |
2250
+ # Field |
2251
+ # TopConstField |
2252
+ # VarField
2253
+ # ) target,
2047
2254
  # Op operator,
2048
2255
  # untyped value
2049
2256
  # ) -> OpAssign
@@ -2118,14 +2325,15 @@ module SyntaxTree
2118
2325
  lparen = find_token(LParen)
2119
2326
  rparen = find_token(RParen)
2120
2327
 
2121
- if contents && contents.is_a?(Params)
2328
+ if contents.is_a?(Params)
2122
2329
  location = contents.location
2123
2330
  start_char = find_next_statement_start(lparen.location.end_char)
2124
2331
  location =
2125
2332
  Location.new(
2126
2333
  start_line: location.start_line,
2127
2334
  start_char: start_char,
2128
- start_column: start_char - line_counts[lparen.location.start_line - 1].start,
2335
+ start_column:
2336
+ start_char - line_counts[lparen.location.start_line - 1].start,
2129
2337
  end_line: location.end_line,
2130
2338
  end_char: rparen.location.start_char,
2131
2339
  end_column: rparen.location.start_column
@@ -2166,7 +2374,13 @@ module SyntaxTree
2166
2374
  def on_period(value)
2167
2375
  Period.new(
2168
2376
  value: value,
2169
- location: Location.token(line: lineno, char: char_pos, column: current_column, size: value.size)
2377
+ location:
2378
+ Location.token(
2379
+ line: lineno,
2380
+ char: char_pos,
2381
+ column: current_column,
2382
+ size: value.size
2383
+ )
2170
2384
  )
2171
2385
  end
2172
2386
 
@@ -2298,7 +2512,13 @@ module SyntaxTree
2298
2512
  node =
2299
2513
  QSymbolsBeg.new(
2300
2514
  value: value,
2301
- location: Location.token(line: lineno, char: char_pos, column: current_column, size: value.size)
2515
+ location:
2516
+ Location.token(
2517
+ line: lineno,
2518
+ char: char_pos,
2519
+ column: current_column,
2520
+ size: value.size
2521
+ )
2302
2522
  )
2303
2523
 
2304
2524
  tokens << node
@@ -2333,7 +2553,13 @@ module SyntaxTree
2333
2553
  node =
2334
2554
  QWordsBeg.new(
2335
2555
  value: value,
2336
- location: Location.token(line: lineno, char: char_pos, column: current_column, size: value.size)
2556
+ location:
2557
+ Location.token(
2558
+ line: lineno,
2559
+ char: char_pos,
2560
+ column: current_column,
2561
+ size: value.size
2562
+ )
2337
2563
  )
2338
2564
 
2339
2565
  tokens << node
@@ -2345,7 +2571,11 @@ module SyntaxTree
2345
2571
  def on_qwords_new
2346
2572
  beginning = find_token(QWordsBeg)
2347
2573
 
2348
- QWords.new(beginning: beginning, elements: [], location: beginning.location)
2574
+ QWords.new(
2575
+ beginning: beginning,
2576
+ elements: [],
2577
+ location: beginning.location
2578
+ )
2349
2579
  end
2350
2580
 
2351
2581
  # :call-seq:
@@ -2353,7 +2583,13 @@ module SyntaxTree
2353
2583
  def on_rational(value)
2354
2584
  RationalLiteral.new(
2355
2585
  value: value,
2356
- location: Location.token(line: lineno, char: char_pos, column: current_column, size: value.size)
2586
+ location:
2587
+ Location.token(
2588
+ line: lineno,
2589
+ char: char_pos,
2590
+ column: current_column,
2591
+ size: value.size
2592
+ )
2357
2593
  )
2358
2594
  end
2359
2595
 
@@ -2363,7 +2599,13 @@ module SyntaxTree
2363
2599
  node =
2364
2600
  RBrace.new(
2365
2601
  value: value,
2366
- location: Location.token(line: lineno, char: char_pos, column: current_column, size: value.size)
2602
+ location:
2603
+ Location.token(
2604
+ line: lineno,
2605
+ char: char_pos,
2606
+ column: current_column,
2607
+ size: value.size
2608
+ )
2367
2609
  )
2368
2610
 
2369
2611
  tokens << node
@@ -2376,7 +2618,13 @@ module SyntaxTree
2376
2618
  node =
2377
2619
  RBracket.new(
2378
2620
  value: value,
2379
- location: Location.token(line: lineno, char: char_pos, column: current_column, size: value.size)
2621
+ location:
2622
+ Location.token(
2623
+ line: lineno,
2624
+ char: char_pos,
2625
+ column: current_column,
2626
+ size: value.size
2627
+ )
2380
2628
  )
2381
2629
 
2382
2630
  tokens << node
@@ -2410,7 +2658,13 @@ module SyntaxTree
2410
2658
  node =
2411
2659
  RegexpBeg.new(
2412
2660
  value: value,
2413
- location: Location.token(line: lineno, char: char_pos, column: current_column, size: value.size)
2661
+ location:
2662
+ Location.token(
2663
+ line: lineno,
2664
+ char: char_pos,
2665
+ column: current_column,
2666
+ size: value.size
2667
+ )
2414
2668
  )
2415
2669
 
2416
2670
  tokens << node
@@ -2422,7 +2676,13 @@ module SyntaxTree
2422
2676
  def on_regexp_end(value)
2423
2677
  RegexpEnd.new(
2424
2678
  value: value,
2425
- location: Location.token(line: lineno, char: char_pos, column: current_column, size: value.size)
2679
+ location:
2680
+ Location.token(
2681
+ line: lineno,
2682
+ char: char_pos,
2683
+ column: current_column,
2684
+ size: value.size
2685
+ )
2426
2686
  )
2427
2687
  end
2428
2688
 
@@ -2563,7 +2823,13 @@ module SyntaxTree
2563
2823
  node =
2564
2824
  RParen.new(
2565
2825
  value: value,
2566
- location: Location.token(line: lineno, char: char_pos, column: current_column, size: value.size)
2826
+ location:
2827
+ Location.token(
2828
+ line: lineno,
2829
+ char: char_pos,
2830
+ column: current_column,
2831
+ size: value.size
2832
+ )
2567
2833
  )
2568
2834
 
2569
2835
  tokens << node
@@ -2611,7 +2877,11 @@ module SyntaxTree
2611
2877
  statements.location.to(statement.location)
2612
2878
  end
2613
2879
 
2614
- Statements.new(self, body: statements.body << statement, location: location)
2880
+ Statements.new(
2881
+ self,
2882
+ body: statements.body << statement,
2883
+ location: location
2884
+ )
2615
2885
  end
2616
2886
 
2617
2887
  # :call-seq:
@@ -2620,7 +2890,8 @@ module SyntaxTree
2620
2890
  Statements.new(
2621
2891
  self,
2622
2892
  body: [],
2623
- location: Location.fixed(line: lineno, char: char_pos, column: current_column)
2893
+ location:
2894
+ Location.fixed(line: lineno, char: char_pos, column: current_column)
2624
2895
  )
2625
2896
  end
2626
2897
 
@@ -2654,7 +2925,8 @@ module SyntaxTree
2654
2925
  def on_string_content
2655
2926
  StringContent.new(
2656
2927
  parts: [],
2657
- location: Location.fixed(line: lineno, char: char_pos, column: current_column)
2928
+ location:
2929
+ Location.fixed(line: lineno, char: char_pos, column: current_column)
2658
2930
  )
2659
2931
  end
2660
2932
 
@@ -2703,7 +2975,7 @@ module SyntaxTree
2703
2975
  def on_string_literal(string)
2704
2976
  heredoc = @heredocs[-1]
2705
2977
 
2706
- if heredoc && heredoc.ending
2978
+ if heredoc&.ending
2707
2979
  heredoc = @heredocs.pop
2708
2980
 
2709
2981
  Heredoc.new(
@@ -2756,7 +3028,13 @@ module SyntaxTree
2756
3028
  node =
2757
3029
  SymBeg.new(
2758
3030
  value: value,
2759
- location: Location.token(line: lineno, char: char_pos, column: current_column, size: value.size)
3031
+ location:
3032
+ Location.token(
3033
+ line: lineno,
3034
+ char: char_pos,
3035
+ column: current_column,
3036
+ size: value.size
3037
+ )
2760
3038
  )
2761
3039
 
2762
3040
  tokens << node
@@ -2810,7 +3088,13 @@ module SyntaxTree
2810
3088
  node =
2811
3089
  SymbolsBeg.new(
2812
3090
  value: value,
2813
- location: Location.token(line: lineno, char: char_pos, column: current_column, size: value.size)
3091
+ location:
3092
+ Location.token(
3093
+ line: lineno,
3094
+ char: char_pos,
3095
+ column: current_column,
3096
+ size: value.size
3097
+ )
2814
3098
  )
2815
3099
 
2816
3100
  tokens << node
@@ -2835,7 +3119,13 @@ module SyntaxTree
2835
3119
  node =
2836
3120
  TLambda.new(
2837
3121
  value: value,
2838
- location: Location.token(line: lineno, char: char_pos, column: current_column, size: value.size)
3122
+ location:
3123
+ Location.token(
3124
+ line: lineno,
3125
+ char: char_pos,
3126
+ column: current_column,
3127
+ size: value.size
3128
+ )
2839
3129
  )
2840
3130
 
2841
3131
  tokens << node
@@ -2848,7 +3138,13 @@ module SyntaxTree
2848
3138
  node =
2849
3139
  TLamBeg.new(
2850
3140
  value: value,
2851
- location: Location.token(line: lineno, char: char_pos, column: current_column, size: value.size)
3141
+ location:
3142
+ Location.token(
3143
+ line: lineno,
3144
+ char: char_pos,
3145
+ column: current_column,
3146
+ size: value.size
3147
+ )
2852
3148
  )
2853
3149
 
2854
3150
  tokens << node
@@ -2883,7 +3179,13 @@ module SyntaxTree
2883
3179
  node =
2884
3180
  TStringBeg.new(
2885
3181
  value: value,
2886
- location: Location.token(line: lineno, char: char_pos, column: current_column, size: value.size)
3182
+ location:
3183
+ Location.token(
3184
+ line: lineno,
3185
+ char: char_pos,
3186
+ column: current_column,
3187
+ size: value.size
3188
+ )
2887
3189
  )
2888
3190
 
2889
3191
  tokens << node
@@ -2895,7 +3197,13 @@ module SyntaxTree
2895
3197
  def on_tstring_content(value)
2896
3198
  TStringContent.new(
2897
3199
  value: value,
2898
- location: Location.token(line: lineno, char: char_pos, column: current_column, size: value.size)
3200
+ location:
3201
+ Location.token(
3202
+ line: lineno,
3203
+ char: char_pos,
3204
+ column: current_column,
3205
+ size: value.size
3206
+ )
2899
3207
  )
2900
3208
  end
2901
3209
 
@@ -2905,7 +3213,13 @@ module SyntaxTree
2905
3213
  node =
2906
3214
  TStringEnd.new(
2907
3215
  value: value,
2908
- location: Location.token(line: lineno, char: char_pos, column: current_column, size: value.size)
3216
+ location:
3217
+ Location.token(
3218
+ line: lineno,
3219
+ char: char_pos,
3220
+ column: current_column,
3221
+ size: value.size
3222
+ )
2909
3223
  )
2910
3224
 
2911
3225
  tokens << node
@@ -3014,7 +3328,7 @@ module SyntaxTree
3014
3328
  # some other block
3015
3329
  keyword = find_token(Kw, "do", consume: false)
3016
3330
  if keyword && keyword.location.start_char > predicate.location.end_char &&
3017
- keyword.location.end_char < ending.location.start_char
3331
+ keyword.location.end_char < ending.location.start_char
3018
3332
  tokens.delete(keyword)
3019
3333
  end
3020
3334
 
@@ -3081,7 +3395,10 @@ module SyntaxTree
3081
3395
 
3082
3396
  if pin && pin.location.start_char == value.location.start_char - 1
3083
3397
  tokens.delete(pin)
3084
- PinnedVarRef.new(value: value, location: pin.location.to(value.location))
3398
+ PinnedVarRef.new(
3399
+ value: value,
3400
+ location: pin.location.to(value.location)
3401
+ )
3085
3402
  else
3086
3403
  VarRef.new(value: value, location: value.location)
3087
3404
  end
@@ -3096,7 +3413,10 @@ module SyntaxTree
3096
3413
  # :call-seq:
3097
3414
  # on_void_stmt: () -> VoidStmt
3098
3415
  def on_void_stmt
3099
- VoidStmt.new(location: Location.fixed(line: lineno, char: char_pos, column: current_column))
3416
+ VoidStmt.new(
3417
+ location:
3418
+ Location.fixed(line: lineno, char: char_pos, column: current_column)
3419
+ )
3100
3420
  end
3101
3421
 
3102
3422
  # :call-seq:
@@ -3110,7 +3430,7 @@ module SyntaxTree
3110
3430
  ending = consequent || find_token(Kw, "end")
3111
3431
 
3112
3432
  statements_start = arguments
3113
- if token = find_token(Kw, "then", consume: false)
3433
+ if (token = find_token(Kw, "then", consume: false))
3114
3434
  tokens.delete(token)
3115
3435
  statements_start = token
3116
3436
  end
@@ -3119,7 +3439,8 @@ module SyntaxTree
3119
3439
 
3120
3440
  statements.bind(
3121
3441
  start_char,
3122
- start_char - line_counts[statements_start.location.start_line - 1].start,
3442
+ start_char -
3443
+ line_counts[statements_start.location.start_line - 1].start,
3123
3444
  ending.location.start_char,
3124
3445
  ending.location.start_column
3125
3446
  )
@@ -3142,7 +3463,7 @@ module SyntaxTree
3142
3463
  # some other block
3143
3464
  keyword = find_token(Kw, "do", consume: false)
3144
3465
  if keyword && keyword.location.start_char > predicate.location.end_char &&
3145
- keyword.location.end_char < ending.location.start_char
3466
+ keyword.location.end_char < ending.location.start_char
3146
3467
  tokens.delete(keyword)
3147
3468
  end
3148
3469
 
@@ -3188,7 +3509,11 @@ module SyntaxTree
3188
3509
  # :call-seq:
3189
3510
  # on_word_new: () -> Word
3190
3511
  def on_word_new
3191
- Word.new(parts: [], location: Location.fixed(line: lineno, char: char_pos, column: current_column))
3512
+ Word.new(
3513
+ parts: [],
3514
+ location:
3515
+ Location.fixed(line: lineno, char: char_pos, column: current_column)
3516
+ )
3192
3517
  end
3193
3518
 
3194
3519
  # :call-seq:
@@ -3207,7 +3532,13 @@ module SyntaxTree
3207
3532
  node =
3208
3533
  WordsBeg.new(
3209
3534
  value: value,
3210
- location: Location.token(line: lineno, char: char_pos, column: current_column, size: value.size)
3535
+ location:
3536
+ Location.token(
3537
+ line: lineno,
3538
+ char: char_pos,
3539
+ column: current_column,
3540
+ size: value.size
3541
+ )
3211
3542
  )
3212
3543
 
3213
3544
  tokens << node
@@ -3219,7 +3550,11 @@ module SyntaxTree
3219
3550
  def on_words_new
3220
3551
  beginning = find_token(WordsBeg)
3221
3552
 
3222
- Words.new(beginning: beginning, elements: [], location: beginning.location)
3553
+ Words.new(
3554
+ beginning: beginning,
3555
+ elements: [],
3556
+ location: beginning.location
3557
+ )
3223
3558
  end
3224
3559
 
3225
3560
  # def on_words_sep(value)