syntax_tree 2.2.0 → 2.4.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.
@@ -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
@@ -1563,24 +1677,32 @@ module SyntaxTree
1563
1677
  keyword_rest = VarField.new(value: nil, location: token.location)
1564
1678
  end
1565
1679
 
1680
+ parts = [constant, *keywords&.flatten(1), keyword_rest].compact
1681
+
1682
+ # If there's no constant, there may be braces, so we're going to look for
1683
+ # those to get our bounds.
1684
+ unless constant
1685
+ lbrace = find_token(LBrace, consume: false)
1686
+ rbrace = find_token(RBrace, consume: false)
1687
+
1688
+ if lbrace && rbrace
1689
+ parts = [lbrace, *parts, rbrace]
1690
+ tokens.delete(lbrace)
1691
+ tokens.delete(rbrace)
1692
+ end
1693
+ end
1694
+
1566
1695
  # Delete the optional then keyword
1567
- if token = find_token(Kw, "then", consume: false)
1696
+ if (token = find_token(Kw, "then", consume: false))
1697
+ parts << token
1568
1698
  tokens.delete(token)
1569
1699
  end
1570
1700
 
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
1701
  HshPtn.new(
1580
1702
  constant: constant,
1581
1703
  keywords: keywords || [],
1582
1704
  keyword_rest: keyword_rest,
1583
- location: location
1705
+ location: parts[0].location.to(parts[-1].location)
1584
1706
  )
1585
1707
  end
1586
1708
 
@@ -1589,7 +1711,13 @@ module SyntaxTree
1589
1711
  def on_ident(value)
1590
1712
  Ident.new(
1591
1713
  value: value,
1592
- location: Location.token(line: lineno, char: char_pos, column: current_column, size: value.size)
1714
+ location:
1715
+ Location.token(
1716
+ line: lineno,
1717
+ char: char_pos,
1718
+ column: current_column,
1719
+ size: value.size
1720
+ )
1593
1721
  )
1594
1722
  end
1595
1723
 
@@ -1654,7 +1782,13 @@ module SyntaxTree
1654
1782
  def on_imaginary(value)
1655
1783
  Imaginary.new(
1656
1784
  value: value,
1657
- location: Location.token(line: lineno, char: char_pos, column: current_column, size: value.size)
1785
+ location:
1786
+ Location.token(
1787
+ line: lineno,
1788
+ char: char_pos,
1789
+ column: current_column,
1790
+ size: value.size
1791
+ )
1658
1792
  )
1659
1793
  end
1660
1794
 
@@ -1673,7 +1807,7 @@ module SyntaxTree
1673
1807
  ending = consequent || find_token(Kw, "end")
1674
1808
 
1675
1809
  statements_start = pattern
1676
- if token = find_token(Kw, "then", consume: false)
1810
+ if (token = find_token(Kw, "then", consume: false))
1677
1811
  tokens.delete(token)
1678
1812
  statements_start = token
1679
1813
  end
@@ -1681,7 +1815,8 @@ module SyntaxTree
1681
1815
  start_char = find_next_statement_start(statements_start.location.end_char)
1682
1816
  statements.bind(
1683
1817
  start_char,
1684
- start_char - line_counts[statements_start.location.start_line - 1].start,
1818
+ start_char -
1819
+ line_counts[statements_start.location.start_line - 1].start,
1685
1820
  ending.location.start_char,
1686
1821
  ending.location.start_column
1687
1822
  )
@@ -1699,7 +1834,13 @@ module SyntaxTree
1699
1834
  def on_int(value)
1700
1835
  Int.new(
1701
1836
  value: value,
1702
- location: Location.token(line: lineno, char: char_pos, column: current_column, size: value.size)
1837
+ location:
1838
+ Location.token(
1839
+ line: lineno,
1840
+ char: char_pos,
1841
+ column: current_column,
1842
+ size: value.size
1843
+ )
1703
1844
  )
1704
1845
  end
1705
1846
 
