syntax_tree 2.3.1 → 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.
@@ -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
+ PP.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-Za-z]([_A-Za-z0-9]*)?([!_=?A-Za-z0-9])?$/
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,
@@ -1682,7 +1746,7 @@ module SyntaxTree
1682
1746
 
1683
1747
  alias deconstruct child_nodes
1684
1748
 
1685
- def deconstruct_keys(keys)
1749
+ def deconstruct_keys(_keys)
1686
1750
  { params: params, locals: locals, location: location, comments: comments }
1687
1751
  end
1688
1752
 
@@ -1726,7 +1790,7 @@ module SyntaxTree
1726
1790
 
1727
1791
  alias deconstruct child_nodes
1728
1792
 
1729
- def deconstruct_keys(keys)
1793
+ def deconstruct_keys(_keys)
1730
1794
  { name: name, location: location, comments: comments }
1731
1795
  end
1732
1796
 
@@ -1822,7 +1886,7 @@ module SyntaxTree
1822
1886
 
1823
1887
  alias deconstruct child_nodes
1824
1888
 
1825
- def deconstruct_keys(keys)
1889
+ def deconstruct_keys(_keys)
1826
1890
  {
1827
1891
  statements: statements,
1828
1892
  rescue_clause: rescue_clause,
@@ -1868,6 +1932,7 @@ module SyntaxTree
1868
1932
 
1869
1933
  # Responsible for formatting either a BraceBlock or a DoBlock.
1870
1934
  class BlockFormatter
1935
+ # Formats the opening brace or keyword of a block.
1871
1936
  class BlockOpenFormatter
1872
1937
  # [String] the actual output that should be printed
1873
1938
  attr_reader :text
@@ -1933,9 +1998,9 @@ module SyntaxTree
1933
1998
  end
1934
1999
 
1935
2000
  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
2001
+ q
2002
+ .if_break { format_break(q, break_opening, break_closing) }
2003
+ .if_flat { format_flat(q, flat_opening, flat_closing) }
1939
2004
  end
1940
2005
  end
1941
2006
 
@@ -2060,7 +2125,7 @@ module SyntaxTree
2060
2125
 
2061
2126
  alias deconstruct child_nodes
2062
2127
 
2063
- def deconstruct_keys(keys)
2128
+ def deconstruct_keys(_keys)
2064
2129
  {
2065
2130
  lbrace: lbrace,
2066
2131
  block_var: block_var,
@@ -2099,7 +2164,11 @@ module SyntaxTree
2099
2164
  #
2100
2165
  # break
2101
2166
  #
2102
- in [Paren[contents: { body: [ArrayLiteral[contents: { parts: [_, _, *] }] => array] }]]
2167
+ in [Paren[
2168
+ contents: {
2169
+ body: [ArrayLiteral[contents: { parts: [_, _, *] }] => array]
2170
+ }
2171
+ ]]
2103
2172
  # Here we have a single argument that is a set of parentheses wrapping
2104
2173
  # an array literal that has at least 2 elements. We're going to print
2105
2174
  # the contents of the array directly. This would be like if we had:
@@ -2255,7 +2324,7 @@ module SyntaxTree
2255
2324
 
2256
2325
  alias deconstruct child_nodes
2257
2326
 
2258
- def deconstruct_keys(keys)
2327
+ def deconstruct_keys(_keys)
2259
2328
  { arguments: arguments, location: location, comments: comments }
2260
2329
  end
2261
2330
 
@@ -2287,6 +2356,20 @@ module SyntaxTree
2287
2356
  end
2288
2357
  end
2289
2358
 
2359
+ # This is probably the most complicated formatter in this file. It's
2360
+ # responsible for formatting chains of method calls, with or without arguments
2361
+ # or blocks. In general, we want to go from something like
2362
+ #
2363
+ # foo.bar.baz
2364
+ #
2365
+ # to
2366
+ #
2367
+ # foo
2368
+ # .bar
2369
+ # .baz
2370
+ #
2371
+ # Of course there are a lot of caveats to that, including trailing operators
2372
+ # when necessary, where comments are places, how blocks are aligned, etc.
2290
2373
  class CallChainFormatter
2291
2374
  # [Call | MethodAddBlock] the top of the call chain
2292
2375
  attr_reader :node
@@ -2301,7 +2384,7 @@ module SyntaxTree
2301
2384
 
2302
2385
  # First, walk down the chain until we get to the point where we're not
2303
2386
  # longer at a chainable node.
2304
- while true
2387
+ loop do
2305
2388
  case children.last
2306
2389
  in Call[receiver: Call]
2307
2390
  children << children.last.receiver
@@ -2321,7 +2404,7 @@ module SyntaxTree
2321
2404
  # block. For more details, see
2322
2405
  # https://github.com/prettier/plugin-ruby/issues/863.
2323
2406
  parents = q.parents.take(4)
2324
- if parent = parents[2]
2407
+ if (parent = parents[2])
2325
2408
  # If we're at a do_block, then we want to go one more level up. This is
2326
2409
  # because do blocks have BodyStmt nodes instead of just Statements
2327
2410
  # nodes.
@@ -2335,7 +2418,11 @@ module SyntaxTree
2335
2418
  end
2336
2419
 
2337
2420
  if children.length >= threshold
2338
- q.group { q.if_break { format_chain(q, children) }.if_flat { node.format_contents(q) } }
2421
+ q.group do
2422
+ q
2423
+ .if_break { format_chain(q, children) }
2424
+ .if_flat { node.format_contents(q) }
2425
+ end
2339
2426
  else
2340
2427
  node.format_contents(q)
2341
2428
  end
@@ -2346,9 +2433,9 @@ module SyntaxTree
2346
2433
  # chain of calls without arguments except for the last one. This is common
2347
2434
  # enough in Ruby source code that it's worth the extra complexity here.
2348
2435
  empty_except_last =
2349
- children.drop(1).all? do |child|
2350
- child.is_a?(Call) && child.arguments.nil?
2351
- end
2436
+ children
2437
+ .drop(1)
2438
+ .all? { |child| child.is_a?(Call) && child.arguments.nil? }
2352
2439
 
2353
2440
  # Here, we're going to add all of the children onto the stack of the
2354
2441
  # formatter so it's as if we had descending normally into them. This is
@@ -2368,9 +2455,12 @@ module SyntaxTree
2368
2455
  # and a trailing operator.
2369
2456
  skip_operator = false
2370
2457
 
2371
- while child = children.pop
2458
+ while (child = children.pop)
2372
2459
  case child
2373
- in Call[receiver: Call[message: { value: "where" }], message: { value: "not" }]
2460
+ in Call[
2461
+ receiver: Call[message: { value: "where" }],
2462
+ message: { value: "not" }
2463
+ ]
2374
2464
  # This is very specialized behavior wherein we group
2375
2465
  # .where.not calls together because it looks better. For more
2376
2466
  # information, see
@@ -2432,16 +2522,25 @@ module SyntaxTree
2432
2522
  # want to indent the first call. So we'll pop off the first children and
2433
2523
  # format it separately here.
2434
2524
  def attach_directly?(node)
2435
- [ArrayLiteral, HashLiteral, Heredoc, If, Unless, XStringLiteral]
2436
- .include?(node.receiver.class)
2525
+ [ArrayLiteral, HashLiteral, Heredoc, If, Unless, XStringLiteral].include?(
2526
+ node.receiver.class
2527
+ )
2437
2528
  end
2438
2529
 
2439
- def format_child(q, child, skip_comments: false, skip_operator: false, skip_attached: false)
2530
+ def format_child(
2531
+ q,
2532
+ child,
2533
+ skip_comments: false,
2534
+ skip_operator: false,
2535
+ skip_attached: false
2536
+ )
2440
2537
  # First, format the actual contents of the child.
2441
2538
  case child
2442
2539
  in Call
2443
2540
  q.group do
2444
- q.format(CallOperatorFormatter.new(child.operator)) unless skip_operator
2541
+ unless skip_operator
2542
+ q.format(CallOperatorFormatter.new(child.operator))
2543
+ end
2445
2544
  q.format(child.message) if child.message != :call
2446
2545
  child.format_arguments(q) unless skip_attached
2447
2546
  end
@@ -2513,7 +2612,7 @@ module SyntaxTree
2513
2612
 
2514
2613
  alias deconstruct child_nodes
2515
2614
 
2516
- def deconstruct_keys(keys)
2615
+ def deconstruct_keys(_keys)
2517
2616
  {
2518
2617
  receiver: receiver,
2519
2618
  operator: operator,
@@ -2528,8 +2627,13 @@ module SyntaxTree
2528
2627
  # If we're at the top of a call chain, then we're going to do some
2529
2628
  # specialized printing in case we can print it nicely. We _only_ do this
2530
2629
  # 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) } }
2630
+ if !CallChainFormatter.chained?(q.parent) &&
2631
+ CallChainFormatter.chained?(receiver)
2632
+ q.group do
2633
+ q
2634
+ .if_break { CallChainFormatter.new(self).format(q) }
2635
+ .if_flat { format_contents(q) }
2636
+ end
2533
2637
  else
2534
2638
  format_contents(q)
2535
2639
  end
@@ -2618,7 +2722,7 @@ module SyntaxTree
2618
2722
 
2619
2723
  alias deconstruct child_nodes
2620
2724
 
2621
- def deconstruct_keys(keys)
2725
+ def deconstruct_keys(_keys)
2622
2726
  {
2623
2727
  keyword: keyword,
2624
2728
  value: value,
@@ -2683,7 +2787,7 @@ module SyntaxTree
2683
2787
 
2684
2788
  alias deconstruct child_nodes
2685
2789
 
2686
- def deconstruct_keys(keys)
2790
+ def deconstruct_keys(_keys)
2687
2791
  {
2688
2792
  value: value,
2689
2793
  operator: operator,
@@ -2772,7 +2876,7 @@ module SyntaxTree
2772
2876
 
2773
2877
  alias deconstruct child_nodes
2774
2878
 
2775
- def deconstruct_keys(keys)
2879
+ def deconstruct_keys(_keys)
2776
2880
  {
2777
2881
  constant: constant,
2778
2882
  superclass: superclass,
@@ -2837,7 +2941,7 @@ module SyntaxTree
2837
2941
 
2838
2942
  alias deconstruct child_nodes
2839
2943
 
2840
- def deconstruct_keys(keys)
2944
+ def deconstruct_keys(_keys)
2841
2945
  { value: value, location: location }
2842
2946
  end
2843
2947
  end
@@ -2875,7 +2979,7 @@ module SyntaxTree
2875
2979
 
2876
2980
  alias deconstruct child_nodes
2877
2981
 
2878
- def deconstruct_keys(keys)
2982
+ def deconstruct_keys(_keys)
2879
2983
  {
2880
2984
  message: message,
2881
2985
  arguments: arguments,
@@ -2957,7 +3061,7 @@ module SyntaxTree
2957
3061
 
2958
3062
  alias deconstruct child_nodes
2959
3063
 
2960
- def deconstruct_keys(keys)
3064
+ def deconstruct_keys(_keys)
2961
3065
  {
2962
3066
  receiver: receiver,
2963
3067
  operator: operator,
@@ -3079,7 +3183,7 @@ module SyntaxTree
3079
3183
  end
3080
3184
 
3081
3185
  def ignore?
3082
- value[1..-1].strip == "stree-ignore"
3186
+ value[1..].strip == "stree-ignore"
3083
3187
  end
3084
3188
 
3085
3189
  def comments
@@ -3096,7 +3200,7 @@ module SyntaxTree
3096
3200
 
3097
3201
  alias deconstruct child_nodes
3098
3202
 
3099
- def deconstruct_keys(keys)
3203
+ def deconstruct_keys(_keys)
3100
3204
  { value: value, inline: inline, location: location }
3101
3205
  end
3102
3206
 
@@ -3142,7 +3246,7 @@ module SyntaxTree
3142
3246
 
3143
3247
  alias deconstruct child_nodes
3144
3248
 
3145
- def deconstruct_keys(keys)
3249
+ def deconstruct_keys(_keys)
3146
3250
  { value: value, location: location, comments: comments }
3147
3251
  end
3148
3252
 
@@ -3184,7 +3288,7 @@ module SyntaxTree
3184
3288
 
3185
3289
  alias deconstruct child_nodes
3186
3290
 
3187
- def deconstruct_keys(keys)
3291
+ def deconstruct_keys(_keys)
3188
3292
  {
3189
3293
  parent: parent,
3190
3294
  constant: constant,
@@ -3231,7 +3335,7 @@ module SyntaxTree
3231
3335
 
3232
3336
  alias deconstruct child_nodes
3233
3337
 
3234
- def deconstruct_keys(keys)
3338
+ def deconstruct_keys(_keys)
3235
3339
  {
3236
3340
  parent: parent,
3237
3341
  constant: constant,
@@ -3276,7 +3380,7 @@ module SyntaxTree
3276
3380
 
3277
3381
  alias deconstruct child_nodes
3278
3382
 
3279
- def deconstruct_keys(keys)
3383
+ def deconstruct_keys(_keys)
3280
3384
  { constant: constant, location: location, comments: comments }
3281
3385
  end
3282
3386
 
@@ -3312,7 +3416,7 @@ module SyntaxTree
3312
3416
 
3313
3417
  alias deconstruct child_nodes
3314
3418
 
3315
- def deconstruct_keys(keys)
3419
+ def deconstruct_keys(_keys)
3316
3420
  { value: value, location: location, comments: comments }
3317
3421
  end
3318
3422
 
@@ -3356,7 +3460,7 @@ module SyntaxTree
3356
3460
 
3357
3461
  alias deconstruct child_nodes
3358
3462
 
3359
- def deconstruct_keys(keys)
3463
+ def deconstruct_keys(_keys)
3360
3464
  {
3361
3465
  name: name,
3362
3466
  params: params,
@@ -3441,7 +3545,7 @@ module SyntaxTree
3441
3545
 
3442
3546
  alias deconstruct child_nodes
3443
3547
 
3444
- def deconstruct_keys(keys)
3548
+ def deconstruct_keys(_keys)
3445
3549
  {
3446
3550
  target: target,
3447
3551
  operator: operator,
@@ -3509,7 +3613,7 @@ module SyntaxTree
3509
3613
 
3510
3614
  alias deconstruct child_nodes
3511
3615
 
3512
- def deconstruct_keys(keys)
3616
+ def deconstruct_keys(_keys)
3513
3617
  { value: value, location: location, comments: comments }
3514
3618
  end
3515
3619
 
@@ -3575,7 +3679,7 @@ module SyntaxTree
3575
3679
 
3576
3680
  alias deconstruct child_nodes
3577
3681
 
3578
- def deconstruct_keys(keys)
3682
+ def deconstruct_keys(_keys)
3579
3683
  {
3580
3684
  target: target,
3581
3685
  operator: operator,
@@ -3650,7 +3754,7 @@ module SyntaxTree
3650
3754
 
3651
3755
  alias deconstruct child_nodes
3652
3756
 
3653
- def deconstruct_keys(keys)
3757
+ def deconstruct_keys(_keys)
3654
3758
  {
3655
3759
  keyword: keyword,
3656
3760
  block_var: block_var,
@@ -3730,7 +3834,7 @@ module SyntaxTree
3730
3834
 
3731
3835
  alias deconstruct child_nodes
3732
3836
 
3733
- def deconstruct_keys(keys)
3837
+ def deconstruct_keys(_keys)
3734
3838
  { left: left, right: right, location: location, comments: comments }
3735
3839
  end
3736
3840
 
@@ -3778,7 +3882,7 @@ module SyntaxTree
3778
3882
 
3779
3883
  alias deconstruct child_nodes
3780
3884
 
3781
- def deconstruct_keys(keys)
3885
+ def deconstruct_keys(_keys)
3782
3886
  { left: left, right: right, location: location, comments: comments }
3783
3887
  end
3784
3888
 
@@ -3800,7 +3904,7 @@ module SyntaxTree
3800
3904
  # quotes, then single quotes would deactivate it.)
3801
3905
  def self.locked?(node)
3802
3906
  node.parts.any? do |part|
3803
- part.is_a?(TStringContent) && part.value.match?(/#[@${]|[\\]/)
3907
+ part.is_a?(TStringContent) && part.value.match?(/\\|#[@${]/)
3804
3908
  end
3805
3909
  end
3806
3910
 
@@ -3865,7 +3969,7 @@ module SyntaxTree
3865
3969
 
3866
3970
  alias deconstruct child_nodes
3867
3971
 
3868
- def deconstruct_keys(keys)
3972
+ def deconstruct_keys(_keys)
3869
3973
  { parts: parts, quote: quote, location: location, comments: comments }
3870
3974
  end
3871
3975
 
@@ -3906,9 +4010,14 @@ module SyntaxTree
3906
4010
  matching = Quotes.matching(quote[2])
3907
4011
  pattern = /[\n#{Regexp.escape(matching)}'"]/
3908
4012
 
3909
- if parts.any? { |part|
3910
- part.is_a?(TStringContent) && part.value.match?(pattern)
3911
- }
4013
+ # This check is to ensure we don't find a matching quote inside of the
4014
+ # symbol that would be confusing.
4015
+ matched =
4016
+ parts.any? do |part|
4017
+ part.is_a?(TStringContent) && part.value.match?(pattern)
4018
+ end
4019
+
4020
+ if matched
3912
4021
  [quote, matching]
3913
4022
  elsif Quotes.locked?(self)
3914
4023
  ["#{":" unless hash_key}'", "'"]
@@ -3917,7 +4026,7 @@ module SyntaxTree
3917
4026
  end
3918
4027
  elsif Quotes.locked?(self)
3919
4028
  if quote.start_with?(":")
3920
- [hash_key ? quote[1..-1] : quote, quote[1..-1]]
4029
+ [hash_key ? quote[1..] : quote, quote[1..]]
3921
4030
  else
3922
4031
  [hash_key ? quote : ":#{quote}", quote]
3923
4032
  end
@@ -3960,7 +4069,7 @@ module SyntaxTree
3960
4069
 
3961
4070
  alias deconstruct child_nodes
3962
4071
 
3963
- def deconstruct_keys(keys)
4072
+ def deconstruct_keys(_keys)
3964
4073
  {
3965
4074
  keyword: keyword,
3966
4075
  statements: statements,
@@ -4026,7 +4135,7 @@ module SyntaxTree
4026
4135
 
4027
4136
  alias deconstruct child_nodes
4028
4137
 
4029
- def deconstruct_keys(keys)
4138
+ def deconstruct_keys(_keys)
4030
4139
  {
4031
4140
  predicate: predicate,
4032
4141
  statements: statements,
@@ -4098,7 +4207,7 @@ module SyntaxTree
4098
4207
 
4099
4208
  alias deconstruct child_nodes
4100
4209
 
4101
- def deconstruct_keys(keys)
4210
+ def deconstruct_keys(_keys)
4102
4211
  { value: value, location: location }
4103
4212
  end
4104
4213
 
@@ -4133,7 +4242,7 @@ module SyntaxTree
4133
4242
 
4134
4243
  alias deconstruct child_nodes
4135
4244
 
4136
- def deconstruct_keys(keys)
4245
+ def deconstruct_keys(_keys)
4137
4246
  { value: value, location: location }
4138
4247
  end
4139
4248
  end
@@ -4163,7 +4272,7 @@ module SyntaxTree
4163
4272
 
4164
4273
  alias deconstruct child_nodes
4165
4274
 
4166
- def deconstruct_keys(keys)
4275
+ def deconstruct_keys(_keys)
4167
4276
  { value: value, location: location }
4168
4277
  end
4169
4278
  end
@@ -4195,7 +4304,7 @@ module SyntaxTree
4195
4304
 
4196
4305
  alias deconstruct child_nodes
4197
4306
 
4198
- def deconstruct_keys(keys)
4307
+ def deconstruct_keys(_keys)
4199
4308
  { value: value, location: location }
4200
4309
  end
4201
4310
  end
@@ -4234,7 +4343,7 @@ module SyntaxTree
4234
4343
 
4235
4344
  alias deconstruct child_nodes
4236
4345
 
4237
- def deconstruct_keys(keys)
4346
+ def deconstruct_keys(_keys)
4238
4347
  {
4239
4348
  keyword: keyword,
4240
4349
  statements: statements,
@@ -4288,7 +4397,7 @@ module SyntaxTree
4288
4397
 
4289
4398
  alias deconstruct child_nodes
4290
4399
 
4291
- def deconstruct_keys(keys)
4400
+ def deconstruct_keys(_keys)
4292
4401
  { value: value, location: location, comments: comments }
4293
4402
  end
4294
4403
 
@@ -4331,7 +4440,7 @@ module SyntaxTree
4331
4440
 
4332
4441
  alias deconstruct child_nodes
4333
4442
 
4334
- def deconstruct_keys(keys)
4443
+ def deconstruct_keys(_keys)
4335
4444
  {
4336
4445
  value: value,
4337
4446
  arguments: arguments,
@@ -4343,7 +4452,8 @@ module SyntaxTree
4343
4452
  def format(q)
4344
4453
  q.format(value)
4345
4454
 
4346
- if arguments.is_a?(ArgParen) && arguments.arguments.nil? && !value.is_a?(Const)
4455
+ if arguments.is_a?(ArgParen) && arguments.arguments.nil? &&
4456
+ !value.is_a?(Const)
4347
4457
  # If you're using an explicit set of parentheses on something that looks
4348
4458
  # like a constant, then we need to match that in order to maintain valid
4349
4459
  # Ruby. For example, you could do something like Foo(), on which we
@@ -4390,7 +4500,7 @@ module SyntaxTree
4390
4500
 
4391
4501
  alias deconstruct child_nodes
4392
4502
 
4393
- def deconstruct_keys(keys)
4503
+ def deconstruct_keys(_keys)
4394
4504
  {
4395
4505
  parent: parent,
4396
4506
  operator: operator,
@@ -4436,7 +4546,7 @@ module SyntaxTree
4436
4546
 
4437
4547
  alias deconstruct child_nodes
4438
4548
 
4439
- def deconstruct_keys(keys)
4549
+ def deconstruct_keys(_keys)
4440
4550
  { value: value, location: location, comments: comments }
4441
4551
  end
4442
4552
 
@@ -4488,7 +4598,7 @@ module SyntaxTree
4488
4598
 
4489
4599
  alias deconstruct child_nodes
4490
4600
 
4491
- def deconstruct_keys(keys)
4601
+ def deconstruct_keys(_keys)
4492
4602
  {
4493
4603
  constant: constant,
4494
4604
  left: left,
@@ -4552,7 +4662,7 @@ module SyntaxTree
4552
4662
 
4553
4663
  alias deconstruct child_nodes
4554
4664
 
4555
- def deconstruct_keys(keys)
4665
+ def deconstruct_keys(_keys)
4556
4666
  {
4557
4667
  index: index,
4558
4668
  collection: collection,
@@ -4609,7 +4719,7 @@ module SyntaxTree
4609
4719
 
4610
4720
  alias deconstruct child_nodes
4611
4721
 
4612
- def deconstruct_keys(keys)
4722
+ def deconstruct_keys(_keys)
4613
4723
  { value: value, location: location, comments: comments }
4614
4724
  end
4615
4725
 
@@ -4623,6 +4733,9 @@ module SyntaxTree
4623
4733
  # { key => value }
4624
4734
  #
4625
4735
  class HashLiteral < Node
4736
+ # This is a special formatter used if the hash literal contains no values
4737
+ # but _does_ contain comments. In this case we do some special formatting to
4738
+ # make sure the comments gets indented properly.
4626
4739
  class EmptyWithCommentsFormatter
4627
4740
  # [LBrace] the opening brace
4628
4741
  attr_reader :lbrace
@@ -4672,7 +4785,7 @@ module SyntaxTree
4672
4785
 
4673
4786
  alias deconstruct child_nodes
4674
4787
 
4675
- def deconstruct_keys(keys)
4788
+ def deconstruct_keys(_keys)
4676
4789
  { lbrace: lbrace, assocs: assocs, location: location, comments: comments }
4677
4790
  end
4678
4791
 
@@ -4741,7 +4854,14 @@ module SyntaxTree
4741
4854
  # [Array[ Comment | EmbDoc ]] the comments attached to this node
4742
4855
  attr_reader :comments
4743
4856
 
4744
- def initialize(beginning:, ending: nil, dedent: 0, parts: [], location:, comments: [])
4857
+ def initialize(
4858
+ beginning:,
4859
+ ending: nil,
4860
+ dedent: 0,
4861
+ parts: [],
4862
+ location:,
4863
+ comments: []
4864
+ )
4745
4865
  @beginning = beginning
4746
4866
  @ending = ending
4747
4867
  @dedent = dedent
@@ -4760,7 +4880,7 @@ module SyntaxTree
4760
4880
 
4761
4881
  alias deconstruct child_nodes
4762
4882
 
4763
- def deconstruct_keys(keys)
4883
+ def deconstruct_keys(_keys)
4764
4884
  {
4765
4885
  beginning: beginning,
4766
4886
  location: location,
@@ -4775,8 +4895,12 @@ module SyntaxTree
4775
4895
  # prettyprint module. It's when you want to force a newline, but don't
4776
4896
  # want to force the break parent.
4777
4897
  breakable = -> do
4778
- q.target <<
4779
- PrettyPrint::Breakable.new(" ", 1, indent: false, force: true)
4898
+ q.target << PrettyPrint::Breakable.new(
4899
+ " ",
4900
+ 1,
4901
+ indent: false,
4902
+ force: true
4903
+ )
4780
4904
  end
4781
4905
 
4782
4906
  q.group do
@@ -4832,7 +4956,7 @@ module SyntaxTree
4832
4956
 
4833
4957
  alias deconstruct child_nodes
4834
4958
 
4835
- def deconstruct_keys(keys)
4959
+ def deconstruct_keys(_keys)
4836
4960
  { value: value, location: location, comments: comments }
4837
4961
  end
4838
4962
 
@@ -4849,6 +4973,7 @@ module SyntaxTree
4849
4973
  # end
4850
4974
  #
4851
4975
  class HshPtn < Node
4976
+ # Formats a key-value pair in a hash pattern. The value is optional.
4852
4977
  class KeywordFormatter
4853
4978
  # [Label] the keyword being used
4854
4979
  attr_reader :key
@@ -4875,6 +5000,7 @@ module SyntaxTree
4875
5000
  end
4876
5001
  end
4877
5002
 
5003
+ # Formats the optional double-splat from the pattern.
4878
5004
  class KeywordRestFormatter
4879
5005
  # [VarField] the parameter that matches the remaining keywords
4880
5006
  attr_reader :keyword_rest
@@ -4924,7 +5050,7 @@ module SyntaxTree
4924
5050
 
4925
5051
  alias deconstruct child_nodes
4926
5052
 
4927
- def deconstruct_keys(keys)
5053
+ def deconstruct_keys(_keys)
4928
5054
  {
4929
5055
  constant: constant,
4930
5056
  keywords: keywords,
@@ -4947,27 +5073,52 @@ module SyntaxTree
4947
5073
  q.text(" then") if !constant && keyword_rest && keyword_rest.value.nil?
4948
5074
  end
4949
5075
 
5076
+ # If there is a constant, we're going to format to have the constant name
5077
+ # first and then use brackets.
4950
5078
  if constant
4951
- q.format(constant)
4952
- q.group(0, "[", "]", &contents)
5079
+ q.group do
5080
+ q.format(constant)
5081
+ q.text("[")
5082
+ q.indent do
5083
+ q.breakable("")
5084
+ contents.call
5085
+ end
5086
+ q.breakable("")
5087
+ q.text("]")
5088
+ end
4953
5089
  return
4954
5090
  end
4955
5091
 
5092
+ # If there's nothing at all, then we're going to use empty braces.
4956
5093
  if parts.empty?
4957
5094
  q.text("{}")
4958
- elsif PATTERNS.include?(q.parent.class)
4959
- q.text("{ ")
4960
- contents.call
4961
- q.text(" }")
4962
- else
5095
+ return
5096
+ end
5097
+
5098
+ # If there's only one pair, then we'll just print the contents provided
5099
+ # we're not inside another pattern.
5100
+ if !PATTERNS.include?(q.parent.class) && parts.size == 1
4963
5101
  contents.call
5102
+ return
5103
+ end
5104
+
5105
+ # Otherwise, we're going to always use braces to make it clear it's a hash
5106
+ # pattern.
5107
+ q.group do
5108
+ q.text("{")
5109
+ q.indent do
5110
+ q.breakable
5111
+ contents.call
5112
+ end
5113
+ q.breakable
5114
+ q.text("}")
4964
5115
  end
4965
5116
  end
4966
5117
  end
4967
5118
 
4968
5119
  # The list of nodes that represent patterns inside of pattern matching so that
4969
5120
  # when a pattern is being printed it knows if it's nested.
4970
- PATTERNS = [AryPtn, Binary, FndPtn, HshPtn, RAssign]
5121
+ PATTERNS = [AryPtn, Binary, FndPtn, HshPtn, RAssign].freeze
4971
5122
 
4972
5123
  # Ident represents an identifier anywhere in code. It can represent a very
4973
5124
  # large number of things, depending on where it is in the syntax tree.
@@ -4997,7 +5148,7 @@ module SyntaxTree
4997
5148
 
4998
5149
  alias deconstruct child_nodes
4999
5150
 
5000
- def deconstruct_keys(keys)
5151
+ def deconstruct_keys(_keys)
5001
5152
  { value: value, location: location, comments: comments }
5002
5153
  end
5003
5154
 
@@ -5014,7 +5165,7 @@ module SyntaxTree
5014
5165
  def self.call(parent)
5015
5166
  queue = [parent]
5016
5167
 
5017
- while node = queue.shift
5168
+ while (node = queue.shift)
5018
5169
  return true if [Assign, MAssign, OpAssign].include?(node.class)
5019
5170
  queue += node.child_nodes
5020
5171
  end
@@ -5041,9 +5192,12 @@ module SyntaxTree
5041
5192
  else
5042
5193
  # Otherwise, we're going to check the conditional for certain cases.
5043
5194
  case node
5044
- in { predicate: Assign | Command | CommandCall | MAssign | OpAssign }
5195
+ in predicate: Assign | Command | CommandCall | MAssign | OpAssign
5045
5196
  false
5046
- in { statements: { body: [truthy] }, consequent: Else[statements: { body: [falsy] }] }
5197
+ in {
5198
+ statements: { body: [truthy] },
5199
+ consequent: Else[statements: { body: [falsy] }]
5200
+ }
5047
5201
  ternaryable?(truthy) && ternaryable?(falsy)
5048
5202
  else
5049
5203
  false
@@ -5054,8 +5208,8 @@ module SyntaxTree
5054
5208
  private
5055
5209
 
5056
5210
  # 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.
5211
+ # parentheses around them. In this case we say they cannot be ternaried
5212
+ # and default instead to breaking them into multiple lines.
5059
5213
  def ternaryable?(statement)
5060
5214
  # This is a list of nodes that should not be allowed to be a part of a
5061
5215
  # ternary clause.
@@ -5113,13 +5267,15 @@ module SyntaxTree
5113
5267
  q.group { format_break(q, force: true) }
5114
5268
  else
5115
5269
  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)
5270
+ q
5271
+ .if_break { format_break(q, force: false) }
5272
+ .if_flat do
5273
+ Parentheses.flat(q) do
5274
+ q.format(node.statements)
5275
+ q.text(" #{keyword} ")
5276
+ q.format(node.predicate)
5277
+ end
5121
5278
  end
5122
- end
5123
5279
  end
5124
5280
  end
5125
5281
  end
@@ -5148,51 +5304,53 @@ module SyntaxTree
5148
5304
 
5149
5305
  def format_ternary(q)
5150
5306
  q.group do
5151
- q.if_break do
5152
- q.text("#{keyword} ")
5153
- q.nest(keyword.length + 1) { q.format(node.predicate) }
5154
-
5155
- q.indent do
5156
- q.breakable
5157
- q.format(node.statements)
5158
- end
5307
+ q
5308
+ .if_break do
5309
+ q.text("#{keyword} ")
5310
+ q.nest(keyword.length + 1) { q.format(node.predicate) }
5159
5311
 
5160
- q.breakable
5161
- q.group do
5162
- q.format(node.consequent.keyword)
5163
5312
  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)
5313
+ q.breakable
5314
+ q.format(node.statements)
5172
5315
  end
5173
- end
5174
5316
 
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(" ? ")
5317
+ q.breakable
5318
+ q.group do
5319
+ q.format(node.consequent.keyword)
5320
+ q.indent do
5321
+ # This is a very special case of breakable where we want to
5322
+ # force it into the output but we _don't_ want to explicitly
5323
+ # break the parent. If a break-parent shows up in the tree, then
5324
+ # it's going to force it all the way up to the tree, which is
5325
+ # going to negate the ternary. Maybe this should be an option in
5326
+ # prettyprint? As in force: :no_break_parent or something.
5327
+ q.target << PrettyPrint::Breakable.new(" ", 1, force: true)
5328
+ q.format(node.consequent.statements)
5329
+ end
5330
+ end
5331
+
5332
+ q.breakable
5333
+ q.text("end")
5334
+ end
5335
+ .if_flat do
5336
+ Parentheses.flat(q) do
5337
+ q.format(node.predicate)
5338
+ q.text(" ? ")
5181
5339
 
5182
- statements = [node.statements, node.consequent.statements]
5183
- statements.reverse! if keyword == "unless"
5340
+ statements = [node.statements, node.consequent.statements]
5341
+ statements.reverse! if keyword == "unless"
5184
5342
 
5185
- q.format(statements[0])
5186
- q.text(" : ")
5187
- q.format(statements[1])
5343
+ q.format(statements[0])
5344
+ q.text(" : ")
5345
+ q.format(statements[1])
5346
+ end
5188
5347
  end
5189
- end
5190
5348
  end
5191
5349
  end
5192
5350
 
5193
5351
  def contains_conditional?
5194
5352
  case node
5195
- in { statements: { body: [If | IfMod | IfOp | Unless | UnlessMod] } }
5353
+ in statements: { body: [If | IfMod | IfOp | Unless | UnlessMod] }
5196
5354
  true
5197
5355
  else
5198
5356
  false
@@ -5242,7 +5400,7 @@ module SyntaxTree
5242
5400
 
5243
5401
  alias deconstruct child_nodes
5244
5402
 
5245
- def deconstruct_keys(keys)
5403
+ def deconstruct_keys(_keys)
5246
5404
  {
5247
5405
  predicate: predicate,
5248
5406
  statements: statements,
@@ -5292,7 +5450,7 @@ module SyntaxTree
5292
5450
 
5293
5451
  alias deconstruct child_nodes
5294
5452
 
5295
- def deconstruct_keys(keys)
5453
+ def deconstruct_keys(_keys)
5296
5454
  {
5297
5455
  predicate: predicate,
5298
5456
  truthy: truthy,
@@ -5310,7 +5468,8 @@ module SyntaxTree
5310
5468
  Yield0, ZSuper
5311
5469
  ]
5312
5470
 
5313
- if q.parent.is_a?(Paren) || force_flat.include?(truthy.class) || force_flat.include?(falsy.class)
5471
+ if q.parent.is_a?(Paren) || force_flat.include?(truthy.class) ||
5472
+ force_flat.include?(falsy.class)
5314
5473
  q.group { format_flat(q) }
5315
5474
  return
5316
5475
  end
@@ -5430,7 +5589,7 @@ module SyntaxTree
5430
5589
 
5431
5590
  alias deconstruct child_nodes
5432
5591
 
5433
- def deconstruct_keys(keys)
5592
+ def deconstruct_keys(_keys)
5434
5593
  {
5435
5594
  statement: statement,
5436
5595
  predicate: predicate,
@@ -5471,7 +5630,7 @@ module SyntaxTree
5471
5630
 
5472
5631
  alias deconstruct child_nodes
5473
5632
 
5474
- def deconstruct_keys(keys)
5633
+ def deconstruct_keys(_keys)
5475
5634
  { value: value, location: location, comments: comments }
5476
5635
  end
5477
5636
 
@@ -5518,7 +5677,7 @@ module SyntaxTree
5518
5677
 
5519
5678
  alias deconstruct child_nodes
5520
5679
 
5521
- def deconstruct_keys(keys)
5680
+ def deconstruct_keys(_keys)
5522
5681
  {
5523
5682
  pattern: pattern,
5524
5683
  statements: statements,
@@ -5577,7 +5736,7 @@ module SyntaxTree
5577
5736
 
5578
5737
  alias deconstruct child_nodes
5579
5738
 
5580
- def deconstruct_keys(keys)
5739
+ def deconstruct_keys(_keys)
5581
5740
  { value: value, location: location, comments: comments }
5582
5741
  end
5583
5742
 
@@ -5587,7 +5746,7 @@ module SyntaxTree
5587
5746
  # the values, then we're going to insert them every 3 characters
5588
5747
  # starting from the right.
5589
5748
  index = (value.length + 2) % 3
5590
- q.text(" #{value}"[index..-1].scan(/.../).join("_").strip)
5749
+ q.text(" #{value}"[index..].scan(/.../).join("_").strip)
5591
5750
  else
5592
5751
  q.text(value)
5593
5752
  end
@@ -5621,7 +5780,7 @@ module SyntaxTree
5621
5780
 
5622
5781
  alias deconstruct child_nodes
5623
5782
 
5624
- def deconstruct_keys(keys)
5783
+ def deconstruct_keys(_keys)
5625
5784
  { value: value, location: location, comments: comments }
5626
5785
  end
5627
5786
 
@@ -5666,7 +5825,7 @@ module SyntaxTree
5666
5825
 
5667
5826
  alias deconstruct child_nodes
5668
5827
 
5669
- def deconstruct_keys(keys)
5828
+ def deconstruct_keys(_keys)
5670
5829
  { value: value, location: location, comments: comments }
5671
5830
  end
5672
5831
 
@@ -5703,7 +5862,7 @@ module SyntaxTree
5703
5862
 
5704
5863
  alias deconstruct child_nodes
5705
5864
 
5706
- def deconstruct_keys(keys)
5865
+ def deconstruct_keys(_keys)
5707
5866
  { name: name, location: location, comments: comments }
5708
5867
  end
5709
5868
 
@@ -5749,7 +5908,7 @@ module SyntaxTree
5749
5908
 
5750
5909
  alias deconstruct child_nodes
5751
5910
 
5752
- def deconstruct_keys(keys)
5911
+ def deconstruct_keys(_keys)
5753
5912
  { value: value, location: location, comments: comments }
5754
5913
  end
5755
5914
 
@@ -5784,7 +5943,7 @@ module SyntaxTree
5784
5943
 
5785
5944
  alias deconstruct child_nodes
5786
5945
 
5787
- def deconstruct_keys(keys)
5946
+ def deconstruct_keys(_keys)
5788
5947
  { value: value, location: location }
5789
5948
  end
5790
5949
  end
@@ -5820,7 +5979,7 @@ module SyntaxTree
5820
5979
 
5821
5980
  alias deconstruct child_nodes
5822
5981
 
5823
- def deconstruct_keys(keys)
5982
+ def deconstruct_keys(_keys)
5824
5983
  {
5825
5984
  params: params,
5826
5985
  statements: statements,
@@ -5842,25 +6001,27 @@ module SyntaxTree
5842
6001
  end
5843
6002
 
5844
6003
  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)
6004
+ q
6005
+ .if_break do
6006
+ force_parens =
6007
+ q.parents.any? do |node|
6008
+ node.is_a?(Command) || node.is_a?(CommandCall)
6009
+ end
6010
+
6011
+ q.text(force_parens ? "{" : "do")
6012
+ q.indent do
6013
+ q.breakable
6014
+ q.format(statements)
5849
6015
  end
5850
6016
 
5851
- q.text(force_parens ? "{" : "do")
5852
- q.indent do
5853
6017
  q.breakable
6018
+ q.text(force_parens ? "}" : "end")
6019
+ end
6020
+ .if_flat do
6021
+ q.text("{ ")
5854
6022
  q.format(statements)
6023
+ q.text(" }")
5855
6024
  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
6025
  end
5865
6026
  end
5866
6027
  end
@@ -5889,7 +6050,7 @@ module SyntaxTree
5889
6050
 
5890
6051
  alias deconstruct child_nodes
5891
6052
 
5892
- def deconstruct_keys(keys)
6053
+ def deconstruct_keys(_keys)
5893
6054
  { value: value, location: location, comments: comments }
5894
6055
  end
5895
6056
 
@@ -5922,7 +6083,7 @@ module SyntaxTree
5922
6083
 
5923
6084
  alias deconstruct child_nodes
5924
6085
 
5925
- def deconstruct_keys(keys)
6086
+ def deconstruct_keys(_keys)
5926
6087
  { value: value, location: location, comments: comments }
5927
6088
  end
5928
6089
 
@@ -5955,7 +6116,7 @@ module SyntaxTree
5955
6116
 
5956
6117
  alias deconstruct child_nodes
5957
6118
 
5958
- def deconstruct_keys(keys)
6119
+ def deconstruct_keys(_keys)
5959
6120
  { value: value, location: location, comments: comments }
5960
6121
  end
5961
6122
 
@@ -6005,7 +6166,7 @@ module SyntaxTree
6005
6166
 
6006
6167
  alias deconstruct child_nodes
6007
6168
 
6008
- def deconstruct_keys(keys)
6169
+ def deconstruct_keys(_keys)
6009
6170
  { target: target, value: value, location: location, comments: comments }
6010
6171
  end
6011
6172
 
@@ -6052,7 +6213,7 @@ module SyntaxTree
6052
6213
 
6053
6214
  alias deconstruct child_nodes
6054
6215
 
6055
- def deconstruct_keys(keys)
6216
+ def deconstruct_keys(_keys)
6056
6217
  { call: call, block: block, location: location, comments: comments }
6057
6218
  end
6058
6219
 
@@ -6060,8 +6221,13 @@ module SyntaxTree
6060
6221
  # If we're at the top of a call chain, then we're going to do some
6061
6222
  # specialized printing in case we can print it nicely. We _only_ do this
6062
6223
  # 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) } }
6224
+ if !CallChainFormatter.chained?(q.parent) &&
6225
+ CallChainFormatter.chained?(call)
6226
+ q.group do
6227
+ q
6228
+ .if_break { CallChainFormatter.new(self).format(q) }
6229
+ .if_flat { format_contents(q) }
6230
+ end
6065
6231
  else
6066
6232
  format_contents(q)
6067
6233
  end
@@ -6108,7 +6274,7 @@ module SyntaxTree
6108
6274
 
6109
6275
  alias deconstruct child_nodes
6110
6276
 
6111
- def deconstruct_keys(keys)
6277
+ def deconstruct_keys(_keys)
6112
6278
  { parts: parts, location: location, comma: comma, comments: comments }
6113
6279
  end
6114
6280
 
@@ -6152,7 +6318,7 @@ module SyntaxTree
6152
6318
 
6153
6319
  alias deconstruct child_nodes
6154
6320
 
6155
- def deconstruct_keys(keys)
6321
+ def deconstruct_keys(_keys)
6156
6322
  { contents: contents, location: location, comments: comments }
6157
6323
  end
6158
6324
 
@@ -6208,7 +6374,7 @@ module SyntaxTree
6208
6374
 
6209
6375
  alias deconstruct child_nodes
6210
6376
 
6211
- def deconstruct_keys(keys)
6377
+ def deconstruct_keys(_keys)
6212
6378
  {
6213
6379
  constant: constant,
6214
6380
  bodystmt: bodystmt,
@@ -6275,7 +6441,7 @@ module SyntaxTree
6275
6441
 
6276
6442
  alias deconstruct child_nodes
6277
6443
 
6278
- def deconstruct_keys(keys)
6444
+ def deconstruct_keys(_keys)
6279
6445
  { parts: parts, location: location, comments: comments }
6280
6446
  end
6281
6447
 
@@ -6324,7 +6490,7 @@ module SyntaxTree
6324
6490
 
6325
6491
  alias deconstruct child_nodes
6326
6492
 
6327
- def deconstruct_keys(keys)
6493
+ def deconstruct_keys(_keys)
6328
6494
  { arguments: arguments, location: location, comments: comments }
6329
6495
  end
6330
6496
 
@@ -6361,7 +6527,7 @@ module SyntaxTree
6361
6527
 
6362
6528
  alias deconstruct child_nodes
6363
6529
 
6364
- def deconstruct_keys(keys)
6530
+ def deconstruct_keys(_keys)
6365
6531
  { value: value, location: location, comments: comments }
6366
6532
  end
6367
6533
 
@@ -6407,7 +6573,7 @@ module SyntaxTree
6407
6573
 
6408
6574
  alias deconstruct child_nodes
6409
6575
 
6410
- def deconstruct_keys(keys)
6576
+ def deconstruct_keys(_keys)
6411
6577
  {
6412
6578
  target: target,
6413
6579
  operator: operator,
@@ -6475,7 +6641,16 @@ module SyntaxTree
6475
6641
  # This approach maintains the nice conciseness of the inline version, while
6476
6642
  # keeping the correct semantic meaning.
6477
6643
  module Parentheses
6478
- NODES = [Args, Assign, Assoc, Binary, Call, Defined, MAssign, OpAssign]
6644
+ NODES = [
6645
+ Args,
6646
+ Assign,
6647
+ Assoc,
6648
+ Binary,
6649
+ Call,
6650
+ Defined,
6651
+ MAssign,
6652
+ OpAssign
6653
+ ].freeze
6479
6654
 
6480
6655
  def self.flat(q)
6481
6656
  return yield unless NODES.include?(q.parent.class)
@@ -6507,6 +6682,8 @@ module SyntaxTree
6507
6682
  # def method(param) end
6508
6683
  #
6509
6684
  class Params < Node
6685
+ # Formats the optional position of the parameters. This includes the label,
6686
+ # as well as the default value.
6510
6687
  class OptionalFormatter
6511
6688
  # [Ident] the name of the parameter
6512
6689
  attr_reader :name
@@ -6530,6 +6707,8 @@ module SyntaxTree
6530
6707
  end
6531
6708
  end
6532
6709
 
6710
+ # Formats the keyword position of the parameters. This includes the label,
6711
+ # as well as an optional default value.
6533
6712
  class KeywordFormatter
6534
6713
  # [Ident] the name of the parameter
6535
6714
  attr_reader :name
@@ -6556,6 +6735,8 @@ module SyntaxTree
6556
6735
  end
6557
6736
  end
6558
6737
 
6738
+ # Formats the keyword_rest position of the parameters. This can be the **nil
6739
+ # syntax, the ... syntax, or the ** syntax.
6559
6740
  class KeywordRestFormatter
6560
6741
  # [:nil | ArgsForward | KwRestParam] the value of the parameter
6561
6742
  attr_reader :value
@@ -6569,11 +6750,7 @@ module SyntaxTree
6569
6750
  end
6570
6751
 
6571
6752
  def format(q)
6572
- if value == :nil
6573
- q.text("**nil")
6574
- else
6575
- q.format(value)
6576
- end
6753
+ value == :nil ? q.text("**nil") : q.format(value)
6577
6754
  end
6578
6755
  end
6579
6756
 
@@ -6654,7 +6831,7 @@ module SyntaxTree
6654
6831
 
6655
6832
  alias deconstruct child_nodes
6656
6833
 
6657
- def deconstruct_keys(keys)
6834
+ def deconstruct_keys(_keys)
6658
6835
  {
6659
6836
  location: location,
6660
6837
  requireds: requireds,
@@ -6675,18 +6852,17 @@ module SyntaxTree
6675
6852
  ]
6676
6853
 
6677
6854
  parts << rest if rest && !rest.is_a?(ExcessedComma)
6678
- parts +=
6679
- [
6680
- *posts,
6681
- *keywords.map { |(name, value)| KeywordFormatter.new(name, value) }
6682
- ]
6855
+ parts += [
6856
+ *posts,
6857
+ *keywords.map { |(name, value)| KeywordFormatter.new(name, value) }
6858
+ ]
6683
6859
 
6684
6860
  parts << KeywordRestFormatter.new(keyword_rest) if keyword_rest
6685
6861
  parts << block if block
6686
6862
 
6687
6863
  contents = -> do
6688
6864
  q.seplist(parts) { |part| q.format(part) }
6689
- q.format(rest) if rest && rest.is_a?(ExcessedComma)
6865
+ q.format(rest) if rest.is_a?(ExcessedComma)
6690
6866
  end
6691
6867
 
6692
6868
  if ![Def, Defs, DefEndless].include?(q.parent.class) || parts.empty?
@@ -6736,7 +6912,7 @@ module SyntaxTree
6736
6912
 
6737
6913
  alias deconstruct child_nodes
6738
6914
 
6739
- def deconstruct_keys(keys)
6915
+ def deconstruct_keys(_keys)
6740
6916
  {
6741
6917
  lparen: lparen,
6742
6918
  contents: contents,
@@ -6787,7 +6963,7 @@ module SyntaxTree
6787
6963
 
6788
6964
  alias deconstruct child_nodes
6789
6965
 
6790
- def deconstruct_keys(keys)
6966
+ def deconstruct_keys(_keys)
6791
6967
  { value: value, location: location, comments: comments }
6792
6968
  end
6793
6969
 
@@ -6820,7 +6996,7 @@ module SyntaxTree
6820
6996
 
6821
6997
  alias deconstruct child_nodes
6822
6998
 
6823
- def deconstruct_keys(keys)
6999
+ def deconstruct_keys(_keys)
6824
7000
  { statements: statements, location: location, comments: comments }
6825
7001
  end
6826
7002
 
@@ -6865,7 +7041,7 @@ module SyntaxTree
6865
7041
 
6866
7042
  alias deconstruct child_nodes
6867
7043
 
6868
- def deconstruct_keys(keys)
7044
+ def deconstruct_keys(_keys)
6869
7045
  {
6870
7046
  beginning: beginning,
6871
7047
  elements: elements,
@@ -6920,18 +7096,9 @@ module SyntaxTree
6920
7096
 
6921
7097
  alias deconstruct child_nodes
6922
7098
 
6923
- def deconstruct_keys(keys)
7099
+ def deconstruct_keys(_keys)
6924
7100
  { value: value, location: location }
6925
7101
  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
7102
  end
6936
7103
 
6937
7104
  # QWords represents a string literal array without interpolation.
@@ -6965,7 +7132,7 @@ module SyntaxTree
6965
7132
 
6966
7133
  alias deconstruct child_nodes
6967
7134
 
6968
- def deconstruct_keys(keys)
7135
+ def deconstruct_keys(_keys)
6969
7136
  {
6970
7137
  beginning: beginning,
6971
7138
  elements: elements,
@@ -7020,7 +7187,7 @@ module SyntaxTree
7020
7187
 
7021
7188
  alias deconstruct child_nodes
7022
7189
 
7023
- def deconstruct_keys(keys)
7190
+ def deconstruct_keys(_keys)
7024
7191
  { value: value, location: location }
7025
7192
  end
7026
7193
  end
@@ -7052,7 +7219,7 @@ module SyntaxTree
7052
7219
 
7053
7220
  alias deconstruct child_nodes
7054
7221
 
7055
- def deconstruct_keys(keys)
7222
+ def deconstruct_keys(_keys)
7056
7223
  { value: value, location: location, comments: comments }
7057
7224
  end
7058
7225
 
@@ -7081,7 +7248,7 @@ module SyntaxTree
7081
7248
 
7082
7249
  alias deconstruct child_nodes
7083
7250
 
7084
- def deconstruct_keys(keys)
7251
+ def deconstruct_keys(_keys)
7085
7252
  { value: value, location: location }
7086
7253
  end
7087
7254
  end
@@ -7106,7 +7273,7 @@ module SyntaxTree
7106
7273
 
7107
7274
  alias deconstruct child_nodes
7108
7275
 
7109
- def deconstruct_keys(keys)
7276
+ def deconstruct_keys(_keys)
7110
7277
  { value: value, location: location }
7111
7278
  end
7112
7279
  end
@@ -7138,7 +7305,7 @@ module SyntaxTree
7138
7305
 
7139
7306
  alias deconstruct child_nodes
7140
7307
 
7141
- def deconstruct_keys(keys)
7308
+ def deconstruct_keys(_keys)
7142
7309
  { value: value, location: location, comments: comments }
7143
7310
  end
7144
7311
 
@@ -7177,7 +7344,7 @@ module SyntaxTree
7177
7344
 
7178
7345
  alias deconstruct child_nodes
7179
7346
 
7180
- def deconstruct_keys(keys)
7347
+ def deconstruct_keys(_keys)
7181
7348
  { beginning: beginning, parts: parts, location: location }
7182
7349
  end
7183
7350
  end
@@ -7210,7 +7377,7 @@ module SyntaxTree
7210
7377
 
7211
7378
  alias deconstruct child_nodes
7212
7379
 
7213
- def deconstruct_keys(keys)
7380
+ def deconstruct_keys(_keys)
7214
7381
  { value: value, location: location }
7215
7382
  end
7216
7383
  end
@@ -7244,7 +7411,7 @@ module SyntaxTree
7244
7411
 
7245
7412
  alias deconstruct child_nodes
7246
7413
 
7247
- def deconstruct_keys(keys)
7414
+ def deconstruct_keys(_keys)
7248
7415
  { value: value, location: location }
7249
7416
  end
7250
7417
  end
@@ -7285,10 +7452,11 @@ module SyntaxTree
7285
7452
 
7286
7453
  alias deconstruct child_nodes
7287
7454
 
7288
- def deconstruct_keys(keys)
7455
+ def deconstruct_keys(_keys)
7289
7456
  {
7290
7457
  beginning: beginning,
7291
7458
  ending: ending,
7459
+ options: options,
7292
7460
  parts: parts,
7293
7461
  location: location,
7294
7462
  comments: comments
@@ -7296,7 +7464,7 @@ module SyntaxTree
7296
7464
  end
7297
7465
 
7298
7466
  def format(q)
7299
- braces = ambiguous?(q) || include?(%r{\/})
7467
+ braces = ambiguous?(q) || include?(%r{/})
7300
7468
 
7301
7469
  if braces && include?(/[{}]/)
7302
7470
  q.group do
@@ -7323,18 +7491,22 @@ module SyntaxTree
7323
7491
  end
7324
7492
 
7325
7493
  q.text("}")
7326
- q.text(ending[1..-1])
7494
+ q.text(options)
7327
7495
  end
7328
7496
  else
7329
7497
  q.group do
7330
7498
  q.text("/")
7331
7499
  q.format_each(parts)
7332
7500
  q.text("/")
7333
- q.text(ending[1..-1])
7501
+ q.text(options)
7334
7502
  end
7335
7503
  end
7336
7504
  end
7337
7505
 
7506
+ def options
7507
+ ending[1..]
7508
+ end
7509
+
7338
7510
  private
7339
7511
 
7340
7512
  def include?(pattern)
@@ -7390,7 +7562,7 @@ module SyntaxTree
7390
7562
 
7391
7563
  alias deconstruct child_nodes
7392
7564
 
7393
- def deconstruct_keys(keys)
7565
+ def deconstruct_keys(_keys)
7394
7566
  {
7395
7567
  exceptions: exceptions,
7396
7568
  variable: variable,
@@ -7465,7 +7637,10 @@ module SyntaxTree
7465
7637
 
7466
7638
  if consequent
7467
7639
  consequent.bind_end(end_char, end_column)
7468
- statements.bind_end(consequent.location.start_char, consequent.location.start_column)
7640
+ statements.bind_end(
7641
+ consequent.location.start_char,
7642
+ consequent.location.start_column
7643
+ )
7469
7644
  else
7470
7645
  statements.bind_end(end_char, end_column)
7471
7646
  end
@@ -7481,7 +7656,7 @@ module SyntaxTree
7481
7656
 
7482
7657
  alias deconstruct child_nodes
7483
7658
 
7484
- def deconstruct_keys(keys)
7659
+ def deconstruct_keys(_keys)
7485
7660
  {
7486
7661
  keyword: keyword,
7487
7662
  exception: exception,
@@ -7548,7 +7723,7 @@ module SyntaxTree
7548
7723
 
7549
7724
  alias deconstruct child_nodes
7550
7725
 
7551
- def deconstruct_keys(keys)
7726
+ def deconstruct_keys(_keys)
7552
7727
  {
7553
7728
  statement: statement,
7554
7729
  value: value,
@@ -7602,7 +7777,7 @@ module SyntaxTree
7602
7777
 
7603
7778
  alias deconstruct child_nodes
7604
7779
 
7605
- def deconstruct_keys(keys)
7780
+ def deconstruct_keys(_keys)
7606
7781
  { name: name, location: location, comments: comments }
7607
7782
  end
7608
7783
 
@@ -7639,7 +7814,7 @@ module SyntaxTree
7639
7814
 
7640
7815
  alias deconstruct child_nodes
7641
7816
 
7642
- def deconstruct_keys(keys)
7817
+ def deconstruct_keys(_keys)
7643
7818
  { value: value, location: location, comments: comments }
7644
7819
  end
7645
7820
 
@@ -7675,7 +7850,7 @@ module SyntaxTree
7675
7850
 
7676
7851
  alias deconstruct child_nodes
7677
7852
 
7678
- def deconstruct_keys(keys)
7853
+ def deconstruct_keys(_keys)
7679
7854
  { arguments: arguments, location: location, comments: comments }
7680
7855
  end
7681
7856
 
@@ -7711,7 +7886,7 @@ module SyntaxTree
7711
7886
 
7712
7887
  alias deconstruct child_nodes
7713
7888
 
7714
- def deconstruct_keys(keys)
7889
+ def deconstruct_keys(_keys)
7715
7890
  { value: value, location: location, comments: comments }
7716
7891
  end
7717
7892
 
@@ -7740,7 +7915,7 @@ module SyntaxTree
7740
7915
 
7741
7916
  alias deconstruct child_nodes
7742
7917
 
7743
- def deconstruct_keys(keys)
7918
+ def deconstruct_keys(_keys)
7744
7919
  { value: value, location: location }
7745
7920
  end
7746
7921
  end
@@ -7779,7 +7954,7 @@ module SyntaxTree
7779
7954
 
7780
7955
  alias deconstruct child_nodes
7781
7956
 
7782
- def deconstruct_keys(keys)
7957
+ def deconstruct_keys(_keys)
7783
7958
  {
7784
7959
  target: target,
7785
7960
  bodystmt: bodystmt,
@@ -7881,7 +8056,7 @@ module SyntaxTree
7881
8056
 
7882
8057
  alias deconstruct child_nodes
7883
8058
 
7884
- def deconstruct_keys(keys)
8059
+ def deconstruct_keys(_keys)
7885
8060
  { parser: parser, body: body, location: location, comments: comments }
7886
8061
  end
7887
8062
 
@@ -7951,12 +8126,19 @@ module SyntaxTree
7951
8126
  comment = parser_comments[comment_index]
7952
8127
  location = comment.location
7953
8128
 
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)
8129
+ if !comment.inline? && (start_char <= location.start_char) &&
8130
+ (end_char >= location.end_char) && !comment.ignore?
8131
+ while (node = body[body_index]) &&
8132
+ (
8133
+ node.is_a?(VoidStmt) ||
8134
+ node.location.start_char < location.start_char
8135
+ )
7956
8136
  body_index += 1
7957
8137
  end
7958
8138
 
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
8139
+ if body_index != 0 &&
8140
+ body[body_index - 1].location.start_char < location.start_char &&
8141
+ body[body_index - 1].location.end_char > location.start_char
7960
8142
  # The previous node entirely encapsules the comment, so we don't
7961
8143
  # want to attach it here since it will get attached normally. This
7962
8144
  # is mostly in the case of hash and array literals.
@@ -7996,7 +8178,7 @@ module SyntaxTree
7996
8178
 
7997
8179
  alias deconstruct child_nodes
7998
8180
 
7999
- def deconstruct_keys(keys)
8181
+ def deconstruct_keys(_keys)
8000
8182
  { parts: parts, location: location }
8001
8183
  end
8002
8184
  end
@@ -8034,14 +8216,14 @@ module SyntaxTree
8034
8216
 
8035
8217
  alias deconstruct child_nodes
8036
8218
 
8037
- def deconstruct_keys(keys)
8219
+ def deconstruct_keys(_keys)
8038
8220
  { left: left, right: right, location: location, comments: comments }
8039
8221
  end
8040
8222
 
8041
8223
  def format(q)
8042
8224
  q.group do
8043
8225
  q.format(left)
8044
- q.text(' \\')
8226
+ q.text(" \\")
8045
8227
  q.indent do
8046
8228
  q.breakable(force: true)
8047
8229
  q.format(right)
@@ -8079,7 +8261,7 @@ module SyntaxTree
8079
8261
 
8080
8262
  alias deconstruct child_nodes
8081
8263
 
8082
- def deconstruct_keys(keys)
8264
+ def deconstruct_keys(_keys)
8083
8265
  { variable: variable, location: location, comments: comments }
8084
8266
  end
8085
8267
 
@@ -8119,7 +8301,7 @@ module SyntaxTree
8119
8301
 
8120
8302
  alias deconstruct child_nodes
8121
8303
 
8122
- def deconstruct_keys(keys)
8304
+ def deconstruct_keys(_keys)
8123
8305
  { statements: statements, location: location, comments: comments }
8124
8306
  end
8125
8307
 
@@ -8177,7 +8359,7 @@ module SyntaxTree
8177
8359
 
8178
8360
  alias deconstruct child_nodes
8179
8361
 
8180
- def deconstruct_keys(keys)
8362
+ def deconstruct_keys(_keys)
8181
8363
  { parts: parts, quote: quote, location: location, comments: comments }
8182
8364
  end
8183
8365
 
@@ -8240,7 +8422,7 @@ module SyntaxTree
8240
8422
 
8241
8423
  alias deconstruct child_nodes
8242
8424
 
8243
- def deconstruct_keys(keys)
8425
+ def deconstruct_keys(_keys)
8244
8426
  { arguments: arguments, location: location, comments: comments }
8245
8427
  end
8246
8428
 
@@ -8293,7 +8475,7 @@ module SyntaxTree
8293
8475
 
8294
8476
  alias deconstruct child_nodes
8295
8477
 
8296
- def deconstruct_keys(keys)
8478
+ def deconstruct_keys(_keys)
8297
8479
  { value: value, location: location }
8298
8480
  end
8299
8481
  end
@@ -8323,7 +8505,7 @@ module SyntaxTree
8323
8505
 
8324
8506
  alias deconstruct child_nodes
8325
8507
 
8326
- def deconstruct_keys(keys)
8508
+ def deconstruct_keys(_keys)
8327
8509
  { value: value, location: location }
8328
8510
  end
8329
8511
  end
@@ -8357,7 +8539,7 @@ module SyntaxTree
8357
8539
 
8358
8540
  alias deconstruct child_nodes
8359
8541
 
8360
- def deconstruct_keys(keys)
8542
+ def deconstruct_keys(_keys)
8361
8543
  { value: value, location: location, comments: comments }
8362
8544
  end
8363
8545
 
@@ -8398,7 +8580,7 @@ module SyntaxTree
8398
8580
 
8399
8581
  alias deconstruct child_nodes
8400
8582
 
8401
- def deconstruct_keys(keys)
8583
+ def deconstruct_keys(_keys)
8402
8584
  {
8403
8585
  beginning: beginning,
8404
8586
  elements: elements,
@@ -8454,7 +8636,7 @@ module SyntaxTree
8454
8636
 
8455
8637
  alias deconstruct child_nodes
8456
8638
 
8457
- def deconstruct_keys(keys)
8639
+ def deconstruct_keys(_keys)
8458
8640
  { value: value, location: location }
8459
8641
  end
8460
8642
  end
@@ -8483,7 +8665,7 @@ module SyntaxTree
8483
8665
 
8484
8666
  alias deconstruct child_nodes
8485
8667
 
8486
- def deconstruct_keys(keys)
8668
+ def deconstruct_keys(_keys)
8487
8669
  { value: value, location: location }
8488
8670
  end
8489
8671
  end
@@ -8513,7 +8695,7 @@ module SyntaxTree
8513
8695
 
8514
8696
  alias deconstruct child_nodes
8515
8697
 
8516
- def deconstruct_keys(keys)
8698
+ def deconstruct_keys(_keys)
8517
8699
  { value: value, location: location }
8518
8700
  end
8519
8701
  end
@@ -8547,7 +8729,7 @@ module SyntaxTree
8547
8729
 
8548
8730
  alias deconstruct child_nodes
8549
8731
 
8550
- def deconstruct_keys(keys)
8732
+ def deconstruct_keys(_keys)
8551
8733
  { constant: constant, location: location, comments: comments }
8552
8734
  end
8553
8735
 
@@ -8585,7 +8767,7 @@ module SyntaxTree
8585
8767
 
8586
8768
  alias deconstruct child_nodes
8587
8769
 
8588
- def deconstruct_keys(keys)
8770
+ def deconstruct_keys(_keys)
8589
8771
  { constant: constant, location: location, comments: comments }
8590
8772
  end
8591
8773
 
@@ -8624,7 +8806,7 @@ module SyntaxTree
8624
8806
 
8625
8807
  alias deconstruct child_nodes
8626
8808
 
8627
- def deconstruct_keys(keys)
8809
+ def deconstruct_keys(_keys)
8628
8810
  { value: value, location: location }
8629
8811
  end
8630
8812
  end
@@ -8664,7 +8846,7 @@ module SyntaxTree
8664
8846
 
8665
8847
  alias deconstruct child_nodes
8666
8848
 
8667
- def deconstruct_keys(keys)
8849
+ def deconstruct_keys(_keys)
8668
8850
  { value: value, location: location, comments: comments }
8669
8851
  end
8670
8852
 
@@ -8702,7 +8884,7 @@ module SyntaxTree
8702
8884
 
8703
8885
  alias deconstruct child_nodes
8704
8886
 
8705
- def deconstruct_keys(keys)
8887
+ def deconstruct_keys(_keys)
8706
8888
  { value: value, location: location }
8707
8889
  end
8708
8890
  end
@@ -8738,7 +8920,7 @@ module SyntaxTree
8738
8920
 
8739
8921
  alias deconstruct child_nodes
8740
8922
 
8741
- def deconstruct_keys(keys)
8923
+ def deconstruct_keys(_keys)
8742
8924
  {
8743
8925
  statement: statement,
8744
8926
  parentheses: parentheses,
@@ -8749,7 +8931,9 @@ module SyntaxTree
8749
8931
 
8750
8932
  def format(q)
8751
8933
  parent = q.parents.take(2)[1]
8752
- ternary = (parent.is_a?(If) || parent.is_a?(Unless)) && Ternaryable.call(q, parent)
8934
+ ternary =
8935
+ (parent.is_a?(If) || parent.is_a?(Unless)) &&
8936
+ Ternaryable.call(q, parent)
8753
8937
 
8754
8938
  q.text("not")
8755
8939
 
@@ -8803,7 +8987,7 @@ module SyntaxTree
8803
8987
 
8804
8988
  alias deconstruct child_nodes
8805
8989
 
8806
- def deconstruct_keys(keys)
8990
+ def deconstruct_keys(_keys)
8807
8991
  {
8808
8992
  operator: operator,
8809
8993
  statement: statement,
@@ -8823,6 +9007,9 @@ module SyntaxTree
8823
9007
  # undef method
8824
9008
  #
8825
9009
  class Undef < Node
9010
+ # Undef accepts a variable number of arguments that can be either DynaSymbol
9011
+ # or SymbolLiteral objects. For SymbolLiteral objects we descend directly
9012
+ # into the value in order to have it come out as bare words.
8826
9013
  class UndefArgumentFormatter
8827
9014
  # [DynaSymbol | SymbolLiteral] the symbol to undefine
8828
9015
  attr_reader :node
@@ -8866,7 +9053,7 @@ module SyntaxTree
8866
9053
 
8867
9054
  alias deconstruct child_nodes
8868
9055
 
8869
- def deconstruct_keys(keys)
9056
+ def deconstruct_keys(_keys)
8870
9057
  { symbols: symbols, location: location, comments: comments }
8871
9058
  end
8872
9059
 
@@ -8925,7 +9112,7 @@ module SyntaxTree
8925
9112
 
8926
9113
  alias deconstruct child_nodes
8927
9114
 
8928
- def deconstruct_keys(keys)
9115
+ def deconstruct_keys(_keys)
8929
9116
  {
8930
9117
  predicate: predicate,
8931
9118
  statements: statements,
@@ -8971,7 +9158,7 @@ module SyntaxTree
8971
9158
 
8972
9159
  alias deconstruct child_nodes
8973
9160
 
8974
- def deconstruct_keys(keys)
9161
+ def deconstruct_keys(_keys)
8975
9162
  {
8976
9163
  statement: statement,
8977
9164
  predicate: predicate,
@@ -9010,13 +9197,15 @@ module SyntaxTree
9010
9197
  end
9011
9198
 
9012
9199
  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)
9200
+ q
9201
+ .if_break { format_break(q) }
9202
+ .if_flat do
9203
+ Parentheses.flat(q) do
9204
+ q.format(statements)
9205
+ q.text(" #{keyword} ")
9206
+ q.format(node.predicate)
9207
+ end
9018
9208
  end
9019
- end
9020
9209
  end
9021
9210
  end
9022
9211
 
@@ -9066,7 +9255,7 @@ module SyntaxTree
9066
9255
 
9067
9256
  alias deconstruct child_nodes
9068
9257
 
9069
- def deconstruct_keys(keys)
9258
+ def deconstruct_keys(_keys)
9070
9259
  {
9071
9260
  predicate: predicate,
9072
9261
  statements: statements,
@@ -9122,7 +9311,7 @@ module SyntaxTree
9122
9311
 
9123
9312
  alias deconstruct child_nodes
9124
9313
 
9125
- def deconstruct_keys(keys)
9314
+ def deconstruct_keys(_keys)
9126
9315
  {
9127
9316
  statement: statement,
9128
9317
  predicate: predicate,
@@ -9188,7 +9377,7 @@ module SyntaxTree
9188
9377
 
9189
9378
  alias deconstruct child_nodes
9190
9379
 
9191
- def deconstruct_keys(keys)
9380
+ def deconstruct_keys(_keys)
9192
9381
  { left: left, right: right, location: location, comments: comments }
9193
9382
  end
9194
9383
 
@@ -9231,7 +9420,7 @@ module SyntaxTree
9231
9420
 
9232
9421
  alias deconstruct child_nodes
9233
9422
 
9234
- def deconstruct_keys(keys)
9423
+ def deconstruct_keys(_keys)
9235
9424
  { value: value, location: location, comments: comments }
9236
9425
  end
9237
9426
 
@@ -9275,7 +9464,7 @@ module SyntaxTree
9275
9464
 
9276
9465
  alias deconstruct child_nodes
9277
9466
 
9278
- def deconstruct_keys(keys)
9467
+ def deconstruct_keys(_keys)
9279
9468
  { value: value, location: location, comments: comments }
9280
9469
  end
9281
9470
 
@@ -9316,7 +9505,7 @@ module SyntaxTree
9316
9505
 
9317
9506
  alias deconstruct child_nodes
9318
9507
 
9319
- def deconstruct_keys(keys)
9508
+ def deconstruct_keys(_keys)
9320
9509
  { value: value, location: location, comments: comments }
9321
9510
  end
9322
9511
 
@@ -9356,7 +9545,7 @@ module SyntaxTree
9356
9545
 
9357
9546
  alias deconstruct child_nodes
9358
9547
 
9359
- def deconstruct_keys(keys)
9548
+ def deconstruct_keys(_keys)
9360
9549
  { value: value, location: location, comments: comments }
9361
9550
  end
9362
9551
 
@@ -9391,7 +9580,7 @@ module SyntaxTree
9391
9580
 
9392
9581
  alias deconstruct child_nodes
9393
9582
 
9394
- def deconstruct_keys(keys)
9583
+ def deconstruct_keys(_keys)
9395
9584
  { location: location, comments: comments }
9396
9585
  end
9397
9586
 
@@ -9442,7 +9631,7 @@ module SyntaxTree
9442
9631
 
9443
9632
  alias deconstruct child_nodes
9444
9633
 
9445
- def deconstruct_keys(keys)
9634
+ def deconstruct_keys(_keys)
9446
9635
  {
9447
9636
  arguments: arguments,
9448
9637
  statements: statements,
@@ -9523,7 +9712,7 @@ module SyntaxTree
9523
9712
 
9524
9713
  alias deconstruct child_nodes
9525
9714
 
9526
- def deconstruct_keys(keys)
9715
+ def deconstruct_keys(_keys)
9527
9716
  {
9528
9717
  predicate: predicate,
9529
9718
  statements: statements,
@@ -9579,7 +9768,7 @@ module SyntaxTree
9579
9768
 
9580
9769
  alias deconstruct child_nodes
9581
9770
 
9582
- def deconstruct_keys(keys)
9771
+ def deconstruct_keys(_keys)
9583
9772
  {
9584
9773
  statement: statement,
9585
9774
  predicate: predicate,
@@ -9648,7 +9837,7 @@ module SyntaxTree
9648
9837
 
9649
9838
  alias deconstruct child_nodes
9650
9839
 
9651
- def deconstruct_keys(keys)
9840
+ def deconstruct_keys(_keys)
9652
9841
  { parts: parts, location: location, comments: comments }
9653
9842
  end
9654
9843
 
@@ -9688,7 +9877,7 @@ module SyntaxTree
9688
9877
 
9689
9878
  alias deconstruct child_nodes
9690
9879
 
9691
- def deconstruct_keys(keys)
9880
+ def deconstruct_keys(_keys)
9692
9881
  {
9693
9882
  beginning: beginning,
9694
9883
  elements: elements,
@@ -9744,7 +9933,7 @@ module SyntaxTree
9744
9933
 
9745
9934
  alias deconstruct child_nodes
9746
9935
 
9747
- def deconstruct_keys(keys)
9936
+ def deconstruct_keys(_keys)
9748
9937
  { value: value, location: location }
9749
9938
  end
9750
9939
  end
@@ -9773,7 +9962,7 @@ module SyntaxTree
9773
9962
 
9774
9963
  alias deconstruct child_nodes
9775
9964
 
9776
- def deconstruct_keys(keys)
9965
+ def deconstruct_keys(_keys)
9777
9966
  { parts: parts, location: location }
9778
9967
  end
9779
9968
  end
@@ -9806,7 +9995,7 @@ module SyntaxTree
9806
9995
 
9807
9996
  alias deconstruct child_nodes
9808
9997
 
9809
- def deconstruct_keys(keys)
9998
+ def deconstruct_keys(_keys)
9810
9999
  { parts: parts, location: location, comments: comments }
9811
10000
  end
9812
10001
 
@@ -9844,7 +10033,7 @@ module SyntaxTree
9844
10033
 
9845
10034
  alias deconstruct child_nodes
9846
10035
 
9847
- def deconstruct_keys(keys)
10036
+ def deconstruct_keys(_keys)
9848
10037
  { arguments: arguments, location: location, comments: comments }
9849
10038
  end
9850
10039
 
@@ -9894,7 +10083,7 @@ module SyntaxTree
9894
10083
 
9895
10084
  alias deconstruct child_nodes
9896
10085
 
9897
- def deconstruct_keys(keys)
10086
+ def deconstruct_keys(_keys)
9898
10087
  { value: value, location: location, comments: comments }
9899
10088
  end
9900
10089
 
@@ -9930,7 +10119,7 @@ module SyntaxTree
9930
10119
 
9931
10120
  alias deconstruct child_nodes
9932
10121
 
9933
- def deconstruct_keys(keys)
10122
+ def deconstruct_keys(_keys)
9934
10123
  { value: value, location: location, comments: comments }
9935
10124
  end
9936
10125