syntax_tree 2.3.1 → 2.5.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.
@@ -3,9 +3,21 @@
3
3
  module SyntaxTree
4
4
  # Represents the location of a node in the tree from the source code.
5
5
  class Location
6
- attr_reader :start_line, :start_char, :start_column, :end_line, :end_char, :end_column
6
+ attr_reader :start_line,
7
+ :start_char,
8
+ :start_column,
9
+ :end_line,
10
+ :end_char,
11
+ :end_column
7
12
 
8
- def initialize(start_line:, start_char:, start_column:, end_line:, end_char:, end_column:)
13
+ def initialize(
14
+ start_line:,
15
+ start_char:,
16
+ start_column:,
17
+ end_line:,
18
+ end_char:,
19
+ end_column:
20
+ )
9
21
  @start_line = start_line
10
22
  @start_char = start_char
11
23
  @start_column = start_column
@@ -39,7 +51,7 @@ module SyntaxTree
39
51
  [start_line, start_char, start_column, end_line, end_char, end_column]
40
52
  end
41
53
 
42
- def deconstruct_keys(keys)
54
+ def deconstruct_keys(_keys)
43
55
  {
44
56
  start_line: start_line,
45
57
  start_char: start_char,
@@ -62,7 +74,14 @@ module SyntaxTree
62
74
  end
63
75
 
64
76
  def self.fixed(line:, char:, column:)
65
- new(start_line: line, start_char: char, start_column: column, end_line: line, end_char: char, end_column: column)
77
+ new(
78
+ start_line: line,
79
+ start_char: char,
80
+ start_column: column,
81
+ end_line: line,
82
+ end_char: char,
83
+ end_column: column
84
+ )
66
85
  end
67
86
  end
68
87
 
@@ -102,6 +121,10 @@ module SyntaxTree
102
121
  visitor = Visitor::JSONVisitor.new
103
122
  visitor.visit(self).to_json(*opts)
104
123
  end
124
+
125
+ def construct_keys
126
+ PrettierPrint.format(+"") { |q| Visitor::MatchVisitor.new(q).visit(self) }
127
+ end
105
128
  end
106
129
 
107
130
  # BEGINBlock represents the use of the +BEGIN+ keyword, which hooks into the
@@ -140,7 +163,7 @@ module SyntaxTree
140
163
 
141
164
  alias deconstruct child_nodes
142
165
 
143
- def deconstruct_keys(keys)
166
+ def deconstruct_keys(_keys)
144
167
  {
145
168
  lbrace: lbrace,
146
169
  statements: statements,
@@ -192,7 +215,7 @@ module SyntaxTree
192
215
 
193
216
  alias deconstruct child_nodes
194
217
 
195
- def deconstruct_keys(keys)
218
+ def deconstruct_keys(_keys)
196
219
  { value: value, location: location, comments: comments }
197
220
  end
198
221
 
@@ -243,7 +266,7 @@ module SyntaxTree
243
266
 
244
267
  alias deconstruct child_nodes
245
268
 
246
- def deconstruct_keys(keys)
269
+ def deconstruct_keys(_keys)
247
270
  {
248
271
  lbrace: lbrace,
249
272
  statements: statements,
@@ -298,7 +321,7 @@ module SyntaxTree
298
321
 
299
322
  alias deconstruct child_nodes
300
323
 
301
- def deconstruct_keys(keys)
324
+ def deconstruct_keys(_keys)
302
325
  { value: value, location: location, comments: comments }
303
326
  end
304
327
 
@@ -323,6 +346,8 @@ module SyntaxTree
323
346
  # symbols (note that this includes dynamic symbols like
324
347
  # :"left-#{middle}-right").
325
348
  class Alias < Node
349
+ # Formats an argument to the alias keyword. For symbol literals it uses the
350
+ # value of the symbol directly to look like bare words.
326
351
  class AliasArgumentFormatter
327
352
  # [DynaSymbol | SymbolLiteral] the argument being passed to alias
328
353
  attr_reader :argument
@@ -374,7 +399,7 @@ module SyntaxTree
374
399
 
375
400
  alias deconstruct child_nodes
376
401
 
377
- def deconstruct_keys(keys)
402
+ def deconstruct_keys(_keys)
378
403
  { left: left, right: right, location: location, comments: comments }
379
404
  end
380
405
 
@@ -435,7 +460,7 @@ module SyntaxTree
435
460
 
436
461
  alias deconstruct child_nodes
437
462
 
438
- def deconstruct_keys(keys)
463
+ def deconstruct_keys(_keys)
439
464
  {
440
465
  collection: collection,
441
466
  index: index,
@@ -496,7 +521,7 @@ module SyntaxTree
496
521
 
497
522
  alias deconstruct child_nodes
498
523
 
499
- def deconstruct_keys(keys)
524
+ def deconstruct_keys(_keys)
500
525
  {
501
526
  collection: collection,
502
527
  index: index,
@@ -558,7 +583,7 @@ module SyntaxTree
558
583
 
559
584
  alias deconstruct child_nodes
560
585
 
561
- def deconstruct_keys(keys)
586
+ def deconstruct_keys(_keys)
562
587
  { arguments: arguments, location: location, comments: comments }
563
588
  end
564
589
 
@@ -606,7 +631,7 @@ module SyntaxTree
606
631
 
607
632
  alias deconstruct child_nodes
608
633
 
609
- def deconstruct_keys(keys)
634
+ def deconstruct_keys(_keys)
610
635
  { parts: parts, location: location, comments: comments }
611
636
  end
612
637
 
@@ -642,7 +667,7 @@ module SyntaxTree
642
667
 
643
668
  alias deconstruct child_nodes
644
669
 
645
- def deconstruct_keys(keys)
670
+ def deconstruct_keys(_keys)
646
671
  { value: value, location: location, comments: comments }
647
672
  end
648
673
 
@@ -679,7 +704,7 @@ module SyntaxTree
679
704
 
680
705
  alias deconstruct child_nodes
681
706
 
682
- def deconstruct_keys(keys)
707
+ def deconstruct_keys(_keys)
683
708
  { value: value, location: location, comments: comments }
684
709
  end
685
710
 
@@ -729,7 +754,7 @@ module SyntaxTree
729
754
 
730
755
  alias deconstruct child_nodes
731
756
 
732
- def deconstruct_keys(keys)
757
+ def deconstruct_keys(_keys)
733
758
  { value: value, location: location, comments: comments }
734
759
  end
735
760
 
@@ -745,6 +770,7 @@ module SyntaxTree
745
770
  # [one, two, three]
746
771
  #
747
772
  class ArrayLiteral < Node
773
+ # Formats an array of multiple simple string literals into the %w syntax.
748
774
  class QWordsFormatter
749
775
  # [Args] the contents of the array
750
776
  attr_reader :contents
@@ -761,7 +787,7 @@ module SyntaxTree
761
787
  if part.is_a?(StringLiteral)
762
788
  q.format(part.parts.first)
763
789
  else
764
- q.text(part.value[1..-1])
790
+ q.text(part.value[1..])
765
791
  end
766
792
  end
767
793
  end
@@ -770,6 +796,7 @@ module SyntaxTree
770
796
  end
771
797
  end
772
798
 
799
+ # Formats an array of multiple simple symbol literals into the %i syntax.
773
800
  class QSymbolsFormatter
774
801
  # [Args] the contents of the array
775
802
  attr_reader :contents
@@ -791,6 +818,28 @@ module SyntaxTree
791
818
  end
792
819
  end
793
820
 
821
+ # Formats an array that contains only a list of variable references. To make
822
+ # things simpler, if there are a bunch, we format them all using the "fill"
823
+ # algorithm as opposed to breaking them into a ton of lines. For example,
824
+ #
825
+ # [foo, bar, baz]
826
+ #
827
+ # instead of becoming:
828
+ #
829
+ # [
830
+ # foo,
831
+ # bar,
832
+ # baz
833
+ # ]
834
+ #
835
+ # would instead become:
836
+ #
837
+ # [
838
+ # foo, bar,
839
+ # baz
840
+ # ]
841
+ #
842
+ # provided the line length was hit between `bar` and `baz`.
794
843
  class VarRefsFormatter
795
844
  # [Args] the contents of the array
796
845
  attr_reader :contents
@@ -816,6 +865,9 @@ module SyntaxTree
816
865
  end
817
866
  end
818
867
 
868
+ # This is a special formatter used if the array literal contains no values
869
+ # but _does_ contain comments. In this case we do some special formatting to
870
+ # make sure the comments gets indented properly.
819
871
  class EmptyWithCommentsFormatter
820
872
  # [LBracket] the opening bracket
821
873
  attr_reader :lbracket
@@ -865,7 +917,7 @@ module SyntaxTree
865
917
 
866
918
  alias deconstruct child_nodes
867
919
 
868
- def deconstruct_keys(keys)
920
+ def deconstruct_keys(_keys)
869
921
  {
870
922
  lbracket: lbracket,
871
923
  contents: contents,
@@ -951,7 +1003,8 @@ module SyntaxTree
951
1003
  # If we have an empty array that contains only comments, then we're going
952
1004
  # to do some special printing to ensure they get indented correctly.
953
1005
  def empty_with_comments?
954
- contents.nil? && lbracket.comments.any? && lbracket.comments.none?(&:inline?)
1006
+ contents.nil? && lbracket.comments.any? &&
1007
+ lbracket.comments.none?(&:inline?)
955
1008
  end
956
1009
  end
957
1010
 
@@ -973,6 +1026,7 @@ module SyntaxTree
973
1026
  # and an optional array of positional matches that occur after the splat.
974
1027
  # All of the in clauses above would create an AryPtn node.
975
1028
  class AryPtn < Node
1029
+ # Formats the optional splat of an array pattern.
976
1030
  class RestFormatter
977
1031
  # [VarField] the identifier that represents the remaining positionals
978
1032
  attr_reader :value
@@ -1035,7 +1089,7 @@ module SyntaxTree
1035
1089
 
1036
1090
  alias deconstruct child_nodes
1037
1091
 
1038
- def deconstruct_keys(keys)
1092
+ def deconstruct_keys(_keys)
1039
1093
  {
1040
1094
  constant: constant,
1041
1095
  requireds: requireds,
@@ -1067,6 +1121,8 @@ module SyntaxTree
1067
1121
  q.text("[")
1068
1122
  q.seplist(parts) { |part| q.format(part) }
1069
1123
  q.text("]")
1124
+ elsif parts.empty?
1125
+ q.text("[]")
1070
1126
  else
1071
1127
  q.group { q.seplist(parts) { |part| q.format(part) } }
1072
1128
  end
@@ -1077,7 +1133,8 @@ module SyntaxTree
1077
1133
  module AssignFormatting
1078
1134
  def self.skip_indent?(value)
1079
1135
  case value
1080
- in ArrayLiteral | HashLiteral | Heredoc | Lambda | QSymbols | QWords | Symbols | Words
1136
+ in ArrayLiteral | HashLiteral | Heredoc | Lambda | QSymbols | QWords |
1137
+ Symbols | Words
1081
1138
  true
1082
1139
  in Call[receiver:]
1083
1140
  skip_indent?(receiver)
@@ -1123,7 +1180,7 @@ module SyntaxTree
1123
1180
 
1124
1181
  alias deconstruct child_nodes
1125
1182
 
1126
- def deconstruct_keys(keys)
1183
+ def deconstruct_keys(_keys)
1127
1184
  { target: target, value: value, location: location, comments: comments }
1128
1185
  end
1129
1186
 
@@ -1185,12 +1242,12 @@ module SyntaxTree
1185
1242
 
1186
1243
  alias deconstruct child_nodes
1187
1244
 
1188
- def deconstruct_keys(keys)
1245
+ def deconstruct_keys(_keys)
1189
1246
  { key: key, value: value, location: location, comments: comments }
1190
1247
  end
1191
1248
 
1192
1249
  def format(q)
1193
- if value&.is_a?(HashLiteral)
1250
+ if value.is_a?(HashLiteral)
1194
1251
  format_contents(q)
1195
1252
  else
1196
1253
  q.group { format_contents(q) }
@@ -1243,7 +1300,7 @@ module SyntaxTree
1243
1300
 
1244
1301
  alias deconstruct child_nodes
1245
1302
 
1246
- def deconstruct_keys(keys)
1303
+ def deconstruct_keys(_keys)
1247
1304
  { value: value, location: location, comments: comments }
1248
1305
  end
1249
1306
 
@@ -1281,7 +1338,7 @@ module SyntaxTree
1281
1338
 
1282
1339
  alias deconstruct child_nodes
1283
1340
 
1284
- def deconstruct_keys(keys)
1341
+ def deconstruct_keys(_keys)
1285
1342
  { value: value, location: location, comments: comments }
1286
1343
  end
1287
1344
 
@@ -1316,7 +1373,7 @@ module SyntaxTree
1316
1373
 
1317
1374
  alias deconstruct child_nodes
1318
1375
 
1319
- def deconstruct_keys(keys)
1376
+ def deconstruct_keys(_keys)
1320
1377
  { value: value, location: location, comments: comments }
1321
1378
  end
1322
1379
 
@@ -1329,21 +1386,28 @@ module SyntaxTree
1329
1386
  # hash or bare hash. It first determines if every key in the hash can use
1330
1387
  # labels. If it can, it uses labels. Otherwise it uses hash rockets.
1331
1388
  module HashKeyFormatter
1389
+ # Formats the keys of a hash literal using labels.
1332
1390
  class Labels
1391
+ LABEL = /\A[A-Za-z_](\w*[\w!?])?\z/
1392
+
1333
1393
  def format_key(q, key)
1334
1394
  case key
1335
- when Label
1395
+ in Label
1336
1396
  q.format(key)
1337
- when SymbolLiteral
1397
+ in SymbolLiteral
1338
1398
  q.format(key.value)
1339
1399
  q.text(":")
1340
- when DynaSymbol
1400
+ in DynaSymbol[parts: [TStringContent[value: LABEL] => part]]
1401
+ q.format(part)
1402
+ q.text(":")
1403
+ in DynaSymbol
1341
1404
  q.format(key)
1342
1405
  q.text(":")
1343
1406
  end
1344
1407
  end
1345
1408
  end
1346
1409
 
1410
+ # Formats the keys of a hash literal using hash rockets.
1347
1411
  class Rockets
1348
1412
  def format_key(q, key)
1349
1413
  case key
@@ -1417,7 +1481,7 @@ module SyntaxTree
1417
1481
 
1418
1482
  alias deconstruct child_nodes
1419
1483
 
1420
- def deconstruct_keys(keys)
1484
+ def deconstruct_keys(_keys)
1421
1485
  { assocs: assocs, location: location, comments: comments }
1422
1486
  end
1423
1487
 
@@ -1459,7 +1523,7 @@ module SyntaxTree
1459
1523
 
1460
1524
  alias deconstruct child_nodes
1461
1525
 
1462
- def deconstruct_keys(keys)
1526
+ def deconstruct_keys(_keys)
1463
1527
  { bodystmt: bodystmt, location: location, comments: comments }
1464
1528
  end
1465
1529
 
@@ -1507,7 +1571,7 @@ module SyntaxTree
1507
1571
 
1508
1572
  alias deconstruct child_nodes
1509
1573
 
1510
- def deconstruct_keys(keys)
1574
+ def deconstruct_keys(_keys)
1511
1575
  { statement: statement, location: location, comments: comments }
1512
1576
  end
1513
1577
 
@@ -1567,7 +1631,7 @@ module SyntaxTree
1567
1631
 
1568
1632
  alias deconstruct child_nodes
1569
1633
 
1570
- def deconstruct_keys(keys)
1634
+ def deconstruct_keys(_keys)
1571
1635
  {
1572
1636
  left: left,
1573
1637
  operator: operator,
@@ -1602,52 +1666,6 @@ module SyntaxTree
1602
1666
  end
1603
1667
  end
1604
1668
 
1605
- # This module will remove any breakables from the list of contents so that no
1606
- # newlines are present in the output.
1607
- module RemoveBreaks
1608
- class << self
1609
- def call(doc)
1610
- marker = Object.new
1611
- stack = [doc]
1612
-
1613
- while stack.any?
1614
- doc = stack.pop
1615
-
1616
- if doc == marker
1617
- stack.pop
1618
- next
1619
- end
1620
-
1621
- stack += [doc, marker]
1622
-
1623
- case doc
1624
- when PrettyPrint::Align, PrettyPrint::Indent, PrettyPrint::Group
1625
- doc.contents.map! { |child| remove_breaks(child) }
1626
- stack += doc.contents.reverse
1627
- when PrettyPrint::IfBreak
1628
- doc.flat_contents.map! { |child| remove_breaks(child) }
1629
- stack += doc.flat_contents.reverse
1630
- end
1631
- end
1632
- end
1633
-
1634
- private
1635
-
1636
- def remove_breaks(doc)
1637
- case doc
1638
- when PrettyPrint::Breakable
1639
- text = PrettyPrint::Text.new
1640
- text.add(object: doc.force? ? "; " : doc.separator, width: doc.width)
1641
- text
1642
- when PrettyPrint::IfBreak
1643
- PrettyPrint::Align.new(indent: 0, contents: doc.flat_contents)
1644
- else
1645
- doc
1646
- end
1647
- end
1648
- end
1649
- end
1650
-
1651
1669
  # BlockVar represents the parameters being declared for a block. Effectively
1652
1670
  # this node is everything contained within the pipes. This includes all of the
1653
1671
  # various parameter types, as well as block-local variable declarations.
@@ -1682,14 +1700,13 @@ module SyntaxTree
1682
1700
 
1683
1701
  alias deconstruct child_nodes
1684
1702
 
1685
- def deconstruct_keys(keys)
1703
+ def deconstruct_keys(_keys)
1686
1704
  { params: params, locals: locals, location: location, comments: comments }
1687
1705
  end
1688
1706
 
1689
1707
  def format(q)
1690
1708
  q.group(0, "|", "|") do
1691
- doc = q.format(params)
1692
- RemoveBreaks.call(doc)
1709
+ q.remove_breaks(q.format(params))
1693
1710
 
1694
1711
  if locals.any?
1695
1712
  q.text("; ")
@@ -1726,7 +1743,7 @@ module SyntaxTree
1726
1743
 
1727
1744
  alias deconstruct child_nodes
1728
1745
 
1729
- def deconstruct_keys(keys)
1746
+ def deconstruct_keys(_keys)
1730
1747
  { name: name, location: location, comments: comments }
1731
1748
  end
1732
1749
 
@@ -1822,7 +1839,7 @@ module SyntaxTree
1822
1839
 
1823
1840
  alias deconstruct child_nodes
1824
1841
 
1825
- def deconstruct_keys(keys)
1842
+ def deconstruct_keys(_keys)
1826
1843
  {
1827
1844
  statements: statements,
1828
1845
  rescue_clause: rescue_clause,
@@ -1868,6 +1885,7 @@ module SyntaxTree
1868
1885
 
1869
1886
  # Responsible for formatting either a BraceBlock or a DoBlock.
1870
1887
  class BlockFormatter
1888
+ # Formats the opening brace or keyword of a block.
1871
1889
  class BlockOpenFormatter
1872
1890
  # [String] the actual output that should be printed
1873
1891
  attr_reader :text
@@ -1933,9 +1951,9 @@ module SyntaxTree
1933
1951
  end
1934
1952
 
1935
1953
  q.group do
1936
- q.if_break { format_break(q, break_opening, break_closing) }.if_flat do
1937
- format_flat(q, flat_opening, flat_closing)
1938
- end
1954
+ q
1955
+ .if_break { format_break(q, break_opening, break_closing) }
1956
+ .if_flat { format_flat(q, flat_opening, flat_closing) }
1939
1957
  end
1940
1958
  end
1941
1959
 
@@ -2060,7 +2078,7 @@ module SyntaxTree
2060
2078
 
2061
2079
  alias deconstruct child_nodes
2062
2080
 
2063
- def deconstruct_keys(keys)
2081
+ def deconstruct_keys(_keys)
2064
2082
  {
2065
2083
  lbrace: lbrace,
2066
2084
  block_var: block_var,
@@ -2099,7 +2117,11 @@ module SyntaxTree
2099
2117
  #
2100
2118
  # break
2101
2119
  #
2102
- in [Paren[contents: { body: [ArrayLiteral[contents: { parts: [_, _, *] }] => array] }]]
2120
+ in [Paren[
2121
+ contents: {
2122
+ body: [ArrayLiteral[contents: { parts: [_, _, *] }] => array]
2123
+ }
2124
+ ]]
2103
2125
  # Here we have a single argument that is a set of parentheses wrapping
2104
2126
  # an array literal that has at least 2 elements. We're going to print
2105
2127
  # the contents of the array directly. This would be like if we had:
@@ -2255,7 +2277,7 @@ module SyntaxTree
2255
2277
 
2256
2278
  alias deconstruct child_nodes
2257
2279
 
2258
- def deconstruct_keys(keys)
2280
+ def deconstruct_keys(_keys)
2259
2281
  { arguments: arguments, location: location, comments: comments }
2260
2282
  end
2261
2283
 
@@ -2287,6 +2309,20 @@ module SyntaxTree
2287
2309
  end
2288
2310
  end
2289
2311
 
2312
+ # This is probably the most complicated formatter in this file. It's
2313
+ # responsible for formatting chains of method calls, with or without arguments
2314
+ # or blocks. In general, we want to go from something like
2315
+ #
2316
+ # foo.bar.baz
2317
+ #
2318
+ # to
2319
+ #
2320
+ # foo
2321
+ # .bar
2322
+ # .baz
2323
+ #
2324
+ # Of course there are a lot of caveats to that, including trailing operators
2325
+ # when necessary, where comments are places, how blocks are aligned, etc.
2290
2326
  class CallChainFormatter
2291
2327
  # [Call | MethodAddBlock] the top of the call chain
2292
2328
  attr_reader :node
@@ -2301,7 +2337,7 @@ module SyntaxTree
2301
2337
 
2302
2338
  # First, walk down the chain until we get to the point where we're not
2303
2339
  # longer at a chainable node.
2304
- while true
2340
+ loop do
2305
2341
  case children.last
2306
2342
  in Call[receiver: Call]
2307
2343
  children << children.last.receiver
@@ -2321,7 +2357,7 @@ module SyntaxTree
2321
2357
  # block. For more details, see
2322
2358
  # https://github.com/prettier/plugin-ruby/issues/863.
2323
2359
  parents = q.parents.take(4)
2324
- if parent = parents[2]
2360
+ if (parent = parents[2])
2325
2361
  # If we're at a do_block, then we want to go one more level up. This is
2326
2362
  # because do blocks have BodyStmt nodes instead of just Statements
2327
2363
  # nodes.
@@ -2335,7 +2371,11 @@ module SyntaxTree
2335
2371
  end
2336
2372
 
2337
2373
  if children.length >= threshold
2338
- q.group { q.if_break { format_chain(q, children) }.if_flat { node.format_contents(q) } }
2374
+ q.group do
2375
+ q
2376
+ .if_break { format_chain(q, children) }
2377
+ .if_flat { node.format_contents(q) }
2378
+ end
2339
2379
  else
2340
2380
  node.format_contents(q)
2341
2381
  end
@@ -2346,9 +2386,9 @@ module SyntaxTree
2346
2386
  # chain of calls without arguments except for the last one. This is common
2347
2387
  # enough in Ruby source code that it's worth the extra complexity here.
2348
2388
  empty_except_last =
2349
- children.drop(1).all? do |child|
2350
- child.is_a?(Call) && child.arguments.nil?
2351
- end
2389
+ children
2390
+ .drop(1)
2391
+ .all? { |child| child.is_a?(Call) && child.arguments.nil? }
2352
2392
 
2353
2393
  # Here, we're going to add all of the children onto the stack of the
2354
2394
  # formatter so it's as if we had descending normally into them. This is
@@ -2368,9 +2408,12 @@ module SyntaxTree
2368
2408
  # and a trailing operator.
2369
2409
  skip_operator = false
2370
2410
 
2371
- while child = children.pop
2411
+ while (child = children.pop)
2372
2412
  case child
2373
- in Call[receiver: Call[message: { value: "where" }], message: { value: "not" }]
2413
+ in Call[
2414
+ receiver: Call[message: { value: "where" }],
2415
+ message: { value: "not" }
2416
+ ]
2374
2417
  # This is very specialized behavior wherein we group
2375
2418
  # .where.not calls together because it looks better. For more
2376
2419
  # information, see
@@ -2432,16 +2475,25 @@ module SyntaxTree
2432
2475
  # want to indent the first call. So we'll pop off the first children and
2433
2476
  # format it separately here.
2434
2477
  def attach_directly?(node)
2435
- [ArrayLiteral, HashLiteral, Heredoc, If, Unless, XStringLiteral]
2436
- .include?(node.receiver.class)
2478
+ [ArrayLiteral, HashLiteral, Heredoc, If, Unless, XStringLiteral].include?(
2479
+ node.receiver.class
2480
+ )
2437
2481
  end
2438
2482
 
2439
- def format_child(q, child, skip_comments: false, skip_operator: false, skip_attached: false)
2483
+ def format_child(
2484
+ q,
2485
+ child,
2486
+ skip_comments: false,
2487
+ skip_operator: false,
2488
+ skip_attached: false
2489
+ )
2440
2490
  # First, format the actual contents of the child.
2441
2491
  case child
2442
2492
  in Call
2443
2493
  q.group do
2444
- q.format(CallOperatorFormatter.new(child.operator)) unless skip_operator
2494
+ unless skip_operator
2495
+ q.format(CallOperatorFormatter.new(child.operator))
2496
+ end
2445
2497
  q.format(child.message) if child.message != :call
2446
2498
  child.format_arguments(q) unless skip_attached
2447
2499
  end
@@ -2513,7 +2565,7 @@ module SyntaxTree
2513
2565
 
2514
2566
  alias deconstruct child_nodes
2515
2567
 
2516
- def deconstruct_keys(keys)
2568
+ def deconstruct_keys(_keys)
2517
2569
  {
2518
2570
  receiver: receiver,
2519
2571
  operator: operator,
@@ -2528,8 +2580,13 @@ module SyntaxTree
2528
2580
  # If we're at the top of a call chain, then we're going to do some
2529
2581
  # specialized printing in case we can print it nicely. We _only_ do this
2530
2582
  # at the top of the chain to avoid weird recursion issues.
2531
- if !CallChainFormatter.chained?(q.parent) && CallChainFormatter.chained?(receiver)
2532
- q.group { q.if_break { CallChainFormatter.new(self).format(q) }.if_flat { format_contents(q) } }
2583
+ if !CallChainFormatter.chained?(q.parent) &&
2584
+ CallChainFormatter.chained?(receiver)
2585
+ q.group do
2586
+ q
2587
+ .if_break { CallChainFormatter.new(self).format(q) }
2588
+ .if_flat { format_contents(q) }
2589
+ end
2533
2590
  else
2534
2591
  format_contents(q)
2535
2592
  end
@@ -2618,7 +2675,7 @@ module SyntaxTree
2618
2675
 
2619
2676
  alias deconstruct child_nodes
2620
2677
 
2621
- def deconstruct_keys(keys)
2678
+ def deconstruct_keys(_keys)
2622
2679
  {
2623
2680
  keyword: keyword,
2624
2681
  value: value,
@@ -2683,7 +2740,7 @@ module SyntaxTree
2683
2740
 
2684
2741
  alias deconstruct child_nodes
2685
2742
 
2686
- def deconstruct_keys(keys)
2743
+ def deconstruct_keys(_keys)
2687
2744
  {
2688
2745
  value: value,
2689
2746
  operator: operator,
@@ -2772,7 +2829,7 @@ module SyntaxTree
2772
2829
 
2773
2830
  alias deconstruct child_nodes
2774
2831
 
2775
- def deconstruct_keys(keys)
2832
+ def deconstruct_keys(_keys)
2776
2833
  {
2777
2834
  constant: constant,
2778
2835
  superclass: superclass,
@@ -2837,7 +2894,7 @@ module SyntaxTree
2837
2894
 
2838
2895
  alias deconstruct child_nodes
2839
2896
 
2840
- def deconstruct_keys(keys)
2897
+ def deconstruct_keys(_keys)
2841
2898
  { value: value, location: location }
2842
2899
  end
2843
2900
  end
@@ -2875,7 +2932,7 @@ module SyntaxTree
2875
2932
 
2876
2933
  alias deconstruct child_nodes
2877
2934
 
2878
- def deconstruct_keys(keys)
2935
+ def deconstruct_keys(_keys)
2879
2936
  {
2880
2937
  message: message,
2881
2938
  arguments: arguments,
@@ -2957,7 +3014,7 @@ module SyntaxTree
2957
3014
 
2958
3015
  alias deconstruct child_nodes
2959
3016
 
2960
- def deconstruct_keys(keys)
3017
+ def deconstruct_keys(_keys)
2961
3018
  {
2962
3019
  receiver: receiver,
2963
3020
  operator: operator,
@@ -2992,31 +3049,6 @@ module SyntaxTree
2992
3049
 
2993
3050
  private
2994
3051
 
2995
- # This is a somewhat naive method that is attempting to sum up the width of
2996
- # the doc nodes that make up the given doc node. This is used to align
2997
- # content.
2998
- def doc_width(parent)
2999
- queue = [parent]
3000
- width = 0
3001
-
3002
- until queue.empty?
3003
- doc = queue.shift
3004
-
3005
- case doc
3006
- when PrettyPrint::Text
3007
- width += doc.width
3008
- when PrettyPrint::Indent, PrettyPrint::Align, PrettyPrint::Group
3009
- queue = doc.contents + queue
3010
- when PrettyPrint::IfBreak
3011
- queue = doc.break_contents + queue
3012
- when PrettyPrint::Breakable
3013
- width = 0
3014
- end
3015
- end
3016
-
3017
- width
3018
- end
3019
-
3020
3052
  def argument_alignment(q, doc)
3021
3053
  # Very special handling case for rspec matchers. In general with rspec
3022
3054
  # matchers you expect to see something like:
@@ -3034,7 +3066,7 @@ module SyntaxTree
3034
3066
  if %w[to not_to to_not].include?(message.value)
3035
3067
  0
3036
3068
  else
3037
- width = doc_width(doc) + 1
3069
+ width = q.last_position(doc) + 1
3038
3070
  width > (q.maxwidth / 2) ? 0 : width
3039
3071
  end
3040
3072
  end
@@ -3079,7 +3111,7 @@ module SyntaxTree
3079
3111
  end
3080
3112
 
3081
3113
  def ignore?
3082
- value[1..-1].strip == "stree-ignore"
3114
+ value[1..].strip == "stree-ignore"
3083
3115
  end
3084
3116
 
3085
3117
  def comments
@@ -3096,7 +3128,7 @@ module SyntaxTree
3096
3128
 
3097
3129
  alias deconstruct child_nodes
3098
3130
 
3099
- def deconstruct_keys(keys)
3131
+ def deconstruct_keys(_keys)
3100
3132
  { value: value, inline: inline, location: location }
3101
3133
  end
3102
3134
 
@@ -3142,7 +3174,7 @@ module SyntaxTree
3142
3174
 
3143
3175
  alias deconstruct child_nodes
3144
3176
 
3145
- def deconstruct_keys(keys)
3177
+ def deconstruct_keys(_keys)
3146
3178
  { value: value, location: location, comments: comments }
3147
3179
  end
3148
3180
 
@@ -3184,7 +3216,7 @@ module SyntaxTree
3184
3216
 
3185
3217
  alias deconstruct child_nodes
3186
3218
 
3187
- def deconstruct_keys(keys)
3219
+ def deconstruct_keys(_keys)
3188
3220
  {
3189
3221
  parent: parent,
3190
3222
  constant: constant,
@@ -3231,7 +3263,7 @@ module SyntaxTree
3231
3263
 
3232
3264
  alias deconstruct child_nodes
3233
3265
 
3234
- def deconstruct_keys(keys)
3266
+ def deconstruct_keys(_keys)
3235
3267
  {
3236
3268
  parent: parent,
3237
3269
  constant: constant,
@@ -3276,7 +3308,7 @@ module SyntaxTree
3276
3308
 
3277
3309
  alias deconstruct child_nodes
3278
3310
 
3279
- def deconstruct_keys(keys)
3311
+ def deconstruct_keys(_keys)
3280
3312
  { constant: constant, location: location, comments: comments }
3281
3313
  end
3282
3314
 
@@ -3312,7 +3344,7 @@ module SyntaxTree
3312
3344
 
3313
3345
  alias deconstruct child_nodes
3314
3346
 
3315
- def deconstruct_keys(keys)
3347
+ def deconstruct_keys(_keys)
3316
3348
  { value: value, location: location, comments: comments }
3317
3349
  end
3318
3350
 
@@ -3356,7 +3388,7 @@ module SyntaxTree
3356
3388
 
3357
3389
  alias deconstruct child_nodes
3358
3390
 
3359
- def deconstruct_keys(keys)
3391
+ def deconstruct_keys(_keys)
3360
3392
  {
3361
3393
  name: name,
3362
3394
  params: params,
@@ -3441,7 +3473,7 @@ module SyntaxTree
3441
3473
 
3442
3474
  alias deconstruct child_nodes
3443
3475
 
3444
- def deconstruct_keys(keys)
3476
+ def deconstruct_keys(_keys)
3445
3477
  {
3446
3478
  target: target,
3447
3479
  operator: operator,
@@ -3509,7 +3541,7 @@ module SyntaxTree
3509
3541
 
3510
3542
  alias deconstruct child_nodes
3511
3543
 
3512
- def deconstruct_keys(keys)
3544
+ def deconstruct_keys(_keys)
3513
3545
  { value: value, location: location, comments: comments }
3514
3546
  end
3515
3547
 
@@ -3575,7 +3607,7 @@ module SyntaxTree
3575
3607
 
3576
3608
  alias deconstruct child_nodes
3577
3609
 
3578
- def deconstruct_keys(keys)
3610
+ def deconstruct_keys(_keys)
3579
3611
  {
3580
3612
  target: target,
3581
3613
  operator: operator,
@@ -3650,7 +3682,7 @@ module SyntaxTree
3650
3682
 
3651
3683
  alias deconstruct child_nodes
3652
3684
 
3653
- def deconstruct_keys(keys)
3685
+ def deconstruct_keys(_keys)
3654
3686
  {
3655
3687
  keyword: keyword,
3656
3688
  block_var: block_var,
@@ -3730,7 +3762,7 @@ module SyntaxTree
3730
3762
 
3731
3763
  alias deconstruct child_nodes
3732
3764
 
3733
- def deconstruct_keys(keys)
3765
+ def deconstruct_keys(_keys)
3734
3766
  { left: left, right: right, location: location, comments: comments }
3735
3767
  end
3736
3768
 
@@ -3778,7 +3810,7 @@ module SyntaxTree
3778
3810
 
3779
3811
  alias deconstruct child_nodes
3780
3812
 
3781
- def deconstruct_keys(keys)
3813
+ def deconstruct_keys(_keys)
3782
3814
  { left: left, right: right, location: location, comments: comments }
3783
3815
  end
3784
3816
 
@@ -3800,7 +3832,7 @@ module SyntaxTree
3800
3832
  # quotes, then single quotes would deactivate it.)
3801
3833
  def self.locked?(node)
3802
3834
  node.parts.any? do |part|
3803
- part.is_a?(TStringContent) && part.value.match?(/#[@${]|[\\]/)
3835
+ !part.is_a?(TStringContent) || part.value.match?(/\\|#[@${]/)
3804
3836
  end
3805
3837
  end
3806
3838
 
@@ -3865,7 +3897,7 @@ module SyntaxTree
3865
3897
 
3866
3898
  alias deconstruct child_nodes
3867
3899
 
3868
- def deconstruct_keys(keys)
3900
+ def deconstruct_keys(_keys)
3869
3901
  { parts: parts, quote: quote, location: location, comments: comments }
3870
3902
  end
3871
3903
 
@@ -3906,9 +3938,14 @@ module SyntaxTree
3906
3938
  matching = Quotes.matching(quote[2])
3907
3939
  pattern = /[\n#{Regexp.escape(matching)}'"]/
3908
3940
 
3909
- if parts.any? { |part|
3910
- part.is_a?(TStringContent) && part.value.match?(pattern)
3911
- }
3941
+ # This check is to ensure we don't find a matching quote inside of the
3942
+ # symbol that would be confusing.
3943
+ matched =
3944
+ parts.any? do |part|
3945
+ part.is_a?(TStringContent) && part.value.match?(pattern)
3946
+ end
3947
+
3948
+ if matched
3912
3949
  [quote, matching]
3913
3950
  elsif Quotes.locked?(self)
3914
3951
  ["#{":" unless hash_key}'", "'"]
@@ -3917,7 +3954,7 @@ module SyntaxTree
3917
3954
  end
3918
3955
  elsif Quotes.locked?(self)
3919
3956
  if quote.start_with?(":")
3920
- [hash_key ? quote[1..-1] : quote, quote[1..-1]]
3957
+ [hash_key ? quote[1..] : quote, quote[1..]]
3921
3958
  else
3922
3959
  [hash_key ? quote : ":#{quote}", quote]
3923
3960
  end
@@ -3960,7 +3997,7 @@ module SyntaxTree
3960
3997
 
3961
3998
  alias deconstruct child_nodes
3962
3999
 
3963
- def deconstruct_keys(keys)
4000
+ def deconstruct_keys(_keys)
3964
4001
  {
3965
4002
  keyword: keyword,
3966
4003
  statements: statements,
@@ -4026,7 +4063,7 @@ module SyntaxTree
4026
4063
 
4027
4064
  alias deconstruct child_nodes
4028
4065
 
4029
- def deconstruct_keys(keys)
4066
+ def deconstruct_keys(_keys)
4030
4067
  {
4031
4068
  predicate: predicate,
4032
4069
  statements: statements,
@@ -4098,7 +4135,7 @@ module SyntaxTree
4098
4135
 
4099
4136
  alias deconstruct child_nodes
4100
4137
 
4101
- def deconstruct_keys(keys)
4138
+ def deconstruct_keys(_keys)
4102
4139
  { value: value, location: location }
4103
4140
  end
4104
4141
 
@@ -4133,7 +4170,7 @@ module SyntaxTree
4133
4170
 
4134
4171
  alias deconstruct child_nodes
4135
4172
 
4136
- def deconstruct_keys(keys)
4173
+ def deconstruct_keys(_keys)
4137
4174
  { value: value, location: location }
4138
4175
  end
4139
4176
  end
@@ -4163,7 +4200,7 @@ module SyntaxTree
4163
4200
 
4164
4201
  alias deconstruct child_nodes
4165
4202
 
4166
- def deconstruct_keys(keys)
4203
+ def deconstruct_keys(_keys)
4167
4204
  { value: value, location: location }
4168
4205
  end
4169
4206
  end
@@ -4195,7 +4232,7 @@ module SyntaxTree
4195
4232
 
4196
4233
  alias deconstruct child_nodes
4197
4234
 
4198
- def deconstruct_keys(keys)
4235
+ def deconstruct_keys(_keys)
4199
4236
  { value: value, location: location }
4200
4237
  end
4201
4238
  end
@@ -4234,7 +4271,7 @@ module SyntaxTree
4234
4271
 
4235
4272
  alias deconstruct child_nodes
4236
4273
 
4237
- def deconstruct_keys(keys)
4274
+ def deconstruct_keys(_keys)
4238
4275
  {
4239
4276
  keyword: keyword,
4240
4277
  statements: statements,
@@ -4288,7 +4325,7 @@ module SyntaxTree
4288
4325
 
4289
4326
  alias deconstruct child_nodes
4290
4327
 
4291
- def deconstruct_keys(keys)
4328
+ def deconstruct_keys(_keys)
4292
4329
  { value: value, location: location, comments: comments }
4293
4330
  end
4294
4331
 
@@ -4331,7 +4368,7 @@ module SyntaxTree
4331
4368
 
4332
4369
  alias deconstruct child_nodes
4333
4370
 
4334
- def deconstruct_keys(keys)
4371
+ def deconstruct_keys(_keys)
4335
4372
  {
4336
4373
  value: value,
4337
4374
  arguments: arguments,
@@ -4343,7 +4380,8 @@ module SyntaxTree
4343
4380
  def format(q)
4344
4381
  q.format(value)
4345
4382
 
4346
- if arguments.is_a?(ArgParen) && arguments.arguments.nil? && !value.is_a?(Const)
4383
+ if arguments.is_a?(ArgParen) && arguments.arguments.nil? &&
4384
+ !value.is_a?(Const)
4347
4385
  # If you're using an explicit set of parentheses on something that looks
4348
4386
  # like a constant, then we need to match that in order to maintain valid
4349
4387
  # Ruby. For example, you could do something like Foo(), on which we
@@ -4390,7 +4428,7 @@ module SyntaxTree
4390
4428
 
4391
4429
  alias deconstruct child_nodes
4392
4430
 
4393
- def deconstruct_keys(keys)
4431
+ def deconstruct_keys(_keys)
4394
4432
  {
4395
4433
  parent: parent,
4396
4434
  operator: operator,
@@ -4436,7 +4474,7 @@ module SyntaxTree
4436
4474
 
4437
4475
  alias deconstruct child_nodes
4438
4476
 
4439
- def deconstruct_keys(keys)
4477
+ def deconstruct_keys(_keys)
4440
4478
  { value: value, location: location, comments: comments }
4441
4479
  end
4442
4480
 
@@ -4488,7 +4526,7 @@ module SyntaxTree
4488
4526
 
4489
4527
  alias deconstruct child_nodes
4490
4528
 
4491
- def deconstruct_keys(keys)
4529
+ def deconstruct_keys(_keys)
4492
4530
  {
4493
4531
  constant: constant,
4494
4532
  left: left,
@@ -4552,7 +4590,7 @@ module SyntaxTree
4552
4590
 
4553
4591
  alias deconstruct child_nodes
4554
4592
 
4555
- def deconstruct_keys(keys)
4593
+ def deconstruct_keys(_keys)
4556
4594
  {
4557
4595
  index: index,
4558
4596
  collection: collection,
@@ -4609,7 +4647,7 @@ module SyntaxTree
4609
4647
 
4610
4648
  alias deconstruct child_nodes
4611
4649
 
4612
- def deconstruct_keys(keys)
4650
+ def deconstruct_keys(_keys)
4613
4651
  { value: value, location: location, comments: comments }
4614
4652
  end
4615
4653
 
@@ -4623,6 +4661,9 @@ module SyntaxTree
4623
4661
  # { key => value }
4624
4662
  #
4625
4663
  class HashLiteral < Node
4664
+ # This is a special formatter used if the hash literal contains no values
4665
+ # but _does_ contain comments. In this case we do some special formatting to
4666
+ # make sure the comments gets indented properly.
4626
4667
  class EmptyWithCommentsFormatter
4627
4668
  # [LBrace] the opening brace
4628
4669
  attr_reader :lbrace
@@ -4672,7 +4713,7 @@ module SyntaxTree
4672
4713
 
4673
4714
  alias deconstruct child_nodes
4674
4715
 
4675
- def deconstruct_keys(keys)
4716
+ def deconstruct_keys(_keys)
4676
4717
  { lbrace: lbrace, assocs: assocs, location: location, comments: comments }
4677
4718
  end
4678
4719
 
@@ -4741,7 +4782,14 @@ module SyntaxTree
4741
4782
  # [Array[ Comment | EmbDoc ]] the comments attached to this node
4742
4783
  attr_reader :comments
4743
4784
 
4744
- def initialize(beginning:, ending: nil, dedent: 0, parts: [], location:, comments: [])
4785
+ def initialize(
4786
+ beginning:,
4787
+ ending: nil,
4788
+ dedent: 0,
4789
+ parts: [],
4790
+ location:,
4791
+ comments: []
4792
+ )
4745
4793
  @beginning = beginning
4746
4794
  @ending = ending
4747
4795
  @dedent = dedent
@@ -4760,7 +4808,7 @@ module SyntaxTree
4760
4808
 
4761
4809
  alias deconstruct child_nodes
4762
4810
 
4763
- def deconstruct_keys(keys)
4811
+ def deconstruct_keys(_keys)
4764
4812
  {
4765
4813
  beginning: beginning,
4766
4814
  location: location,
@@ -4771,13 +4819,9 @@ module SyntaxTree
4771
4819
  end
4772
4820
 
4773
4821
  def format(q)
4774
- # This is a very specific behavior that should probably be included in the
4775
- # prettyprint module. It's when you want to force a newline, but don't
4776
- # want to force the break parent.
4777
- breakable = -> do
4778
- q.target <<
4779
- PrettyPrint::Breakable.new(" ", 1, indent: false, force: true)
4780
- end
4822
+ # This is a very specific behavior where you want to force a newline, but
4823
+ # don't want to force the break parent.
4824
+ breakable = -> { q.breakable(indent: false, force: :skip_break_parent) }
4781
4825
 
4782
4826
  q.group do
4783
4827
  q.format(beginning)
@@ -4832,7 +4876,7 @@ module SyntaxTree
4832
4876
 
4833
4877
  alias deconstruct child_nodes
4834
4878
 
4835
- def deconstruct_keys(keys)
4879
+ def deconstruct_keys(_keys)
4836
4880
  { value: value, location: location, comments: comments }
4837
4881
  end
4838
4882
 
@@ -4849,6 +4893,7 @@ module SyntaxTree
4849
4893
  # end
4850
4894
  #
4851
4895
  class HshPtn < Node
4896
+ # Formats a key-value pair in a hash pattern. The value is optional.
4852
4897
  class KeywordFormatter
4853
4898
  # [Label] the keyword being used
4854
4899
  attr_reader :key
@@ -4875,6 +4920,7 @@ module SyntaxTree
4875
4920
  end
4876
4921
  end
4877
4922
 
4923
+ # Formats the optional double-splat from the pattern.
4878
4924
  class KeywordRestFormatter
4879
4925
  # [VarField] the parameter that matches the remaining keywords
4880
4926
  attr_reader :keyword_rest
@@ -4924,7 +4970,7 @@ module SyntaxTree
4924
4970
 
4925
4971
  alias deconstruct child_nodes
4926
4972
 
4927
- def deconstruct_keys(keys)
4973
+ def deconstruct_keys(_keys)
4928
4974
  {
4929
4975
  constant: constant,
4930
4976
  keywords: keywords,
@@ -4938,36 +4984,64 @@ module SyntaxTree
4938
4984
  parts = keywords.map { |(key, value)| KeywordFormatter.new(key, value) }
4939
4985
  parts << KeywordRestFormatter.new(keyword_rest) if keyword_rest
4940
4986
 
4987
+ nested = PATTERNS.include?(q.parent.class)
4941
4988
  contents = -> do
4942
4989
  q.group { q.seplist(parts) { |part| q.format(part, stackable: false) } }
4943
4990
 
4944
4991
  # If there isn't a constant, and there's a blank keyword_rest, then we
4945
4992
  # have an plain ** that needs to have a `then` after it in order to
4946
4993
  # parse correctly on the next parse.
4947
- q.text(" then") if !constant && keyword_rest && keyword_rest.value.nil?
4994
+ if !constant && keyword_rest && keyword_rest.value.nil? && !nested
4995
+ q.text(" then")
4996
+ end
4948
4997
  end
4949
4998
 
4999
+ # If there is a constant, we're going to format to have the constant name
5000
+ # first and then use brackets.
4950
5001
  if constant
4951
- q.format(constant)
4952
- q.group(0, "[", "]", &contents)
5002
+ q.group do
5003
+ q.format(constant)
5004
+ q.text("[")
5005
+ q.indent do
5006
+ q.breakable("")
5007
+ contents.call
5008
+ end
5009
+ q.breakable("")
5010
+ q.text("]")
5011
+ end
4953
5012
  return
4954
5013
  end
4955
5014
 
5015
+ # If there's nothing at all, then we're going to use empty braces.
4956
5016
  if parts.empty?
4957
5017
  q.text("{}")
4958
- elsif PATTERNS.include?(q.parent.class)
4959
- q.text("{ ")
4960
- contents.call
4961
- q.text(" }")
4962
- else
5018
+ return
5019
+ end
5020
+
5021
+ # If there's only one pair, then we'll just print the contents provided
5022
+ # we're not inside another pattern.
5023
+ if !nested && parts.size == 1
4963
5024
  contents.call
5025
+ return
5026
+ end
5027
+
5028
+ # Otherwise, we're going to always use braces to make it clear it's a hash
5029
+ # pattern.
5030
+ q.group do
5031
+ q.text("{")
5032
+ q.indent do
5033
+ q.breakable
5034
+ contents.call
5035
+ end
5036
+ q.breakable
5037
+ q.text("}")
4964
5038
  end
4965
5039
  end
4966
5040
  end
4967
5041
 
4968
5042
  # The list of nodes that represent patterns inside of pattern matching so that
4969
5043
  # when a pattern is being printed it knows if it's nested.
4970
- PATTERNS = [AryPtn, Binary, FndPtn, HshPtn, RAssign]
5044
+ PATTERNS = [AryPtn, Binary, FndPtn, HshPtn, RAssign].freeze
4971
5045
 
4972
5046
  # Ident represents an identifier anywhere in code. It can represent a very
4973
5047
  # large number of things, depending on where it is in the syntax tree.
@@ -4997,7 +5071,7 @@ module SyntaxTree
4997
5071
 
4998
5072
  alias deconstruct child_nodes
4999
5073
 
5000
- def deconstruct_keys(keys)
5074
+ def deconstruct_keys(_keys)
5001
5075
  { value: value, location: location, comments: comments }
5002
5076
  end
5003
5077
 
@@ -5014,7 +5088,7 @@ module SyntaxTree
5014
5088
  def self.call(parent)
5015
5089
  queue = [parent]
5016
5090
 
5017
- while node = queue.shift
5091
+ while (node = queue.shift)
5018
5092
  return true if [Assign, MAssign, OpAssign].include?(node.class)
5019
5093
  queue += node.child_nodes
5020
5094
  end
@@ -5041,9 +5115,14 @@ module SyntaxTree
5041
5115
  else
5042
5116
  # Otherwise, we're going to check the conditional for certain cases.
5043
5117
  case node
5044
- in { predicate: Assign | Command | CommandCall | MAssign | OpAssign }
5118
+ in predicate: Assign | Command | CommandCall | MAssign | OpAssign
5045
5119
  false
5046
- in { statements: { body: [truthy] }, consequent: Else[statements: { body: [falsy] }] }
5120
+ in predicate: Not[parentheses: false]
5121
+ false
5122
+ in {
5123
+ statements: { body: [truthy] },
5124
+ consequent: Else[statements: { body: [falsy] }]
5125
+ }
5047
5126
  ternaryable?(truthy) && ternaryable?(falsy)
5048
5127
  else
5049
5128
  false
@@ -5054,8 +5133,8 @@ module SyntaxTree
5054
5133
  private
5055
5134
 
5056
5135
  # Certain expressions cannot be reduced to a ternary without adding
5057
- # parentheses around them. In this case we say they cannot be ternaried and
5058
- # default instead to breaking them into multiple lines.
5136
+ # parentheses around them. In this case we say they cannot be ternaried
5137
+ # and default instead to breaking them into multiple lines.
5059
5138
  def ternaryable?(statement)
5060
5139
  # This is a list of nodes that should not be allowed to be a part of a
5061
5140
  # ternary clause.
@@ -5113,13 +5192,15 @@ module SyntaxTree
5113
5192
  q.group { format_break(q, force: true) }
5114
5193
  else
5115
5194
  q.group do
5116
- q.if_break { format_break(q, force: false) }.if_flat do
5117
- Parentheses.flat(q) do
5118
- q.format(node.statements)
5119
- q.text(" #{keyword} ")
5120
- q.format(node.predicate)
5195
+ q
5196
+ .if_break { format_break(q, force: false) }
5197
+ .if_flat do
5198
+ Parentheses.flat(q) do
5199
+ q.format(node.statements)
5200
+ q.text(" #{keyword} ")
5201
+ q.format(node.predicate)
5202
+ end
5121
5203
  end
5122
- end
5123
5204
  end
5124
5205
  end
5125
5206
  end
@@ -5148,51 +5229,52 @@ module SyntaxTree
5148
5229
 
5149
5230
  def format_ternary(q)
5150
5231
  q.group do
5151
- q.if_break do
5152
- q.text("#{keyword} ")
5153
- q.nest(keyword.length + 1) { q.format(node.predicate) }
5232
+ q
5233
+ .if_break do
5234
+ q.text("#{keyword} ")
5235
+ q.nest(keyword.length + 1) { q.format(node.predicate) }
5154
5236
 
5155
- q.indent do
5156
- q.breakable
5157
- q.format(node.statements)
5158
- end
5159
-
5160
- q.breakable
5161
- q.group do
5162
- q.format(node.consequent.keyword)
5163
5237
  q.indent do
5164
- # This is a very special case of breakable where we want to force
5165
- # it into the output but we _don't_ want to explicitly break the
5166
- # parent. If a break-parent shows up in the tree, then it's going
5167
- # to force it all the way up to the tree, which is going to negate
5168
- # the ternary. Maybe this should be an option in prettyprint? As
5169
- # in force: :no_break_parent or something.
5170
- q.target << PrettyPrint::Breakable.new(" ", 1, force: true)
5171
- q.format(node.consequent.statements)
5238
+ q.breakable
5239
+ q.format(node.statements)
5172
5240
  end
5173
- end
5174
5241
 
5175
- q.breakable
5176
- q.text("end")
5177
- end.if_flat do
5178
- Parentheses.flat(q) do
5179
- q.format(node.predicate)
5180
- q.text(" ? ")
5242
+ q.breakable
5243
+ q.group do
5244
+ q.format(node.consequent.keyword)
5245
+ q.indent do
5246
+ # This is a very special case of breakable where we want to
5247
+ # force it into the output but we _don't_ want to explicitly
5248
+ # break the parent. If a break-parent shows up in the tree, then
5249
+ # it's going to force it all the way up to the tree, which is
5250
+ # going to negate the ternary.
5251
+ q.breakable(force: :skip_break_parent)
5252
+ q.format(node.consequent.statements)
5253
+ end
5254
+ end
5255
+
5256
+ q.breakable
5257
+ q.text("end")
5258
+ end
5259
+ .if_flat do
5260
+ Parentheses.flat(q) do
5261
+ q.format(node.predicate)
5262
+ q.text(" ? ")
5181
5263
 
5182
- statements = [node.statements, node.consequent.statements]
5183
- statements.reverse! if keyword == "unless"
5264
+ statements = [node.statements, node.consequent.statements]
5265
+ statements.reverse! if keyword == "unless"
5184
5266
 
5185
- q.format(statements[0])
5186
- q.text(" : ")
5187
- q.format(statements[1])
5267
+ q.format(statements[0])
5268
+ q.text(" : ")
5269
+ q.format(statements[1])
5270
+ end
5188
5271
  end
5189
- end
5190
5272
  end
5191
5273
  end
5192
5274
 
5193
5275
  def contains_conditional?
5194
5276
  case node
5195
- in { statements: { body: [If | IfMod | IfOp | Unless | UnlessMod] } }
5277
+ in statements: { body: [If | IfMod | IfOp | Unless | UnlessMod] }
5196
5278
  true
5197
5279
  else
5198
5280
  false
@@ -5242,7 +5324,7 @@ module SyntaxTree
5242
5324
 
5243
5325
  alias deconstruct child_nodes
5244
5326
 
5245
- def deconstruct_keys(keys)
5327
+ def deconstruct_keys(_keys)
5246
5328
  {
5247
5329
  predicate: predicate,
5248
5330
  statements: statements,
@@ -5292,7 +5374,7 @@ module SyntaxTree
5292
5374
 
5293
5375
  alias deconstruct child_nodes
5294
5376
 
5295
- def deconstruct_keys(keys)
5377
+ def deconstruct_keys(_keys)
5296
5378
  {
5297
5379
  predicate: predicate,
5298
5380
  truthy: truthy,
@@ -5310,7 +5392,8 @@ module SyntaxTree
5310
5392
  Yield0, ZSuper
5311
5393
  ]
5312
5394
 
5313
- if q.parent.is_a?(Paren) || force_flat.include?(truthy.class) || force_flat.include?(falsy.class)
5395
+ if q.parent.is_a?(Paren) || force_flat.include?(truthy.class) ||
5396
+ force_flat.include?(falsy.class)
5314
5397
  q.group { format_flat(q) }
5315
5398
  return
5316
5399
  end
@@ -5430,7 +5513,7 @@ module SyntaxTree
5430
5513
 
5431
5514
  alias deconstruct child_nodes
5432
5515
 
5433
- def deconstruct_keys(keys)
5516
+ def deconstruct_keys(_keys)
5434
5517
  {
5435
5518
  statement: statement,
5436
5519
  predicate: predicate,
@@ -5471,7 +5554,7 @@ module SyntaxTree
5471
5554
 
5472
5555
  alias deconstruct child_nodes
5473
5556
 
5474
- def deconstruct_keys(keys)
5557
+ def deconstruct_keys(_keys)
5475
5558
  { value: value, location: location, comments: comments }
5476
5559
  end
5477
5560
 
@@ -5518,7 +5601,7 @@ module SyntaxTree
5518
5601
 
5519
5602
  alias deconstruct child_nodes
5520
5603
 
5521
- def deconstruct_keys(keys)
5604
+ def deconstruct_keys(_keys)
5522
5605
  {
5523
5606
  pattern: pattern,
5524
5607
  statements: statements,
@@ -5577,7 +5660,7 @@ module SyntaxTree
5577
5660
 
5578
5661
  alias deconstruct child_nodes
5579
5662
 
5580
- def deconstruct_keys(keys)
5663
+ def deconstruct_keys(_keys)
5581
5664
  { value: value, location: location, comments: comments }
5582
5665
  end
5583
5666
 
@@ -5587,7 +5670,7 @@ module SyntaxTree
5587
5670
  # the values, then we're going to insert them every 3 characters
5588
5671
  # starting from the right.
5589
5672
  index = (value.length + 2) % 3
5590
- q.text(" #{value}"[index..-1].scan(/.../).join("_").strip)
5673
+ q.text(" #{value}"[index..].scan(/.../).join("_").strip)
5591
5674
  else
5592
5675
  q.text(value)
5593
5676
  end
@@ -5621,7 +5704,7 @@ module SyntaxTree
5621
5704
 
5622
5705
  alias deconstruct child_nodes
5623
5706
 
5624
- def deconstruct_keys(keys)
5707
+ def deconstruct_keys(_keys)
5625
5708
  { value: value, location: location, comments: comments }
5626
5709
  end
5627
5710
 
@@ -5666,7 +5749,7 @@ module SyntaxTree
5666
5749
 
5667
5750
  alias deconstruct child_nodes
5668
5751
 
5669
- def deconstruct_keys(keys)
5752
+ def deconstruct_keys(_keys)
5670
5753
  { value: value, location: location, comments: comments }
5671
5754
  end
5672
5755
 
@@ -5703,7 +5786,7 @@ module SyntaxTree
5703
5786
 
5704
5787
  alias deconstruct child_nodes
5705
5788
 
5706
- def deconstruct_keys(keys)
5789
+ def deconstruct_keys(_keys)
5707
5790
  { name: name, location: location, comments: comments }
5708
5791
  end
5709
5792
 
@@ -5749,7 +5832,7 @@ module SyntaxTree
5749
5832
 
5750
5833
  alias deconstruct child_nodes
5751
5834
 
5752
- def deconstruct_keys(keys)
5835
+ def deconstruct_keys(_keys)
5753
5836
  { value: value, location: location, comments: comments }
5754
5837
  end
5755
5838
 
@@ -5784,7 +5867,7 @@ module SyntaxTree
5784
5867
 
5785
5868
  alias deconstruct child_nodes
5786
5869
 
5787
- def deconstruct_keys(keys)
5870
+ def deconstruct_keys(_keys)
5788
5871
  { value: value, location: location }
5789
5872
  end
5790
5873
  end
@@ -5820,7 +5903,7 @@ module SyntaxTree
5820
5903
 
5821
5904
  alias deconstruct child_nodes
5822
5905
 
5823
- def deconstruct_keys(keys)
5906
+ def deconstruct_keys(_keys)
5824
5907
  {
5825
5908
  params: params,
5826
5909
  statements: statements,
@@ -5842,25 +5925,27 @@ module SyntaxTree
5842
5925
  end
5843
5926
 
5844
5927
  q.text(" ")
5845
- q.if_break do
5846
- force_parens =
5847
- q.parents.any? do |node|
5848
- node.is_a?(Command) || node.is_a?(CommandCall)
5928
+ q
5929
+ .if_break do
5930
+ force_parens =
5931
+ q.parents.any? do |node|
5932
+ node.is_a?(Command) || node.is_a?(CommandCall)
5933
+ end
5934
+
5935
+ q.text(force_parens ? "{" : "do")
5936
+ q.indent do
5937
+ q.breakable
5938
+ q.format(statements)
5849
5939
  end
5850
5940
 
5851
- q.text(force_parens ? "{" : "do")
5852
- q.indent do
5853
5941
  q.breakable
5942
+ q.text(force_parens ? "}" : "end")
5943
+ end
5944
+ .if_flat do
5945
+ q.text("{ ")
5854
5946
  q.format(statements)
5947
+ q.text(" }")
5855
5948
  end
5856
-
5857
- q.breakable
5858
- q.text(force_parens ? "}" : "end")
5859
- end.if_flat do
5860
- q.text("{ ")
5861
- q.format(statements)
5862
- q.text(" }")
5863
- end
5864
5949
  end
5865
5950
  end
5866
5951
  end
@@ -5889,7 +5974,7 @@ module SyntaxTree
5889
5974
 
5890
5975
  alias deconstruct child_nodes
5891
5976
 
5892
- def deconstruct_keys(keys)
5977
+ def deconstruct_keys(_keys)
5893
5978
  { value: value, location: location, comments: comments }
5894
5979
  end
5895
5980
 
@@ -5922,7 +6007,7 @@ module SyntaxTree
5922
6007
 
5923
6008
  alias deconstruct child_nodes
5924
6009
 
5925
- def deconstruct_keys(keys)
6010
+ def deconstruct_keys(_keys)
5926
6011
  { value: value, location: location, comments: comments }
5927
6012
  end
5928
6013
 
@@ -5955,7 +6040,7 @@ module SyntaxTree
5955
6040
 
5956
6041
  alias deconstruct child_nodes
5957
6042
 
5958
- def deconstruct_keys(keys)
6043
+ def deconstruct_keys(_keys)
5959
6044
  { value: value, location: location, comments: comments }
5960
6045
  end
5961
6046
 
@@ -6005,7 +6090,7 @@ module SyntaxTree
6005
6090
 
6006
6091
  alias deconstruct child_nodes
6007
6092
 
6008
- def deconstruct_keys(keys)
6093
+ def deconstruct_keys(_keys)
6009
6094
  { target: target, value: value, location: location, comments: comments }
6010
6095
  end
6011
6096
 
@@ -6052,7 +6137,7 @@ module SyntaxTree
6052
6137
 
6053
6138
  alias deconstruct child_nodes
6054
6139
 
6055
- def deconstruct_keys(keys)
6140
+ def deconstruct_keys(_keys)
6056
6141
  { call: call, block: block, location: location, comments: comments }
6057
6142
  end
6058
6143
 
@@ -6060,8 +6145,13 @@ module SyntaxTree
6060
6145
  # If we're at the top of a call chain, then we're going to do some
6061
6146
  # specialized printing in case we can print it nicely. We _only_ do this
6062
6147
  # at the top of the chain to avoid weird recursion issues.
6063
- if !CallChainFormatter.chained?(q.parent) && CallChainFormatter.chained?(call)
6064
- q.group { q.if_break { CallChainFormatter.new(self).format(q) }.if_flat { format_contents(q) } }
6148
+ if !CallChainFormatter.chained?(q.parent) &&
6149
+ CallChainFormatter.chained?(call)
6150
+ q.group do
6151
+ q
6152
+ .if_break { CallChainFormatter.new(self).format(q) }
6153
+ .if_flat { format_contents(q) }
6154
+ end
6065
6155
  else
6066
6156
  format_contents(q)
6067
6157
  end
@@ -6108,7 +6198,7 @@ module SyntaxTree
6108
6198
 
6109
6199
  alias deconstruct child_nodes
6110
6200
 
6111
- def deconstruct_keys(keys)
6201
+ def deconstruct_keys(_keys)
6112
6202
  { parts: parts, location: location, comma: comma, comments: comments }
6113
6203
  end
6114
6204
 
@@ -6152,7 +6242,7 @@ module SyntaxTree
6152
6242
 
6153
6243
  alias deconstruct child_nodes
6154
6244
 
6155
- def deconstruct_keys(keys)
6245
+ def deconstruct_keys(_keys)
6156
6246
  { contents: contents, location: location, comments: comments }
6157
6247
  end
6158
6248
 
@@ -6208,7 +6298,7 @@ module SyntaxTree
6208
6298
 
6209
6299
  alias deconstruct child_nodes
6210
6300
 
6211
- def deconstruct_keys(keys)
6301
+ def deconstruct_keys(_keys)
6212
6302
  {
6213
6303
  constant: constant,
6214
6304
  bodystmt: bodystmt,
@@ -6275,7 +6365,7 @@ module SyntaxTree
6275
6365
 
6276
6366
  alias deconstruct child_nodes
6277
6367
 
6278
- def deconstruct_keys(keys)
6368
+ def deconstruct_keys(_keys)
6279
6369
  { parts: parts, location: location, comments: comments }
6280
6370
  end
6281
6371
 
@@ -6324,7 +6414,7 @@ module SyntaxTree
6324
6414
 
6325
6415
  alias deconstruct child_nodes
6326
6416
 
6327
- def deconstruct_keys(keys)
6417
+ def deconstruct_keys(_keys)
6328
6418
  { arguments: arguments, location: location, comments: comments }
6329
6419
  end
6330
6420
 
@@ -6361,7 +6451,7 @@ module SyntaxTree
6361
6451
 
6362
6452
  alias deconstruct child_nodes
6363
6453
 
6364
- def deconstruct_keys(keys)
6454
+ def deconstruct_keys(_keys)
6365
6455
  { value: value, location: location, comments: comments }
6366
6456
  end
6367
6457
 
@@ -6407,7 +6497,7 @@ module SyntaxTree
6407
6497
 
6408
6498
  alias deconstruct child_nodes
6409
6499
 
6410
- def deconstruct_keys(keys)
6500
+ def deconstruct_keys(_keys)
6411
6501
  {
6412
6502
  target: target,
6413
6503
  operator: operator,
@@ -6475,7 +6565,16 @@ module SyntaxTree
6475
6565
  # This approach maintains the nice conciseness of the inline version, while
6476
6566
  # keeping the correct semantic meaning.
6477
6567
  module Parentheses
6478
- NODES = [Args, Assign, Assoc, Binary, Call, Defined, MAssign, OpAssign]
6568
+ NODES = [
6569
+ Args,
6570
+ Assign,
6571
+ Assoc,
6572
+ Binary,
6573
+ Call,
6574
+ Defined,
6575
+ MAssign,
6576
+ OpAssign
6577
+ ].freeze
6479
6578
 
6480
6579
  def self.flat(q)
6481
6580
  return yield unless NODES.include?(q.parent.class)
@@ -6507,6 +6606,8 @@ module SyntaxTree
6507
6606
  # def method(param) end
6508
6607
  #
6509
6608
  class Params < Node
6609
+ # Formats the optional position of the parameters. This includes the label,
6610
+ # as well as the default value.
6510
6611
  class OptionalFormatter
6511
6612
  # [Ident] the name of the parameter
6512
6613
  attr_reader :name
@@ -6530,6 +6631,8 @@ module SyntaxTree
6530
6631
  end
6531
6632
  end
6532
6633
 
6634
+ # Formats the keyword position of the parameters. This includes the label,
6635
+ # as well as an optional default value.
6533
6636
  class KeywordFormatter
6534
6637
  # [Ident] the name of the parameter
6535
6638
  attr_reader :name
@@ -6556,6 +6659,8 @@ module SyntaxTree
6556
6659
  end
6557
6660
  end
6558
6661
 
6662
+ # Formats the keyword_rest position of the parameters. This can be the **nil
6663
+ # syntax, the ... syntax, or the ** syntax.
6559
6664
  class KeywordRestFormatter
6560
6665
  # [:nil | ArgsForward | KwRestParam] the value of the parameter
6561
6666
  attr_reader :value
@@ -6569,11 +6674,7 @@ module SyntaxTree
6569
6674
  end
6570
6675
 
6571
6676
  def format(q)
6572
- if value == :nil
6573
- q.text("**nil")
6574
- else
6575
- q.format(value)
6576
- end
6677
+ value == :nil ? q.text("**nil") : q.format(value)
6577
6678
  end
6578
6679
  end
6579
6680
 
@@ -6654,7 +6755,7 @@ module SyntaxTree
6654
6755
 
6655
6756
  alias deconstruct child_nodes
6656
6757
 
6657
- def deconstruct_keys(keys)
6758
+ def deconstruct_keys(_keys)
6658
6759
  {
6659
6760
  location: location,
6660
6761
  requireds: requireds,
@@ -6675,18 +6776,17 @@ module SyntaxTree
6675
6776
  ]
6676
6777
 
6677
6778
  parts << rest if rest && !rest.is_a?(ExcessedComma)
6678
- parts +=
6679
- [
6680
- *posts,
6681
- *keywords.map { |(name, value)| KeywordFormatter.new(name, value) }
6682
- ]
6779
+ parts += [
6780
+ *posts,
6781
+ *keywords.map { |(name, value)| KeywordFormatter.new(name, value) }
6782
+ ]
6683
6783
 
6684
6784
  parts << KeywordRestFormatter.new(keyword_rest) if keyword_rest
6685
6785
  parts << block if block
6686
6786
 
6687
6787
  contents = -> do
6688
6788
  q.seplist(parts) { |part| q.format(part) }
6689
- q.format(rest) if rest && rest.is_a?(ExcessedComma)
6789
+ q.format(rest) if rest.is_a?(ExcessedComma)
6690
6790
  end
6691
6791
 
6692
6792
  if ![Def, Defs, DefEndless].include?(q.parent.class) || parts.empty?
@@ -6736,7 +6836,7 @@ module SyntaxTree
6736
6836
 
6737
6837
  alias deconstruct child_nodes
6738
6838
 
6739
- def deconstruct_keys(keys)
6839
+ def deconstruct_keys(_keys)
6740
6840
  {
6741
6841
  lparen: lparen,
6742
6842
  contents: contents,
@@ -6787,7 +6887,7 @@ module SyntaxTree
6787
6887
 
6788
6888
  alias deconstruct child_nodes
6789
6889
 
6790
- def deconstruct_keys(keys)
6890
+ def deconstruct_keys(_keys)
6791
6891
  { value: value, location: location, comments: comments }
6792
6892
  end
6793
6893
 
@@ -6820,7 +6920,7 @@ module SyntaxTree
6820
6920
 
6821
6921
  alias deconstruct child_nodes
6822
6922
 
6823
- def deconstruct_keys(keys)
6923
+ def deconstruct_keys(_keys)
6824
6924
  { statements: statements, location: location, comments: comments }
6825
6925
  end
6826
6926
 
@@ -6865,7 +6965,7 @@ module SyntaxTree
6865
6965
 
6866
6966
  alias deconstruct child_nodes
6867
6967
 
6868
- def deconstruct_keys(keys)
6968
+ def deconstruct_keys(_keys)
6869
6969
  {
6870
6970
  beginning: beginning,
6871
6971
  elements: elements,
@@ -6920,18 +7020,9 @@ module SyntaxTree
6920
7020
 
6921
7021
  alias deconstruct child_nodes
6922
7022
 
6923
- def deconstruct_keys(keys)
7023
+ def deconstruct_keys(_keys)
6924
7024
  { value: value, location: location }
6925
7025
  end
6926
-
6927
- def pretty_print(q)
6928
- q.group(2, "(", ")") do
6929
- q.text("qsymbols_beg")
6930
-
6931
- q.breakable
6932
- q.pp(value)
6933
- end
6934
- end
6935
7026
  end
6936
7027
 
6937
7028
  # QWords represents a string literal array without interpolation.
@@ -6965,7 +7056,7 @@ module SyntaxTree
6965
7056
 
6966
7057
  alias deconstruct child_nodes
6967
7058
 
6968
- def deconstruct_keys(keys)
7059
+ def deconstruct_keys(_keys)
6969
7060
  {
6970
7061
  beginning: beginning,
6971
7062
  elements: elements,
@@ -7020,7 +7111,7 @@ module SyntaxTree
7020
7111
 
7021
7112
  alias deconstruct child_nodes
7022
7113
 
7023
- def deconstruct_keys(keys)
7114
+ def deconstruct_keys(_keys)
7024
7115
  { value: value, location: location }
7025
7116
  end
7026
7117
  end
@@ -7052,7 +7143,7 @@ module SyntaxTree
7052
7143
 
7053
7144
  alias deconstruct child_nodes
7054
7145
 
7055
- def deconstruct_keys(keys)
7146
+ def deconstruct_keys(_keys)
7056
7147
  { value: value, location: location, comments: comments }
7057
7148
  end
7058
7149
 
@@ -7081,7 +7172,7 @@ module SyntaxTree
7081
7172
 
7082
7173
  alias deconstruct child_nodes
7083
7174
 
7084
- def deconstruct_keys(keys)
7175
+ def deconstruct_keys(_keys)
7085
7176
  { value: value, location: location }
7086
7177
  end
7087
7178
  end
@@ -7106,7 +7197,7 @@ module SyntaxTree
7106
7197
 
7107
7198
  alias deconstruct child_nodes
7108
7199
 
7109
- def deconstruct_keys(keys)
7200
+ def deconstruct_keys(_keys)
7110
7201
  { value: value, location: location }
7111
7202
  end
7112
7203
  end
@@ -7138,7 +7229,7 @@ module SyntaxTree
7138
7229
 
7139
7230
  alias deconstruct child_nodes
7140
7231
 
7141
- def deconstruct_keys(keys)
7232
+ def deconstruct_keys(_keys)
7142
7233
  { value: value, location: location, comments: comments }
7143
7234
  end
7144
7235
 
@@ -7177,7 +7268,7 @@ module SyntaxTree
7177
7268
 
7178
7269
  alias deconstruct child_nodes
7179
7270
 
7180
- def deconstruct_keys(keys)
7271
+ def deconstruct_keys(_keys)
7181
7272
  { beginning: beginning, parts: parts, location: location }
7182
7273
  end
7183
7274
  end
@@ -7210,7 +7301,7 @@ module SyntaxTree
7210
7301
 
7211
7302
  alias deconstruct child_nodes
7212
7303
 
7213
- def deconstruct_keys(keys)
7304
+ def deconstruct_keys(_keys)
7214
7305
  { value: value, location: location }
7215
7306
  end
7216
7307
  end
@@ -7244,7 +7335,7 @@ module SyntaxTree
7244
7335
 
7245
7336
  alias deconstruct child_nodes
7246
7337
 
7247
- def deconstruct_keys(keys)
7338
+ def deconstruct_keys(_keys)
7248
7339
  { value: value, location: location }
7249
7340
  end
7250
7341
  end
@@ -7285,10 +7376,11 @@ module SyntaxTree
7285
7376
 
7286
7377
  alias deconstruct child_nodes
7287
7378
 
7288
- def deconstruct_keys(keys)
7379
+ def deconstruct_keys(_keys)
7289
7380
  {
7290
7381
  beginning: beginning,
7291
7382
  ending: ending,
7383
+ options: options,
7292
7384
  parts: parts,
7293
7385
  location: location,
7294
7386
  comments: comments
@@ -7296,7 +7388,7 @@ module SyntaxTree
7296
7388
  end
7297
7389
 
7298
7390
  def format(q)
7299
- braces = ambiguous?(q) || include?(%r{\/})
7391
+ braces = ambiguous?(q) || include?(%r{/})
7300
7392
 
7301
7393
  if braces && include?(/[{}]/)
7302
7394
  q.group do
@@ -7323,18 +7415,22 @@ module SyntaxTree
7323
7415
  end
7324
7416
 
7325
7417
  q.text("}")
7326
- q.text(ending[1..-1])
7418
+ q.text(options)
7327
7419
  end
7328
7420
  else
7329
7421
  q.group do
7330
7422
  q.text("/")
7331
7423
  q.format_each(parts)
7332
7424
  q.text("/")
7333
- q.text(ending[1..-1])
7425
+ q.text(options)
7334
7426
  end
7335
7427
  end
7336
7428
  end
7337
7429
 
7430
+ def options
7431
+ ending[1..]
7432
+ end
7433
+
7338
7434
  private
7339
7435
 
7340
7436
  def include?(pattern)
@@ -7390,7 +7486,7 @@ module SyntaxTree
7390
7486
 
7391
7487
  alias deconstruct child_nodes
7392
7488
 
7393
- def deconstruct_keys(keys)
7489
+ def deconstruct_keys(_keys)
7394
7490
  {
7395
7491
  exceptions: exceptions,
7396
7492
  variable: variable,
@@ -7465,7 +7561,10 @@ module SyntaxTree
7465
7561
 
7466
7562
  if consequent
7467
7563
  consequent.bind_end(end_char, end_column)
7468
- statements.bind_end(consequent.location.start_char, consequent.location.start_column)
7564
+ statements.bind_end(
7565
+ consequent.location.start_char,
7566
+ consequent.location.start_column
7567
+ )
7469
7568
  else
7470
7569
  statements.bind_end(end_char, end_column)
7471
7570
  end
@@ -7481,7 +7580,7 @@ module SyntaxTree
7481
7580
 
7482
7581
  alias deconstruct child_nodes
7483
7582
 
7484
- def deconstruct_keys(keys)
7583
+ def deconstruct_keys(_keys)
7485
7584
  {
7486
7585
  keyword: keyword,
7487
7586
  exception: exception,
@@ -7548,7 +7647,7 @@ module SyntaxTree
7548
7647
 
7549
7648
  alias deconstruct child_nodes
7550
7649
 
7551
- def deconstruct_keys(keys)
7650
+ def deconstruct_keys(_keys)
7552
7651
  {
7553
7652
  statement: statement,
7554
7653
  value: value,
@@ -7602,7 +7701,7 @@ module SyntaxTree
7602
7701
 
7603
7702
  alias deconstruct child_nodes
7604
7703
 
7605
- def deconstruct_keys(keys)
7704
+ def deconstruct_keys(_keys)
7606
7705
  { name: name, location: location, comments: comments }
7607
7706
  end
7608
7707
 
@@ -7639,7 +7738,7 @@ module SyntaxTree
7639
7738
 
7640
7739
  alias deconstruct child_nodes
7641
7740
 
7642
- def deconstruct_keys(keys)
7741
+ def deconstruct_keys(_keys)
7643
7742
  { value: value, location: location, comments: comments }
7644
7743
  end
7645
7744
 
@@ -7675,7 +7774,7 @@ module SyntaxTree
7675
7774
 
7676
7775
  alias deconstruct child_nodes
7677
7776
 
7678
- def deconstruct_keys(keys)
7777
+ def deconstruct_keys(_keys)
7679
7778
  { arguments: arguments, location: location, comments: comments }
7680
7779
  end
7681
7780
 
@@ -7711,7 +7810,7 @@ module SyntaxTree
7711
7810
 
7712
7811
  alias deconstruct child_nodes
7713
7812
 
7714
- def deconstruct_keys(keys)
7813
+ def deconstruct_keys(_keys)
7715
7814
  { value: value, location: location, comments: comments }
7716
7815
  end
7717
7816
 
@@ -7740,7 +7839,7 @@ module SyntaxTree
7740
7839
 
7741
7840
  alias deconstruct child_nodes
7742
7841
 
7743
- def deconstruct_keys(keys)
7842
+ def deconstruct_keys(_keys)
7744
7843
  { value: value, location: location }
7745
7844
  end
7746
7845
  end
@@ -7779,7 +7878,7 @@ module SyntaxTree
7779
7878
 
7780
7879
  alias deconstruct child_nodes
7781
7880
 
7782
- def deconstruct_keys(keys)
7881
+ def deconstruct_keys(_keys)
7783
7882
  {
7784
7883
  target: target,
7785
7884
  bodystmt: bodystmt,
@@ -7881,7 +7980,7 @@ module SyntaxTree
7881
7980
 
7882
7981
  alias deconstruct child_nodes
7883
7982
 
7884
- def deconstruct_keys(keys)
7983
+ def deconstruct_keys(_keys)
7885
7984
  { parser: parser, body: body, location: location, comments: comments }
7886
7985
  end
7887
7986
 
@@ -7951,12 +8050,19 @@ module SyntaxTree
7951
8050
  comment = parser_comments[comment_index]
7952
8051
  location = comment.location
7953
8052
 
7954
- if !comment.inline? && (start_char <= location.start_char) && (end_char >= location.end_char) && !comment.ignore?
7955
- while (node = body[body_index]) && (node.is_a?(VoidStmt) || node.location.start_char < location.start_char)
8053
+ if !comment.inline? && (start_char <= location.start_char) &&
8054
+ (end_char >= location.end_char) && !comment.ignore?
8055
+ while (node = body[body_index]) &&
8056
+ (
8057
+ node.is_a?(VoidStmt) ||
8058
+ node.location.start_char < location.start_char
8059
+ )
7956
8060
  body_index += 1
7957
8061
  end
7958
8062
 
7959
- if body_index != 0 && body[body_index - 1].location.start_char < location.start_char && body[body_index - 1].location.end_char > location.start_char
8063
+ if body_index != 0 &&
8064
+ body[body_index - 1].location.start_char < location.start_char &&
8065
+ body[body_index - 1].location.end_char > location.start_char
7960
8066
  # The previous node entirely encapsules the comment, so we don't
7961
8067
  # want to attach it here since it will get attached normally. This
7962
8068
  # is mostly in the case of hash and array literals.
@@ -7996,7 +8102,7 @@ module SyntaxTree
7996
8102
 
7997
8103
  alias deconstruct child_nodes
7998
8104
 
7999
- def deconstruct_keys(keys)
8105
+ def deconstruct_keys(_keys)
8000
8106
  { parts: parts, location: location }
8001
8107
  end
8002
8108
  end
@@ -8034,14 +8140,14 @@ module SyntaxTree
8034
8140
 
8035
8141
  alias deconstruct child_nodes
8036
8142
 
8037
- def deconstruct_keys(keys)
8143
+ def deconstruct_keys(_keys)
8038
8144
  { left: left, right: right, location: location, comments: comments }
8039
8145
  end
8040
8146
 
8041
8147
  def format(q)
8042
8148
  q.group do
8043
8149
  q.format(left)
8044
- q.text(' \\')
8150
+ q.text(" \\")
8045
8151
  q.indent do
8046
8152
  q.breakable(force: true)
8047
8153
  q.format(right)
@@ -8079,7 +8185,7 @@ module SyntaxTree
8079
8185
 
8080
8186
  alias deconstruct child_nodes
8081
8187
 
8082
- def deconstruct_keys(keys)
8188
+ def deconstruct_keys(_keys)
8083
8189
  { variable: variable, location: location, comments: comments }
8084
8190
  end
8085
8191
 
@@ -8119,7 +8225,7 @@ module SyntaxTree
8119
8225
 
8120
8226
  alias deconstruct child_nodes
8121
8227
 
8122
- def deconstruct_keys(keys)
8228
+ def deconstruct_keys(_keys)
8123
8229
  { statements: statements, location: location, comments: comments }
8124
8230
  end
8125
8231
 
@@ -8129,8 +8235,7 @@ module SyntaxTree
8129
8235
  # same line in the source, then we're going to leave them in place and
8130
8236
  # assume that's the way the developer wanted this expression
8131
8237
  # represented.
8132
- doc = q.group(0, '#{', "}") { q.format(statements) }
8133
- RemoveBreaks.call(doc)
8238
+ q.remove_breaks(q.group(0, '#{', "}") { q.format(statements) })
8134
8239
  else
8135
8240
  q.group do
8136
8241
  q.text('#{')
@@ -8177,7 +8282,7 @@ module SyntaxTree
8177
8282
 
8178
8283
  alias deconstruct child_nodes
8179
8284
 
8180
- def deconstruct_keys(keys)
8285
+ def deconstruct_keys(_keys)
8181
8286
  { parts: parts, quote: quote, location: location, comments: comments }
8182
8287
  end
8183
8288
 
@@ -8240,7 +8345,7 @@ module SyntaxTree
8240
8345
 
8241
8346
  alias deconstruct child_nodes
8242
8347
 
8243
- def deconstruct_keys(keys)
8348
+ def deconstruct_keys(_keys)
8244
8349
  { arguments: arguments, location: location, comments: comments }
8245
8350
  end
8246
8351
 
@@ -8293,7 +8398,7 @@ module SyntaxTree
8293
8398
 
8294
8399
  alias deconstruct child_nodes
8295
8400
 
8296
- def deconstruct_keys(keys)
8401
+ def deconstruct_keys(_keys)
8297
8402
  { value: value, location: location }
8298
8403
  end
8299
8404
  end
@@ -8323,7 +8428,7 @@ module SyntaxTree
8323
8428
 
8324
8429
  alias deconstruct child_nodes
8325
8430
 
8326
- def deconstruct_keys(keys)
8431
+ def deconstruct_keys(_keys)
8327
8432
  { value: value, location: location }
8328
8433
  end
8329
8434
  end
@@ -8357,7 +8462,7 @@ module SyntaxTree
8357
8462
 
8358
8463
  alias deconstruct child_nodes
8359
8464
 
8360
- def deconstruct_keys(keys)
8465
+ def deconstruct_keys(_keys)
8361
8466
  { value: value, location: location, comments: comments }
8362
8467
  end
8363
8468
 
@@ -8398,7 +8503,7 @@ module SyntaxTree
8398
8503
 
8399
8504
  alias deconstruct child_nodes
8400
8505
 
8401
- def deconstruct_keys(keys)
8506
+ def deconstruct_keys(_keys)
8402
8507
  {
8403
8508
  beginning: beginning,
8404
8509
  elements: elements,
@@ -8454,7 +8559,7 @@ module SyntaxTree
8454
8559
 
8455
8560
  alias deconstruct child_nodes
8456
8561
 
8457
- def deconstruct_keys(keys)
8562
+ def deconstruct_keys(_keys)
8458
8563
  { value: value, location: location }
8459
8564
  end
8460
8565
  end
@@ -8483,7 +8588,7 @@ module SyntaxTree
8483
8588
 
8484
8589
  alias deconstruct child_nodes
8485
8590
 
8486
- def deconstruct_keys(keys)
8591
+ def deconstruct_keys(_keys)
8487
8592
  { value: value, location: location }
8488
8593
  end
8489
8594
  end
@@ -8513,7 +8618,7 @@ module SyntaxTree
8513
8618
 
8514
8619
  alias deconstruct child_nodes
8515
8620
 
8516
- def deconstruct_keys(keys)
8621
+ def deconstruct_keys(_keys)
8517
8622
  { value: value, location: location }
8518
8623
  end
8519
8624
  end
@@ -8547,7 +8652,7 @@ module SyntaxTree
8547
8652
 
8548
8653
  alias deconstruct child_nodes
8549
8654
 
8550
- def deconstruct_keys(keys)
8655
+ def deconstruct_keys(_keys)
8551
8656
  { constant: constant, location: location, comments: comments }
8552
8657
  end
8553
8658
 
@@ -8585,7 +8690,7 @@ module SyntaxTree
8585
8690
 
8586
8691
  alias deconstruct child_nodes
8587
8692
 
8588
- def deconstruct_keys(keys)
8693
+ def deconstruct_keys(_keys)
8589
8694
  { constant: constant, location: location, comments: comments }
8590
8695
  end
8591
8696
 
@@ -8624,7 +8729,7 @@ module SyntaxTree
8624
8729
 
8625
8730
  alias deconstruct child_nodes
8626
8731
 
8627
- def deconstruct_keys(keys)
8732
+ def deconstruct_keys(_keys)
8628
8733
  { value: value, location: location }
8629
8734
  end
8630
8735
  end
@@ -8664,7 +8769,7 @@ module SyntaxTree
8664
8769
 
8665
8770
  alias deconstruct child_nodes
8666
8771
 
8667
- def deconstruct_keys(keys)
8772
+ def deconstruct_keys(_keys)
8668
8773
  { value: value, location: location, comments: comments }
8669
8774
  end
8670
8775
 
@@ -8702,7 +8807,7 @@ module SyntaxTree
8702
8807
 
8703
8808
  alias deconstruct child_nodes
8704
8809
 
8705
- def deconstruct_keys(keys)
8810
+ def deconstruct_keys(_keys)
8706
8811
  { value: value, location: location }
8707
8812
  end
8708
8813
  end
@@ -8738,7 +8843,7 @@ module SyntaxTree
8738
8843
 
8739
8844
  alias deconstruct child_nodes
8740
8845
 
8741
- def deconstruct_keys(keys)
8846
+ def deconstruct_keys(_keys)
8742
8847
  {
8743
8848
  statement: statement,
8744
8849
  parentheses: parentheses,
@@ -8749,7 +8854,9 @@ module SyntaxTree
8749
8854
 
8750
8855
  def format(q)
8751
8856
  parent = q.parents.take(2)[1]
8752
- ternary = (parent.is_a?(If) || parent.is_a?(Unless)) && Ternaryable.call(q, parent)
8857
+ ternary =
8858
+ (parent.is_a?(If) || parent.is_a?(Unless)) &&
8859
+ Ternaryable.call(q, parent)
8753
8860
 
8754
8861
  q.text("not")
8755
8862
 
@@ -8803,7 +8910,7 @@ module SyntaxTree
8803
8910
 
8804
8911
  alias deconstruct child_nodes
8805
8912
 
8806
- def deconstruct_keys(keys)
8913
+ def deconstruct_keys(_keys)
8807
8914
  {
8808
8915
  operator: operator,
8809
8916
  statement: statement,
@@ -8823,6 +8930,9 @@ module SyntaxTree
8823
8930
  # undef method
8824
8931
  #
8825
8932
  class Undef < Node
8933
+ # Undef accepts a variable number of arguments that can be either DynaSymbol
8934
+ # or SymbolLiteral objects. For SymbolLiteral objects we descend directly
8935
+ # into the value in order to have it come out as bare words.
8826
8936
  class UndefArgumentFormatter
8827
8937
  # [DynaSymbol | SymbolLiteral] the symbol to undefine
8828
8938
  attr_reader :node
@@ -8866,7 +8976,7 @@ module SyntaxTree
8866
8976
 
8867
8977
  alias deconstruct child_nodes
8868
8978
 
8869
- def deconstruct_keys(keys)
8979
+ def deconstruct_keys(_keys)
8870
8980
  { symbols: symbols, location: location, comments: comments }
8871
8981
  end
8872
8982
 
@@ -8925,7 +9035,7 @@ module SyntaxTree
8925
9035
 
8926
9036
  alias deconstruct child_nodes
8927
9037
 
8928
- def deconstruct_keys(keys)
9038
+ def deconstruct_keys(_keys)
8929
9039
  {
8930
9040
  predicate: predicate,
8931
9041
  statements: statements,
@@ -8971,7 +9081,7 @@ module SyntaxTree
8971
9081
 
8972
9082
  alias deconstruct child_nodes
8973
9083
 
8974
- def deconstruct_keys(keys)
9084
+ def deconstruct_keys(_keys)
8975
9085
  {
8976
9086
  statement: statement,
8977
9087
  predicate: predicate,
@@ -9010,13 +9120,15 @@ module SyntaxTree
9010
9120
  end
9011
9121
 
9012
9122
  q.group do
9013
- q.if_break { format_break(q) }.if_flat do
9014
- Parentheses.flat(q) do
9015
- q.format(statements)
9016
- q.text(" #{keyword} ")
9017
- q.format(node.predicate)
9123
+ q
9124
+ .if_break { format_break(q) }
9125
+ .if_flat do
9126
+ Parentheses.flat(q) do
9127
+ q.format(statements)
9128
+ q.text(" #{keyword} ")
9129
+ q.format(node.predicate)
9130
+ end
9018
9131
  end
9019
- end
9020
9132
  end
9021
9133
  end
9022
9134
 
@@ -9066,7 +9178,7 @@ module SyntaxTree
9066
9178
 
9067
9179
  alias deconstruct child_nodes
9068
9180
 
9069
- def deconstruct_keys(keys)
9181
+ def deconstruct_keys(_keys)
9070
9182
  {
9071
9183
  predicate: predicate,
9072
9184
  statements: statements,
@@ -9122,7 +9234,7 @@ module SyntaxTree
9122
9234
 
9123
9235
  alias deconstruct child_nodes
9124
9236
 
9125
- def deconstruct_keys(keys)
9237
+ def deconstruct_keys(_keys)
9126
9238
  {
9127
9239
  statement: statement,
9128
9240
  predicate: predicate,
@@ -9188,7 +9300,7 @@ module SyntaxTree
9188
9300
 
9189
9301
  alias deconstruct child_nodes
9190
9302
 
9191
- def deconstruct_keys(keys)
9303
+ def deconstruct_keys(_keys)
9192
9304
  { left: left, right: right, location: location, comments: comments }
9193
9305
  end
9194
9306
 
@@ -9231,7 +9343,7 @@ module SyntaxTree
9231
9343
 
9232
9344
  alias deconstruct child_nodes
9233
9345
 
9234
- def deconstruct_keys(keys)
9346
+ def deconstruct_keys(_keys)
9235
9347
  { value: value, location: location, comments: comments }
9236
9348
  end
9237
9349
 
@@ -9275,7 +9387,7 @@ module SyntaxTree
9275
9387
 
9276
9388
  alias deconstruct child_nodes
9277
9389
 
9278
- def deconstruct_keys(keys)
9390
+ def deconstruct_keys(_keys)
9279
9391
  { value: value, location: location, comments: comments }
9280
9392
  end
9281
9393
 
@@ -9316,7 +9428,7 @@ module SyntaxTree
9316
9428
 
9317
9429
  alias deconstruct child_nodes
9318
9430
 
9319
- def deconstruct_keys(keys)
9431
+ def deconstruct_keys(_keys)
9320
9432
  { value: value, location: location, comments: comments }
9321
9433
  end
9322
9434
 
@@ -9356,7 +9468,7 @@ module SyntaxTree
9356
9468
 
9357
9469
  alias deconstruct child_nodes
9358
9470
 
9359
- def deconstruct_keys(keys)
9471
+ def deconstruct_keys(_keys)
9360
9472
  { value: value, location: location, comments: comments }
9361
9473
  end
9362
9474
 
@@ -9391,7 +9503,7 @@ module SyntaxTree
9391
9503
 
9392
9504
  alias deconstruct child_nodes
9393
9505
 
9394
- def deconstruct_keys(keys)
9506
+ def deconstruct_keys(_keys)
9395
9507
  { location: location, comments: comments }
9396
9508
  end
9397
9509
 
@@ -9442,7 +9554,7 @@ module SyntaxTree
9442
9554
 
9443
9555
  alias deconstruct child_nodes
9444
9556
 
9445
- def deconstruct_keys(keys)
9557
+ def deconstruct_keys(_keys)
9446
9558
  {
9447
9559
  arguments: arguments,
9448
9560
  statements: statements,
@@ -9523,7 +9635,7 @@ module SyntaxTree
9523
9635
 
9524
9636
  alias deconstruct child_nodes
9525
9637
 
9526
- def deconstruct_keys(keys)
9638
+ def deconstruct_keys(_keys)
9527
9639
  {
9528
9640
  predicate: predicate,
9529
9641
  statements: statements,
@@ -9579,7 +9691,7 @@ module SyntaxTree
9579
9691
 
9580
9692
  alias deconstruct child_nodes
9581
9693
 
9582
- def deconstruct_keys(keys)
9694
+ def deconstruct_keys(_keys)
9583
9695
  {
9584
9696
  statement: statement,
9585
9697
  predicate: predicate,
@@ -9648,7 +9760,7 @@ module SyntaxTree
9648
9760
 
9649
9761
  alias deconstruct child_nodes
9650
9762
 
9651
- def deconstruct_keys(keys)
9763
+ def deconstruct_keys(_keys)
9652
9764
  { parts: parts, location: location, comments: comments }
9653
9765
  end
9654
9766
 
@@ -9688,7 +9800,7 @@ module SyntaxTree
9688
9800
 
9689
9801
  alias deconstruct child_nodes
9690
9802
 
9691
- def deconstruct_keys(keys)
9803
+ def deconstruct_keys(_keys)
9692
9804
  {
9693
9805
  beginning: beginning,
9694
9806
  elements: elements,
@@ -9744,7 +9856,7 @@ module SyntaxTree
9744
9856
 
9745
9857
  alias deconstruct child_nodes
9746
9858
 
9747
- def deconstruct_keys(keys)
9859
+ def deconstruct_keys(_keys)
9748
9860
  { value: value, location: location }
9749
9861
  end
9750
9862
  end
@@ -9773,7 +9885,7 @@ module SyntaxTree
9773
9885
 
9774
9886
  alias deconstruct child_nodes
9775
9887
 
9776
- def deconstruct_keys(keys)
9888
+ def deconstruct_keys(_keys)
9777
9889
  { parts: parts, location: location }
9778
9890
  end
9779
9891
  end
@@ -9806,7 +9918,7 @@ module SyntaxTree
9806
9918
 
9807
9919
  alias deconstruct child_nodes
9808
9920
 
9809
- def deconstruct_keys(keys)
9921
+ def deconstruct_keys(_keys)
9810
9922
  { parts: parts, location: location, comments: comments }
9811
9923
  end
9812
9924
 
@@ -9844,7 +9956,7 @@ module SyntaxTree
9844
9956
 
9845
9957
  alias deconstruct child_nodes
9846
9958
 
9847
- def deconstruct_keys(keys)
9959
+ def deconstruct_keys(_keys)
9848
9960
  { arguments: arguments, location: location, comments: comments }
9849
9961
  end
9850
9962
 
@@ -9894,7 +10006,7 @@ module SyntaxTree
9894
10006
 
9895
10007
  alias deconstruct child_nodes
9896
10008
 
9897
- def deconstruct_keys(keys)
10009
+ def deconstruct_keys(_keys)
9898
10010
  { value: value, location: location, comments: comments }
9899
10011
  end
9900
10012
 
@@ -9930,7 +10042,7 @@ module SyntaxTree
9930
10042
 
9931
10043
  alias deconstruct child_nodes
9932
10044
 
9933
- def deconstruct_keys(keys)
10045
+ def deconstruct_keys(_keys)
9934
10046
  { value: value, location: location, comments: comments }
9935
10047
  end
9936
10048