@@ -1708,7 +1849,13 @@ module SyntaxTree
1708
1849
  def on_ivar(value)
1709
1850
  IVar.new(
1710
1851
  value: value,
1711
- location: Location.token(line: lineno, char: char_pos, column: current_column, size: value.size)
1852
+ location:
1853
+ Location.token(
1854
+ line: lineno,
1855
+ char: char_pos,
1856
+ column: current_column,
1857
+ size: value.size
1858
+ )
1712
1859
  )
1713
1860
  end
1714
1861
 
@@ -1718,7 +1865,13 @@ module SyntaxTree
1718
1865
  node =
1719
1866
  Kw.new(
1720
1867
  value: value,
1721
- location: Location.token(line: lineno, char: char_pos, column: current_column, size: value.size)
1868
+ location:
1869
+ Location.token(
1870
+ line: lineno,
1871
+ char: char_pos,
1872
+ column: current_column,
1873
+ size: value.size
1874
+ )
1722
1875
  )
1723
1876
 
1724
1877
  tokens << node
@@ -1739,7 +1892,13 @@ module SyntaxTree
1739
1892
  def on_label(value)
1740
1893
  Label.new(
1741
1894
  value: value,
1742
- location: Location.token(line: lineno, char: char_pos, column: current_column, size: value.size)
1895
+ location:
1896
+ Location.token(
1897
+ line: lineno,
1898
+ char: char_pos,
1899
+ column: current_column,
1900
+ size: value.size
1901
+ )
1743
1902
  )
1744
1903
  end
1745
1904
 
@@ -1749,7 +1908,13 @@ module SyntaxTree
1749
1908
  node =
1750
1909
  LabelEnd.new(
1751
1910
  value: value,
1752
- location: Location.token(line: lineno, char: char_pos, column: current_column, size: value.size)
1911
+ location:
1912
+ Location.token(
1913
+ line: lineno,
1914
+ char: char_pos,
1915
+ column: current_column,
1916
+ size: value.size
1917
+ )
1753
1918
  )
1754
1919
 
1755
1920
  tokens << node
@@ -1763,11 +1928,13 @@ module SyntaxTree
1763
1928
  # ) -> Lambda
1764
1929
  def on_lambda(params, statements)
1765
1930
  beginning = find_token(TLambda)
1766
-
1767
- if tokens.any? { |token|
1931
+ braces =
1932
+ tokens.any? do |token|
1768
1933
  token.is_a?(TLamBeg) &&
1769
1934
  token.location.start_char > beginning.location.start_char
1770
- }
1935
+ end
1936
+
1937
+ if braces
1771
1938
  opening = find_token(TLamBeg)
1772
1939
  closing = find_token(RBrace)
1773
1940
  else
@@ -1795,7 +1962,13 @@ module SyntaxTree
1795
1962
  node =
1796
1963
  LBrace.new(
1797
1964
  value: value,
1798
- location: Location.token(line: lineno, char: char_pos, column: current_column, size: value.size)
1965
+ location:
1966
+ Location.token(
1967
+ line: lineno,
1968
+ char: char_pos,
1969
+ column: current_column,
1970
+ size: value.size
1971
+ )
1799
1972
  )
1800
1973
 
1801
1974
  tokens << node
@@ -1808,7 +1981,13 @@ module SyntaxTree
1808
1981
  node =
1809
1982
  LBracket.new(
1810
1983
  value: value,
1811
- location: Location.token(line: lineno, char: char_pos, column: current_column, size: value.size)
1984
+ location:
1985
+ Location.token(
1986
+ line: lineno,
1987
+ char: char_pos,
1988
+ column: current_column,
1989
+ size: value.size
1990
+ )
1812
1991
  )
1813
1992
 
1814
1993
  tokens << node
@@ -1821,7 +2000,13 @@ module SyntaxTree
1821
2000
  node =
1822
2001
  LParen.new(
1823
2002
  value: value,
1824
- location: Location.token(line: lineno, char: char_pos, column: current_column, size: value.size)
2003
+ location:
2004
+ Location.token(
2005
+ line: lineno,
2006
+ char: char_pos,
2007
+ column: current_column,
2008
+ size: value.size
2009
+ )
1825
2010
  )
1826
2011
 
1827
2012
  tokens << node
@@ -1920,7 +2105,11 @@ module SyntaxTree
1920
2105
  # :call-seq:
1921
2106
  # on_mlhs_new: () -> MLHS
1922
2107
  def on_mlhs_new
1923
- MLHS.new(parts: [], location: Location.fixed(line: lineno, char: char_pos, column: current_column))
2108
+ MLHS.new(
2109
+ parts: [],
2110
+ location:
2111
+ Location.fixed(line: lineno, char: char_pos, column: current_column)
2112
+ )
1924
2113
  end
1925
2114
 
1926
2115
  # :call-seq:
@@ -1965,18 +2154,18 @@ module SyntaxTree
1965
2154
  # :call-seq:
1966
2155
  # on_mrhs_new: () -> MRHS
1967
2156
  def on_mrhs_new
1968
- MRHS.new(parts: [], location: Location.fixed(line: lineno, char: char_pos, column: current_column))
2157
+ MRHS.new(
2158
+ parts: [],
2159
+ location:
2160
+ Location.fixed(line: lineno, char: char_pos, column: current_column)
2161
+ )
1969
2162
  end
1970
2163
 
1971
2164
  # :call-seq:
1972
2165
  # on_mrhs_add: (MRHS mrhs, untyped part) -> MRHS
1973
2166
  def on_mrhs_add(mrhs, part)
1974
2167
  location =
1975
- if mrhs.parts.empty?
1976
- mrhs.location
1977
- else
1978
- mrhs.location.to(part.location)
1979
- end
2168
+ (mrhs.parts.empty? ? mrhs.location : mrhs.location.to(part.location))
1980
2169
 
1981
2170
  MRHS.new(parts: mrhs.parts << part, location: location)
1982
2171
  end
@@ -2034,7 +2223,13 @@ module SyntaxTree
2034
2223
  node =
2035
2224
  Op.new(
2036
2225
  value: value,
2037
- location: Location.token(line: lineno, char: char_pos, column: current_column, size: value.size)
2226
+ location:
2227
+ Location.token(
2228
+ line: lineno,
2229
+ char: char_pos,
2230
+ column: current_column,
2231
+ size: value.size
2232
+ )
2038
2233
  )
2039
2234
 
2040
2235
  tokens << node
@@ -2043,7 +2238,13 @@ module SyntaxTree
2043
2238
 
2044
2239
  # :call-seq:
2045
2240
  # on_opassign: (
2046
- # (ARefField | ConstPathField | Field | TopConstField | VarField) target,
2241
+ # (
2242
+ # ARefField |
2243
+ # ConstPathField |
2244
+ # Field |
2245
+ # TopConstField |
2246
+ # VarField
2247
+ # ) target,
2047
2248
  # Op operator,
2048
2249
  # untyped value
2049
2250
  # ) -> OpAssign
@@ -2079,12 +2280,16 @@ module SyntaxTree
2079
2280
  keyword_rest,
2080
2281
  block
2081
2282
  )
2283
+ # This is to make it so that required keyword arguments
2284
+ # have a `nil` for the value instead of a `false`.
2285
+ keywords&.map! { |(key, value)| [key, value || nil] }
2286
+
2082
2287
  parts = [
2083
2288
  *requireds,
2084
2289
  *optionals&.flatten(1),
2085
2290
  rest,
2086
2291
  *posts,
2087
- *keywords&.flat_map { |(key, value)| [key, value || nil] },
2292
+ *keywords&.flatten(1),
2088
2293
  (keyword_rest if keyword_rest != :nil),
2089
2294
  (block if block != :&)
2090
2295
  ].compact
@@ -2114,14 +2319,15 @@ module SyntaxTree
2114
2319
  lparen = find_token(LParen)
2115
2320
  rparen = find_token(RParen)
2116
2321
 
2117
- if contents && contents.is_a?(Params)
2322
+ if contents.is_a?(Params)
2118
2323
  location = contents.location
2119
2324
  start_char = find_next_statement_start(lparen.location.end_char)
2120
2325
  location =
2121
2326
  Location.new(
2122
2327
  start_line: location.start_line,
2123
2328
  start_char: start_char,
2124
- start_column: start_char - line_counts[lparen.location.start_line - 1].start,
2329
+ start_column:
2330
+ start_char - line_counts[lparen.location.start_line - 1].start,
2125
2331
  end_line: location.end_line,
2126
2332
  end_char: rparen.location.start_char,
2127
2333
  end_column: rparen.location.start_column
@@ -2162,7 +2368,13 @@ module SyntaxTree
2162
2368
  def on_period(value)
2163
2369
  Period.new(
2164
2370
  value: value,
2165
- location: Location.token(line: lineno, char: char_pos, column: current_column, size: value.size)
2371
+ location:
2372
+ Location.token(
2373
+ line: lineno,
2374
+ char: char_pos,
2375
+ column: current_column,
2376
+ size: value.size
2377
+ )
2166
2378
  )
2167
2379
  end
2168
2380
 
@@ -2294,7 +2506,13 @@ module SyntaxTree
2294
2506
  node =
2295
2507
  QSymbolsBeg.new(
2296
2508
  value: value,
2297
- location: Location.token(line: lineno, char: char_pos, column: current_column, size: value.size)
2509
+ location:
2510
+ Location.token(
2511
+ line: lineno,
2512
+ char: char_pos,
2513
+ column: current_column,
2514
+ size: value.size
2515
+ )
2298
2516
  )
2299
2517
 
2300
2518
  tokens << node
@@ -2329,7 +2547,13 @@ module SyntaxTree
2329
2547
  node =
2330
2548
  QWordsBeg.new(
2331
2549
  value: value,
2332
- location: Location.token(line: lineno, char: char_pos, column: current_column, size: value.size)
2550
+ location:
2551
+ Location.token(
2552
+ line: lineno,
2553
+ char: char_pos,
2554
+ column: current_column,
2555
+ size: value.size
2556
+ )
2333
2557
  )
2334
2558
 
2335
2559
  tokens << node
@@ -2341,7 +2565,11 @@ module SyntaxTree
2341
2565
  def on_qwords_new
2342
2566
  beginning = find_token(QWordsBeg)
2343
2567
 
2344
- QWords.new(beginning: beginning, elements: [], location: beginning.location)
2568
+ QWords.new(
2569
+ beginning: beginning,
2570
+ elements: [],
2571
+ location: beginning.location
2572
+ )
2345
2573
  end
2346
2574
 
2347
2575
  # :call-seq:
@@ -2349,7 +2577,13 @@ module SyntaxTree
2349
2577
  def on_rational(value)
2350
2578
  RationalLiteral.new(
2351
2579
  value: value,
2352
- location: Location.token(line: lineno, char: char_pos, column: current_column, size: value.size)
2580
+ location:
2581
+ Location.token(
2582
+ line: lineno,
2583
+ char: char_pos,
2584
+ column: current_column,
2585
+ size: value.size
2586
+ )
2353
2587
  )
2354
2588
  end
2355
2589
 
@@ -2359,7 +2593,13 @@ module SyntaxTree
2359
2593
  node =
2360
2594
  RBrace.new(
2361
2595
  value: value,
2362
- location: Location.token(line: lineno, char: char_pos, column: current_column, size: value.size)
2596
+ location:
2597
+ Location.token(
2598
+ line: lineno,
2599
+ char: char_pos,
2600
+ column: current_column,
2601
+ size: value.size
2602
+ )
2363
2603
  )
2364
2604
 
2365
2605
  tokens << node
@@ -2372,7 +2612,13 @@ module SyntaxTree
2372
2612
  node =
2373
2613
  RBracket.new(
2374
2614
  value: value,
2375
- location: Location.token(line: lineno, char: char_pos, column: current_column, size: value.size)
2615
+ location:
2616
+ Location.token(
2617
+ line: lineno,
2618
+ char: char_pos,
2619
+ column: current_column,
2620
+ size: value.size
2621
+ )
2376
2622
  )
2377
2623
 
2378
2624
  tokens << node
@@ -2406,7 +2652,13 @@ module SyntaxTree
2406
2652
  node =
2407
2653
  RegexpBeg.new(
2408
2654
  value: value,
2409
- location: Location.token(line: lineno, char: char_pos, column: current_column, size: value.size)
2655
+ location:
2656
+ Location.token(
2657
+ line: lineno,
2658
+ char: char_pos,
2659
+ column: current_column,
2660
+ size: value.size
2661
+ )
2410
2662
  )
2411
2663
 
2412
2664
  tokens << node
@@ -2418,7 +2670,13 @@ module SyntaxTree
2418
2670
  def on_regexp_end(value)
2419
2671
  RegexpEnd.new(
2420
2672
  value: value,
2421
- location: Location.token(line: lineno, char: char_pos, column: current_column, size: value.size)
2673
+ location:
2674
+ Location.token(
2675
+ line: lineno,
2676
+ char: char_pos,
2677
+ column: current_column,
2678
+ size: value.size
2679
+ )
2422
2680
  )
2423
2681
  end
2424
2682
 
@@ -2559,7 +2817,13 @@ module SyntaxTree
2559
2817
  node =
2560
2818
  RParen.new(
2561
2819
  value: value,
2562
- location: Location.token(line: lineno, char: char_pos, column: current_column, size: value.size)
2820
+ location:
2821
+ Location.token(
2822
+ line: lineno,
2823
+ char: char_pos,
2824
+ column: current_column,
2825
+ size: value.size
2826
+ )
2563
2827
  )
2564
2828
 
2565
2829
  tokens << node
@@ -2607,7 +2871,11 @@ module SyntaxTree
2607
2871
  statements.location.to(statement.location)
2608
2872
  end
2609
2873
 
2610
- Statements.new(self, body: statements.body << statement, location: location)
2874
+ Statements.new(
2875
+ self,
2876
+ body: statements.body << statement,
2877
+ location: location
2878
+ )
2611
2879
  end
2612
2880
 
2613
2881
  # :call-seq:
@@ -2616,7 +2884,8 @@ module SyntaxTree
2616
2884
  Statements.new(
2617
2885
  self,
2618
2886
  body: [],
2619
- location: Location.fixed(line: lineno, char: char_pos, column: current_column)
2887
+ location:
2888
+ Location.fixed(line: lineno, char: char_pos, column: current_column)
2620
2889
  )
2621
2890
  end
2622
2891
 
@@ -2650,7 +2919,8 @@ module SyntaxTree
2650
2919
  def on_string_content
2651
2920
  StringContent.new(
2652
2921
  parts: [],
2653
- location: Location.fixed(line: lineno, char: char_pos, column: current_column)
2922
+ location:
2923
+ Location.fixed(line: lineno, char: char_pos, column: current_column)
2654
2924
  )
2655
2925
  end
2656
2926
 
@@ -2699,7 +2969,7 @@ module SyntaxTree
2699
2969
  def on_string_literal(string)
2700
2970
  heredoc = @heredocs[-1]
2701
2971
 
2702
- if heredoc && heredoc.ending
2972
+ if heredoc&.ending
2703
2973
  heredoc = @heredocs.pop
2704
2974
 
2705
2975
  Heredoc.new(
@@ -2752,7 +3022,13 @@ module SyntaxTree
2752
3022
  node =
2753
3023
  SymBeg.new(
2754
3024
  value: value,
2755
- location: Location.token(line: lineno, char: char_pos, column: current_column, size: value.size)
3025
+ location:
3026
+ Location.token(
3027
+ line: lineno,
3028
+ char: char_pos,
3029
+ column: current_column,
3030
+ size: value.size
3031
+ )
2756
3032
  )
2757
3033
 
2758
3034
  tokens << node
@@ -2806,7 +3082,13 @@ module SyntaxTree
2806
3082
  node =
2807
3083
  SymbolsBeg.new(
2808
3084
  value: value,
2809
- location: Location.token(line: lineno, char: char_pos, column: current_column, size: value.size)
3085
+ location:
3086
+ Location.token(
3087
+ line: lineno,
3088
+ char: char_pos,
3089
+ column: current_column,
3090
+ size: value.size
3091
+ )
2810
3092
  )
2811
3093
 
2812
3094
  tokens << node
@@ -2831,7 +3113,13 @@ module SyntaxTree
2831
3113
  node =
2832
3114
  TLambda.new(
2833
3115
  value: value,
2834
- location: Location.token(line: lineno, char: char_pos, column: current_column, size: value.size)
3116
+ location:
3117
+ Location.token(
3118
+ line: lineno,
3119
+ char: char_pos,
3120
+ column: current_column,
3121
+ size: value.size
3122
+ )
2835
3123
  )
2836
3124
 
2837
3125
  tokens << node
@@ -2844,7 +3132,13 @@ module SyntaxTree
2844
3132
  node =
2845
3133
  TLamBeg.new(
2846
3134
  value: value,
2847
- location: Location.token(line: lineno, char: char_pos, column: current_column, size: value.size)
3135
+ location:
3136
+ Location.token(
3137
+ line: lineno,
3138
+ char: char_pos,
3139
+ column: current_column,
3140
+ size: value.size
3141
+ )
2848
3142
  )
2849
3143
 
2850
3144
  tokens << node
@@ -2879,7 +3173,13 @@ module SyntaxTree
2879
3173
  node =
2880
3174
  TStringBeg.new(
2881
3175
  value: value,
2882
- location: Location.token(line: lineno, char: char_pos, column: current_column, size: value.size)
3176
+ location:
3177
+ Location.token(
3178
+ line: lineno,
3179
+ char: char_pos,
3180
+ column: current_column,
3181
+ size: value.size
3182
+ )
2883
3183
  )
2884
3184
 
2885
3185
  tokens << node
@@ -2891,7 +3191,13 @@ module SyntaxTree
2891
3191
  def on_tstring_content(value)
2892
3192
  TStringContent.new(
2893
3193
  value: value,
2894
- location: Location.token(line: lineno, char: char_pos, column: current_column, size: value.size)
3194
+ location:
3195
+ Location.token(
3196
+ line: lineno,
3197
+ char: char_pos,
3198
+ column: current_column,
3199
+ size: value.size
3200
+ )
2895
3201
  )
2896
3202
  end
2897
3203
 
@@ -2901,7 +3207,13 @@ module SyntaxTree
2901
3207
  node =
2902
3208
  TStringEnd.new(
2903
3209
  value: value,
2904
- location: Location.token(line: lineno, char: char_pos, column: current_column, size: value.size)
3210
+ location:
3211
+ Location.token(
3212
+ line: lineno,
3213
+ char: char_pos,
3214
+ column: current_column,
3215
+ size: value.size
3216
+ )
2905
3217
  )
2906
3218
 
2907
3219
  tokens << node
@@ -3010,7 +3322,7 @@ module SyntaxTree
3010
3322
  # some other block
3011
3323
  keyword = find_token(Kw, "do", consume: false)
3012
3324
  if keyword && keyword.location.start_char > predicate.location.end_char &&
3013
- keyword.location.end_char < ending.location.start_char
3325
+ keyword.location.end_char < ending.location.start_char
3014
3326
  tokens.delete(keyword)
3015
3327
  end
3016
3328
 
@@ -3077,7 +3389,10 @@ module SyntaxTree
3077
3389
 
3078
3390
  if pin && pin.location.start_char == value.location.start_char - 1
3079
3391
  tokens.delete(pin)
3080
- PinnedVarRef.new(value: value, location: pin.location.to(value.location))
3392
+ PinnedVarRef.new(
3393
+ value: value,
3394
+ location: pin.location.to(value.location)
3395
+ )
3081
3396
  else
3082
3397
  VarRef.new(value: value, location: value.location)
3083
3398
  end
@@ -3092,7 +3407,10 @@ module SyntaxTree
3092
3407
  # :call-seq:
3093
3408
  # on_void_stmt: () -> VoidStmt
3094
3409
  def on_void_stmt
3095
- VoidStmt.new(location: Location.fixed(line: lineno, char: char_pos, column: current_column))
3410
+ VoidStmt.new(
3411
+ location:
3412
+ Location.fixed(line: lineno, char: char_pos, column: current_column)
3413
+ )
3096
3414
  end
3097
3415
 
3098
3416
  # :call-seq:
@@ -3106,7 +3424,7 @@ module SyntaxTree
3106
3424
  ending = consequent || find_token(Kw, "end")
3107
3425
 
3108
3426
  statements_start = arguments
3109
- if token = find_token(Kw, "then", consume: false)
3427
+ if (token = find_token(Kw, "then", consume: false))
3110
3428
  tokens.delete(token)
3111
3429
  statements_start = token
3112
3430
  end
@@ -3115,7 +3433,8 @@ module SyntaxTree
3115
3433
 
3116
3434
  statements.bind(
3117
3435
  start_char,
3118
- start_char - line_counts[statements_start.location.start_line - 1].start,
3436
+ start_char -
3437
+ line_counts[statements_start.location.start_line - 1].start,
3119
3438
  ending.location.start_char,
3120
3439
  ending.location.start_column
3121
3440
  )
@@ -3138,7 +3457,7 @@ module SyntaxTree
3138
3457
  # some other block
3139
3458
  keyword = find_token(Kw, "do", consume: false)
3140
3459
  if keyword && keyword.location.start_char > predicate.location.end_char &&
3141
- keyword.location.end_char < ending.location.start_char
3460
+ keyword.location.end_char < ending.location.start_char
3142
3461
  tokens.delete(keyword)
3143
3462
  end
3144
3463
 
@@ -3184,7 +3503,11 @@ module SyntaxTree
3184
3503
  # :call-seq:
3185
3504
  # on_word_new: () -> Word
3186
3505
  def on_word_new
3187
- Word.new(parts: [], location: Location.fixed(line: lineno, char: char_pos, column: current_column))
3506
+ Word.new(
3507
+ parts: [],
3508
+ location:
3509
+ Location.fixed(line: lineno, char: char_pos, column: current_column)
3510
+ )
3188
3511
  end
3189
3512
 
3190
3513
  # :call-seq:
@@ -3203,7 +3526,13 @@ module SyntaxTree
3203
3526
  node =
3204
3527
  WordsBeg.new(
3205
3528
  value: value,
3206
- location: Location.token(line: lineno, char: char_pos, column: current_column, size: value.size)
3529
+ location:
3530
+ Location.token(
3531
+ line: lineno,
3532
+ char: char_pos,
3533
+ column: current_column,
3534
+ size: value.size
3535
+ )
3207
3536
  )
3208
3537
 
3209
3538
  tokens << node
@@ -3215,7 +3544,11 @@ module SyntaxTree
3215
3544
  def on_words_new
3216
3545
  beginning = find_token(WordsBeg)
3217
3546
 
3218
- Words.new(beginning: beginning, elements: [], location: beginning.location)
3547
+ Words.new(
3548
+ beginning: beginning,
3549
+ elements: [],
3550
+ location: beginning.location
3551
+ )
3219
3552
  end
3220
3553
 
3221
3554
  # def on_words_sep(value)