syntax_tree 2.3.0 → 2.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -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,
@@ -2899,6 +3003,7 @@ module SyntaxTree
2899
3003
  q.text(" ")
2900
3004
  yield
2901
3005
  in Args[parts: [IfOp]]
3006
+ q.if_flat { q.text(" ") }
2902
3007
  yield
2903
3008
  in Args[parts: [Command => command]]
2904
3009
  align(q, command, &block)
@@ -2956,7 +3061,7 @@ module SyntaxTree
2956
3061
 
2957
3062
  alias deconstruct child_nodes
2958
3063
 
2959
- def deconstruct_keys(keys)
3064
+ def deconstruct_keys(_keys)
2960
3065
  {
2961
3066
  receiver: receiver,
2962
3067
  operator: operator,
@@ -3078,7 +3183,7 @@ module SyntaxTree
3078
3183
  end
3079
3184
 
3080
3185
  def ignore?
3081
- value[1..-1].strip == "stree-ignore"
3186
+ value[1..].strip == "stree-ignore"
3082
3187
  end
3083
3188
 
3084
3189
  def comments
@@ -3095,7 +3200,7 @@ module SyntaxTree
3095
3200
 
3096
3201
  alias deconstruct child_nodes
3097
3202
 
3098
- def deconstruct_keys(keys)
3203
+ def deconstruct_keys(_keys)
3099
3204
  { value: value, inline: inline, location: location }
3100
3205
  end
3101
3206
 
@@ -3141,7 +3246,7 @@ module SyntaxTree
3141
3246
 
3142
3247
  alias deconstruct child_nodes
3143
3248
 
3144
- def deconstruct_keys(keys)
3249
+ def deconstruct_keys(_keys)
3145
3250
  { value: value, location: location, comments: comments }
3146
3251
  end
3147
3252
 
@@ -3183,7 +3288,7 @@ module SyntaxTree
3183
3288
 
3184
3289
  alias deconstruct child_nodes
3185
3290
 
3186
- def deconstruct_keys(keys)
3291
+ def deconstruct_keys(_keys)
3187
3292
  {
3188
3293
  parent: parent,
3189
3294
  constant: constant,
@@ -3230,7 +3335,7 @@ module SyntaxTree
3230
3335
 
3231
3336
  alias deconstruct child_nodes
3232
3337
 
3233
- def deconstruct_keys(keys)
3338
+ def deconstruct_keys(_keys)
3234
3339
  {
3235
3340
  parent: parent,
3236
3341
  constant: constant,
@@ -3275,7 +3380,7 @@ module SyntaxTree
3275
3380
 
3276
3381
  alias deconstruct child_nodes
3277
3382
 
3278
- def deconstruct_keys(keys)
3383
+ def deconstruct_keys(_keys)
3279
3384
  { constant: constant, location: location, comments: comments }
3280
3385
  end
3281
3386
 
@@ -3311,7 +3416,7 @@ module SyntaxTree
3311
3416
 
3312
3417
  alias deconstruct child_nodes
3313
3418
 
3314
- def deconstruct_keys(keys)
3419
+ def deconstruct_keys(_keys)
3315
3420
  { value: value, location: location, comments: comments }
3316
3421
  end
3317
3422
 
@@ -3355,7 +3460,7 @@ module SyntaxTree
3355
3460
 
3356
3461
  alias deconstruct child_nodes
3357
3462
 
3358
- def deconstruct_keys(keys)
3463
+ def deconstruct_keys(_keys)
3359
3464
  {
3360
3465
  name: name,
3361
3466
  params: params,
@@ -3440,7 +3545,7 @@ module SyntaxTree
3440
3545
 
3441
3546
  alias deconstruct child_nodes
3442
3547
 
3443
- def deconstruct_keys(keys)
3548
+ def deconstruct_keys(_keys)
3444
3549
  {
3445
3550
  target: target,
3446
3551
  operator: operator,
@@ -3508,7 +3613,7 @@ module SyntaxTree
3508
3613
 
3509
3614
  alias deconstruct child_nodes
3510
3615
 
3511
- def deconstruct_keys(keys)
3616
+ def deconstruct_keys(_keys)
3512
3617
  { value: value, location: location, comments: comments }
3513
3618
  end
3514
3619
 
@@ -3574,7 +3679,7 @@ module SyntaxTree
3574
3679
 
3575
3680
  alias deconstruct child_nodes
3576
3681
 
3577
- def deconstruct_keys(keys)
3682
+ def deconstruct_keys(_keys)
3578
3683
  {
3579
3684
  target: target,
3580
3685
  operator: operator,
@@ -3649,7 +3754,7 @@ module SyntaxTree
3649
3754
 
3650
3755
  alias deconstruct child_nodes
3651
3756
 
3652
- def deconstruct_keys(keys)
3757
+ def deconstruct_keys(_keys)
3653
3758
  {
3654
3759
  keyword: keyword,
3655
3760
  block_var: block_var,
@@ -3729,7 +3834,7 @@ module SyntaxTree
3729
3834
 
3730
3835
  alias deconstruct child_nodes
3731
3836
 
3732
- def deconstruct_keys(keys)
3837
+ def deconstruct_keys(_keys)
3733
3838
  { left: left, right: right, location: location, comments: comments }
3734
3839
  end
3735
3840
 
@@ -3777,7 +3882,7 @@ module SyntaxTree
3777
3882
 
3778
3883
  alias deconstruct child_nodes
3779
3884
 
3780
- def deconstruct_keys(keys)
3885
+ def deconstruct_keys(_keys)
3781
3886
  { left: left, right: right, location: location, comments: comments }
3782
3887
  end
3783
3888
 
@@ -3799,7 +3904,7 @@ module SyntaxTree
3799
3904
  # quotes, then single quotes would deactivate it.)
3800
3905
  def self.locked?(node)
3801
3906
  node.parts.any? do |part|
3802
- part.is_a?(TStringContent) && part.value.match?(/#[@${]|[\\]/)
3907
+ !part.is_a?(TStringContent) || part.value.match?(/\\|#[@${]/)
3803
3908
  end
3804
3909
  end
3805
3910
 
@@ -3864,7 +3969,7 @@ module SyntaxTree
3864
3969
 
3865
3970
  alias deconstruct child_nodes
3866
3971
 
3867
- def deconstruct_keys(keys)
3972
+ def deconstruct_keys(_keys)
3868
3973
  { parts: parts, quote: quote, location: location, comments: comments }
3869
3974
  end
3870
3975
 
@@ -3905,9 +4010,14 @@ module SyntaxTree
3905
4010
  matching = Quotes.matching(quote[2])
3906
4011
  pattern = /[\n#{Regexp.escape(matching)}'"]/
3907
4012
 
3908
- if parts.any? { |part|
3909
- part.is_a?(TStringContent) && part.value.match?(pattern)
3910
- }
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
3911
4021
  [quote, matching]
3912
4022
  elsif Quotes.locked?(self)
3913
4023
  ["#{":" unless hash_key}'", "'"]
@@ -3916,7 +4026,7 @@ module SyntaxTree
3916
4026
  end
3917
4027
  elsif Quotes.locked?(self)
3918
4028
  if quote.start_with?(":")
3919
- [hash_key ? quote[1..-1] : quote, quote[1..-1]]
4029
+ [hash_key ? quote[1..] : quote, quote[1..]]
3920
4030
  else
3921
4031
  [hash_key ? quote : ":#{quote}", quote]
3922
4032
  end
@@ -3959,7 +4069,7 @@ module SyntaxTree
3959
4069
 
3960
4070
  alias deconstruct child_nodes
3961
4071
 
3962
- def deconstruct_keys(keys)
4072
+ def deconstruct_keys(_keys)
3963
4073
  {
3964
4074
  keyword: keyword,
3965
4075
  statements: statements,
@@ -4025,7 +4135,7 @@ module SyntaxTree
4025
4135
 
4026
4136
  alias deconstruct child_nodes
4027
4137
 
4028
- def deconstruct_keys(keys)
4138
+ def deconstruct_keys(_keys)
4029
4139
  {
4030
4140
  predicate: predicate,
4031
4141
  statements: statements,
@@ -4097,7 +4207,7 @@ module SyntaxTree
4097
4207
 
4098
4208
  alias deconstruct child_nodes
4099
4209
 
4100
- def deconstruct_keys(keys)
4210
+ def deconstruct_keys(_keys)
4101
4211
  { value: value, location: location }
4102
4212
  end
4103
4213
 
@@ -4132,7 +4242,7 @@ module SyntaxTree
4132
4242
 
4133
4243
  alias deconstruct child_nodes
4134
4244
 
4135
- def deconstruct_keys(keys)
4245
+ def deconstruct_keys(_keys)
4136
4246
  { value: value, location: location }
4137
4247
  end
4138
4248
  end
@@ -4162,7 +4272,7 @@ module SyntaxTree
4162
4272
 
4163
4273
  alias deconstruct child_nodes
4164
4274
 
4165
- def deconstruct_keys(keys)
4275
+ def deconstruct_keys(_keys)
4166
4276
  { value: value, location: location }
4167
4277
  end
4168
4278
  end
@@ -4194,7 +4304,7 @@ module SyntaxTree
4194
4304
 
4195
4305
  alias deconstruct child_nodes
4196
4306
 
4197
- def deconstruct_keys(keys)
4307
+ def deconstruct_keys(_keys)
4198
4308
  { value: value, location: location }
4199
4309
  end
4200
4310
  end
@@ -4233,7 +4343,7 @@ module SyntaxTree
4233
4343
 
4234
4344
  alias deconstruct child_nodes
4235
4345
 
4236
- def deconstruct_keys(keys)
4346
+ def deconstruct_keys(_keys)
4237
4347
  {
4238
4348
  keyword: keyword,
4239
4349
  statements: statements,
@@ -4287,7 +4397,7 @@ module SyntaxTree
4287
4397
 
4288
4398
  alias deconstruct child_nodes
4289
4399
 
4290
- def deconstruct_keys(keys)
4400
+ def deconstruct_keys(_keys)
4291
4401
  { value: value, location: location, comments: comments }
4292
4402
  end
4293
4403
 
@@ -4330,7 +4440,7 @@ module SyntaxTree
4330
4440
 
4331
4441
  alias deconstruct child_nodes
4332
4442
 
4333
- def deconstruct_keys(keys)
4443
+ def deconstruct_keys(_keys)
4334
4444
  {
4335
4445
  value: value,
4336
4446
  arguments: arguments,
@@ -4342,7 +4452,8 @@ module SyntaxTree
4342
4452
  def format(q)
4343
4453
  q.format(value)
4344
4454
 
4345
- 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)
4346
4457
  # If you're using an explicit set of parentheses on something that looks
4347
4458
  # like a constant, then we need to match that in order to maintain valid
4348
4459
  # Ruby. For example, you could do something like Foo(), on which we
@@ -4389,7 +4500,7 @@ module SyntaxTree
4389
4500
 
4390
4501
  alias deconstruct child_nodes
4391
4502
 
4392
- def deconstruct_keys(keys)
4503
+ def deconstruct_keys(_keys)
4393
4504
  {
4394
4505
  parent: parent,
4395
4506
  operator: operator,
@@ -4435,7 +4546,7 @@ module SyntaxTree
4435
4546
 
4436
4547
  alias deconstruct child_nodes
4437
4548
 
4438
- def deconstruct_keys(keys)
4549
+ def deconstruct_keys(_keys)
4439
4550
  { value: value, location: location, comments: comments }
4440
4551
  end
4441
4552
 
@@ -4487,7 +4598,7 @@ module SyntaxTree
4487
4598
 
4488
4599
  alias deconstruct child_nodes
4489
4600
 
4490
- def deconstruct_keys(keys)
4601
+ def deconstruct_keys(_keys)
4491
4602
  {
4492
4603
  constant: constant,
4493
4604
  left: left,
@@ -4551,7 +4662,7 @@ module SyntaxTree
4551
4662
 
4552
4663
  alias deconstruct child_nodes
4553
4664
 
4554
- def deconstruct_keys(keys)
4665
+ def deconstruct_keys(_keys)
4555
4666
  {
4556
4667
  index: index,
4557
4668
  collection: collection,
@@ -4608,7 +4719,7 @@ module SyntaxTree
4608
4719
 
4609
4720
  alias deconstruct child_nodes
4610
4721
 
4611
- def deconstruct_keys(keys)
4722
+ def deconstruct_keys(_keys)
4612
4723
  { value: value, location: location, comments: comments }
4613
4724
  end
4614
4725
 
@@ -4622,6 +4733,9 @@ module SyntaxTree
4622
4733
  # { key => value }
4623
4734
  #
4624
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.
4625
4739
  class EmptyWithCommentsFormatter
4626
4740
  # [LBrace] the opening brace
4627
4741
  attr_reader :lbrace
@@ -4671,7 +4785,7 @@ module SyntaxTree
4671
4785
 
4672
4786
  alias deconstruct child_nodes
4673
4787
 
4674
- def deconstruct_keys(keys)
4788
+ def deconstruct_keys(_keys)
4675
4789
  { lbrace: lbrace, assocs: assocs, location: location, comments: comments }
4676
4790
  end
4677
4791
 
@@ -4740,7 +4854,14 @@ module SyntaxTree
4740
4854
  # [Array[ Comment | EmbDoc ]] the comments attached to this node
4741
4855
  attr_reader :comments
4742
4856
 
4743
- 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
+ )
4744
4865
  @beginning = beginning
4745
4866
  @ending = ending
4746
4867
  @dedent = dedent
@@ -4759,7 +4880,7 @@ module SyntaxTree
4759
4880
 
4760
4881
  alias deconstruct child_nodes
4761
4882
 
4762
- def deconstruct_keys(keys)
4883
+ def deconstruct_keys(_keys)
4763
4884
  {
4764
4885
  beginning: beginning,
4765
4886
  location: location,
@@ -4774,8 +4895,12 @@ module SyntaxTree
4774
4895
  # prettyprint module. It's when you want to force a newline, but don't
4775
4896
  # want to force the break parent.
4776
4897
  breakable = -> do
4777
- q.target <<
4778
- PrettyPrint::Breakable.new(" ", 1, indent: false, force: true)
4898
+ q.target << PrettyPrint::Breakable.new(
4899
+ " ",
4900
+ 1,
4901
+ indent: false,
4902
+ force: true
4903
+ )
4779
4904
  end
4780
4905
 
4781
4906
  q.group do
@@ -4831,7 +4956,7 @@ module SyntaxTree
4831
4956
 
4832
4957
  alias deconstruct child_nodes
4833
4958
 
4834
- def deconstruct_keys(keys)
4959
+ def deconstruct_keys(_keys)
4835
4960
  { value: value, location: location, comments: comments }
4836
4961
  end
4837
4962
 
@@ -4848,6 +4973,7 @@ module SyntaxTree
4848
4973
  # end
4849
4974
  #
4850
4975
  class HshPtn < Node
4976
+ # Formats a key-value pair in a hash pattern. The value is optional.
4851
4977
  class KeywordFormatter
4852
4978
  # [Label] the keyword being used
4853
4979
  attr_reader :key
@@ -4874,6 +5000,7 @@ module SyntaxTree
4874
5000
  end
4875
5001
  end
4876
5002
 
5003
+ # Formats the optional double-splat from the pattern.
4877
5004
  class KeywordRestFormatter
4878
5005
  # [VarField] the parameter that matches the remaining keywords
4879
5006
  attr_reader :keyword_rest
@@ -4923,7 +5050,7 @@ module SyntaxTree
4923
5050
 
4924
5051
  alias deconstruct child_nodes
4925
5052
 
4926
- def deconstruct_keys(keys)
5053
+ def deconstruct_keys(_keys)
4927
5054
  {
4928
5055
  constant: constant,
4929
5056
  keywords: keywords,
@@ -4937,36 +5064,64 @@ module SyntaxTree
4937
5064
  parts = keywords.map { |(key, value)| KeywordFormatter.new(key, value) }
4938
5065
  parts << KeywordRestFormatter.new(keyword_rest) if keyword_rest
4939
5066
 
5067
+ nested = PATTERNS.include?(q.parent.class)
4940
5068
  contents = -> do
4941
5069
  q.group { q.seplist(parts) { |part| q.format(part, stackable: false) } }
4942
5070
 
4943
5071
  # If there isn't a constant, and there's a blank keyword_rest, then we
4944
5072
  # have an plain ** that needs to have a `then` after it in order to
4945
5073
  # parse correctly on the next parse.
4946
- q.text(" then") if !constant && keyword_rest && keyword_rest.value.nil?
5074
+ if !constant && keyword_rest && keyword_rest.value.nil? && !nested
5075
+ q.text(" then")
5076
+ end
4947
5077
  end
4948
5078
 
5079
+ # If there is a constant, we're going to format to have the constant name
5080
+ # first and then use brackets.
4949
5081
  if constant
4950
- q.format(constant)
4951
- q.group(0, "[", "]", &contents)
5082
+ q.group do
5083
+ q.format(constant)
5084
+ q.text("[")
5085
+ q.indent do
5086
+ q.breakable("")
5087
+ contents.call
5088
+ end
5089
+ q.breakable("")
5090
+ q.text("]")
5091
+ end
4952
5092
  return
4953
5093
  end
4954
5094
 
5095
+ # If there's nothing at all, then we're going to use empty braces.
4955
5096
  if parts.empty?
4956
5097
  q.text("{}")
4957
- elsif PATTERNS.include?(q.parent.class)
4958
- q.text("{ ")
4959
- contents.call
4960
- q.text(" }")
4961
- else
5098
+ return
5099
+ end
5100
+
5101
+ # If there's only one pair, then we'll just print the contents provided
5102
+ # we're not inside another pattern.
5103
+ if !nested && parts.size == 1
4962
5104
  contents.call
5105
+ return
5106
+ end
5107
+
5108
+ # Otherwise, we're going to always use braces to make it clear it's a hash
5109
+ # pattern.
5110
+ q.group do
5111
+ q.text("{")
5112
+ q.indent do
5113
+ q.breakable
5114
+ contents.call
5115
+ end
5116
+ q.breakable
5117
+ q.text("}")
4963
5118
  end
4964
5119
  end
4965
5120
  end
4966
5121
 
4967
5122
  # The list of nodes that represent patterns inside of pattern matching so that
4968
5123
  # when a pattern is being printed it knows if it's nested.
4969
- PATTERNS = [AryPtn, Binary, FndPtn, HshPtn, RAssign]
5124
+ PATTERNS = [AryPtn, Binary, FndPtn, HshPtn, RAssign].freeze
4970
5125
 
4971
5126
  # Ident represents an identifier anywhere in code. It can represent a very
4972
5127
  # large number of things, depending on where it is in the syntax tree.
@@ -4996,7 +5151,7 @@ module SyntaxTree
4996
5151
 
4997
5152
  alias deconstruct child_nodes
4998
5153
 
4999
- def deconstruct_keys(keys)
5154
+ def deconstruct_keys(_keys)
5000
5155
  { value: value, location: location, comments: comments }
5001
5156
  end
5002
5157
 
@@ -5013,7 +5168,7 @@ module SyntaxTree
5013
5168
  def self.call(parent)
5014
5169
  queue = [parent]
5015
5170
 
5016
- while node = queue.shift
5171
+ while (node = queue.shift)
5017
5172
  return true if [Assign, MAssign, OpAssign].include?(node.class)
5018
5173
  queue += node.child_nodes
5019
5174
  end
@@ -5040,9 +5195,12 @@ module SyntaxTree
5040
5195
  else
5041
5196
  # Otherwise, we're going to check the conditional for certain cases.
5042
5197
  case node
5043
- in { predicate: Assign | Command | CommandCall | MAssign | OpAssign }
5198
+ in predicate: Assign | Command | CommandCall | MAssign | OpAssign
5044
5199
  false
5045
- in { statements: { body: [truthy] }, consequent: Else[statements: { body: [falsy] }] }
5200
+ in {
5201
+ statements: { body: [truthy] },
5202
+ consequent: Else[statements: { body: [falsy] }]
5203
+ }
5046
5204
  ternaryable?(truthy) && ternaryable?(falsy)
5047
5205
  else
5048
5206
  false
@@ -5053,8 +5211,8 @@ module SyntaxTree
5053
5211
  private
5054
5212
 
5055
5213
  # Certain expressions cannot be reduced to a ternary without adding
5056
- # parentheses around them. In this case we say they cannot be ternaried and
5057
- # default instead to breaking them into multiple lines.
5214
+ # parentheses around them. In this case we say they cannot be ternaried
5215
+ # and default instead to breaking them into multiple lines.
5058
5216
  def ternaryable?(statement)
5059
5217
  # This is a list of nodes that should not be allowed to be a part of a
5060
5218
  # ternary clause.
@@ -5112,13 +5270,15 @@ module SyntaxTree
5112
5270
  q.group { format_break(q, force: true) }
5113
5271
  else
5114
5272
  q.group do
5115
- q.if_break { format_break(q, force: false) }.if_flat do
5116
- Parentheses.flat(q) do
5117
- q.format(node.statements)
5118
- q.text(" #{keyword} ")
5119
- q.format(node.predicate)
5273
+ q
5274
+ .if_break { format_break(q, force: false) }
5275
+ .if_flat do
5276
+ Parentheses.flat(q) do
5277
+ q.format(node.statements)
5278
+ q.text(" #{keyword} ")
5279
+ q.format(node.predicate)
5280
+ end
5120
5281
  end
5121
- end
5122
5282
  end
5123
5283
  end
5124
5284
  end
@@ -5147,51 +5307,53 @@ module SyntaxTree
5147
5307
 
5148
5308
  def format_ternary(q)
5149
5309
  q.group do
5150
- q.if_break do
5151
- q.text("#{keyword} ")
5152
- q.nest(keyword.length + 1) { q.format(node.predicate) }
5153
-
5154
- q.indent do
5155
- q.breakable
5156
- q.format(node.statements)
5157
- end
5310
+ q
5311
+ .if_break do
5312
+ q.text("#{keyword} ")
5313
+ q.nest(keyword.length + 1) { q.format(node.predicate) }
5158
5314
 
5159
- q.breakable
5160
- q.group do
5161
- q.format(node.consequent.keyword)
5162
5315
  q.indent do
5163
- # This is a very special case of breakable where we want to force
5164
- # it into the output but we _don't_ want to explicitly break the
5165
- # parent. If a break-parent shows up in the tree, then it's going
5166
- # to force it all the way up to the tree, which is going to negate
5167
- # the ternary. Maybe this should be an option in prettyprint? As
5168
- # in force: :no_break_parent or something.
5169
- q.target << PrettyPrint::Breakable.new(" ", 1, force: true)
5170
- q.format(node.consequent.statements)
5316
+ q.breakable
5317
+ q.format(node.statements)
5171
5318
  end
5172
- end
5173
5319
 
5174
- q.breakable
5175
- q.text("end")
5176
- end.if_flat do
5177
- Parentheses.flat(q) do
5178
- q.format(node.predicate)
5179
- q.text(" ? ")
5320
+ q.breakable
5321
+ q.group do
5322
+ q.format(node.consequent.keyword)
5323
+ q.indent do
5324
+ # This is a very special case of breakable where we want to
5325
+ # force it into the output but we _don't_ want to explicitly
5326
+ # break the parent. If a break-parent shows up in the tree, then
5327
+ # it's going to force it all the way up to the tree, which is
5328
+ # going to negate the ternary. Maybe this should be an option in
5329
+ # prettyprint? As in force: :no_break_parent or something.
5330
+ q.target << PrettyPrint::Breakable.new(" ", 1, force: true)
5331
+ q.format(node.consequent.statements)
5332
+ end
5333
+ end
5334
+
5335
+ q.breakable
5336
+ q.text("end")
5337
+ end
5338
+ .if_flat do
5339
+ Parentheses.flat(q) do
5340
+ q.format(node.predicate)
5341
+ q.text(" ? ")
5180
5342
 
5181
- statements = [node.statements, node.consequent.statements]
5182
- statements.reverse! if keyword == "unless"
5343
+ statements = [node.statements, node.consequent.statements]
5344
+ statements.reverse! if keyword == "unless"
5183
5345
 
5184
- q.format(statements[0])
5185
- q.text(" : ")
5186
- q.format(statements[1])
5346
+ q.format(statements[0])
5347
+ q.text(" : ")
5348
+ q.format(statements[1])
5349
+ end
5187
5350
  end
5188
- end
5189
5351
  end
5190
5352
  end
5191
5353
 
5192
5354
  def contains_conditional?
5193
5355
  case node
5194
- in { statements: { body: [If | IfMod | IfOp | Unless | UnlessMod] } }
5356
+ in statements: { body: [If | IfMod | IfOp | Unless | UnlessMod] }
5195
5357
  true
5196
5358
  else
5197
5359
  false
@@ -5241,7 +5403,7 @@ module SyntaxTree
5241
5403
 
5242
5404
  alias deconstruct child_nodes
5243
5405
 
5244
- def deconstruct_keys(keys)
5406
+ def deconstruct_keys(_keys)
5245
5407
  {
5246
5408
  predicate: predicate,
5247
5409
  statements: statements,
@@ -5291,7 +5453,7 @@ module SyntaxTree
5291
5453
 
5292
5454
  alias deconstruct child_nodes
5293
5455
 
5294
- def deconstruct_keys(keys)
5456
+ def deconstruct_keys(_keys)
5295
5457
  {
5296
5458
  predicate: predicate,
5297
5459
  truthy: truthy,
@@ -5309,7 +5471,8 @@ module SyntaxTree
5309
5471
  Yield0, ZSuper
5310
5472
  ]
5311
5473
 
5312
- if q.parent.is_a?(Paren) || force_flat.include?(truthy.class) || force_flat.include?(falsy.class)
5474
+ if q.parent.is_a?(Paren) || force_flat.include?(truthy.class) ||
5475
+ force_flat.include?(falsy.class)
5313
5476
  q.group { format_flat(q) }
5314
5477
  return
5315
5478
  end
@@ -5429,7 +5592,7 @@ module SyntaxTree
5429
5592
 
5430
5593
  alias deconstruct child_nodes
5431
5594
 
5432
- def deconstruct_keys(keys)
5595
+ def deconstruct_keys(_keys)
5433
5596
  {
5434
5597
  statement: statement,
5435
5598
  predicate: predicate,
@@ -5470,7 +5633,7 @@ module SyntaxTree
5470
5633
 
5471
5634
  alias deconstruct child_nodes
5472
5635
 
5473
- def deconstruct_keys(keys)
5636
+ def deconstruct_keys(_keys)
5474
5637
  { value: value, location: location, comments: comments }
5475
5638
  end
5476
5639
 
@@ -5517,7 +5680,7 @@ module SyntaxTree
5517
5680
 
5518
5681
  alias deconstruct child_nodes
5519
5682
 
5520
- def deconstruct_keys(keys)
5683
+ def deconstruct_keys(_keys)
5521
5684
  {
5522
5685
  pattern: pattern,
5523
5686
  statements: statements,
@@ -5576,7 +5739,7 @@ module SyntaxTree
5576
5739
 
5577
5740
  alias deconstruct child_nodes
5578
5741
 
5579
- def deconstruct_keys(keys)
5742
+ def deconstruct_keys(_keys)
5580
5743
  { value: value, location: location, comments: comments }
5581
5744
  end
5582
5745
 
@@ -5586,7 +5749,7 @@ module SyntaxTree
5586
5749
  # the values, then we're going to insert them every 3 characters
5587
5750
  # starting from the right.
5588
5751
  index = (value.length + 2) % 3
5589
- q.text(" #{value}"[index..-1].scan(/.../).join("_").strip)
5752
+ q.text(" #{value}"[index..].scan(/.../).join("_").strip)
5590
5753
  else
5591
5754
  q.text(value)
5592
5755
  end
@@ -5620,7 +5783,7 @@ module SyntaxTree
5620
5783
 
5621
5784
  alias deconstruct child_nodes
5622
5785
 
5623
- def deconstruct_keys(keys)
5786
+ def deconstruct_keys(_keys)
5624
5787
  { value: value, location: location, comments: comments }
5625
5788
  end
5626
5789
 
@@ -5665,7 +5828,7 @@ module SyntaxTree
5665
5828
 
5666
5829
  alias deconstruct child_nodes
5667
5830
 
5668
- def deconstruct_keys(keys)
5831
+ def deconstruct_keys(_keys)
5669
5832
  { value: value, location: location, comments: comments }
5670
5833
  end
5671
5834
 
@@ -5702,7 +5865,7 @@ module SyntaxTree
5702
5865
 
5703
5866
  alias deconstruct child_nodes
5704
5867
 
5705
- def deconstruct_keys(keys)
5868
+ def deconstruct_keys(_keys)
5706
5869
  { name: name, location: location, comments: comments }
5707
5870
  end
5708
5871
 
@@ -5748,7 +5911,7 @@ module SyntaxTree
5748
5911
 
5749
5912
  alias deconstruct child_nodes
5750
5913
 
5751
- def deconstruct_keys(keys)
5914
+ def deconstruct_keys(_keys)
5752
5915
  { value: value, location: location, comments: comments }
5753
5916
  end
5754
5917
 
@@ -5783,7 +5946,7 @@ module SyntaxTree
5783
5946
 
5784
5947
  alias deconstruct child_nodes
5785
5948
 
5786
- def deconstruct_keys(keys)
5949
+ def deconstruct_keys(_keys)
5787
5950
  { value: value, location: location }
5788
5951
  end
5789
5952
  end
@@ -5819,7 +5982,7 @@ module SyntaxTree
5819
5982
 
5820
5983
  alias deconstruct child_nodes
5821
5984
 
5822
- def deconstruct_keys(keys)
5985
+ def deconstruct_keys(_keys)
5823
5986
  {
5824
5987
  params: params,
5825
5988
  statements: statements,
@@ -5841,25 +6004,27 @@ module SyntaxTree
5841
6004
  end
5842
6005
 
5843
6006
  q.text(" ")
5844
- q.if_break do
5845
- force_parens =
5846
- q.parents.any? do |node|
5847
- node.is_a?(Command) || node.is_a?(CommandCall)
6007
+ q
6008
+ .if_break do
6009
+ force_parens =
6010
+ q.parents.any? do |node|
6011
+ node.is_a?(Command) || node.is_a?(CommandCall)
6012
+ end
6013
+
6014
+ q.text(force_parens ? "{" : "do")
6015
+ q.indent do
6016
+ q.breakable
6017
+ q.format(statements)
5848
6018
  end
5849
6019
 
5850
- q.text(force_parens ? "{" : "do")
5851
- q.indent do
5852
6020
  q.breakable
6021
+ q.text(force_parens ? "}" : "end")
6022
+ end
6023
+ .if_flat do
6024
+ q.text("{ ")
5853
6025
  q.format(statements)
6026
+ q.text(" }")
5854
6027
  end
5855
-
5856
- q.breakable
5857
- q.text(force_parens ? "}" : "end")
5858
- end.if_flat do
5859
- q.text("{ ")
5860
- q.format(statements)
5861
- q.text(" }")
5862
- end
5863
6028
  end
5864
6029
  end
5865
6030
  end
@@ -5888,7 +6053,7 @@ module SyntaxTree
5888
6053
 
5889
6054
  alias deconstruct child_nodes
5890
6055
 
5891
- def deconstruct_keys(keys)
6056
+ def deconstruct_keys(_keys)
5892
6057
  { value: value, location: location, comments: comments }
5893
6058
  end
5894
6059
 
@@ -5921,7 +6086,7 @@ module SyntaxTree
5921
6086
 
5922
6087
  alias deconstruct child_nodes
5923
6088
 
5924
- def deconstruct_keys(keys)
6089
+ def deconstruct_keys(_keys)
5925
6090
  { value: value, location: location, comments: comments }
5926
6091
  end
5927
6092
 
@@ -5954,7 +6119,7 @@ module SyntaxTree
5954
6119
 
5955
6120
  alias deconstruct child_nodes
5956
6121
 
5957
- def deconstruct_keys(keys)
6122
+ def deconstruct_keys(_keys)
5958
6123
  { value: value, location: location, comments: comments }
5959
6124
  end
5960
6125
 
@@ -6004,7 +6169,7 @@ module SyntaxTree
6004
6169
 
6005
6170
  alias deconstruct child_nodes
6006
6171
 
6007
- def deconstruct_keys(keys)
6172
+ def deconstruct_keys(_keys)
6008
6173
  { target: target, value: value, location: location, comments: comments }
6009
6174
  end
6010
6175
 
@@ -6051,7 +6216,7 @@ module SyntaxTree
6051
6216
 
6052
6217
  alias deconstruct child_nodes
6053
6218
 
6054
- def deconstruct_keys(keys)
6219
+ def deconstruct_keys(_keys)
6055
6220
  { call: call, block: block, location: location, comments: comments }
6056
6221
  end
6057
6222
 
@@ -6059,8 +6224,13 @@ module SyntaxTree
6059
6224
  # If we're at the top of a call chain, then we're going to do some
6060
6225
  # specialized printing in case we can print it nicely. We _only_ do this
6061
6226
  # at the top of the chain to avoid weird recursion issues.
6062
- if !CallChainFormatter.chained?(q.parent) && CallChainFormatter.chained?(call)
6063
- q.group { q.if_break { CallChainFormatter.new(self).format(q) }.if_flat { format_contents(q) } }
6227
+ if !CallChainFormatter.chained?(q.parent) &&
6228
+ CallChainFormatter.chained?(call)
6229
+ q.group do
6230
+ q
6231
+ .if_break { CallChainFormatter.new(self).format(q) }
6232
+ .if_flat { format_contents(q) }
6233
+ end
6064
6234
  else
6065
6235
  format_contents(q)
6066
6236
  end
@@ -6107,7 +6277,7 @@ module SyntaxTree
6107
6277
 
6108
6278
  alias deconstruct child_nodes
6109
6279
 
6110
- def deconstruct_keys(keys)
6280
+ def deconstruct_keys(_keys)
6111
6281
  { parts: parts, location: location, comma: comma, comments: comments }
6112
6282
  end
6113
6283
 
@@ -6151,7 +6321,7 @@ module SyntaxTree
6151
6321
 
6152
6322
  alias deconstruct child_nodes
6153
6323
 
6154
- def deconstruct_keys(keys)
6324
+ def deconstruct_keys(_keys)
6155
6325
  { contents: contents, location: location, comments: comments }
6156
6326
  end
6157
6327
 
@@ -6207,7 +6377,7 @@ module SyntaxTree
6207
6377
 
6208
6378
  alias deconstruct child_nodes
6209
6379
 
6210
- def deconstruct_keys(keys)
6380
+ def deconstruct_keys(_keys)
6211
6381
  {
6212
6382
  constant: constant,
6213
6383
  bodystmt: bodystmt,
@@ -6274,7 +6444,7 @@ module SyntaxTree
6274
6444
 
6275
6445
  alias deconstruct child_nodes
6276
6446
 
6277
- def deconstruct_keys(keys)
6447
+ def deconstruct_keys(_keys)
6278
6448
  { parts: parts, location: location, comments: comments }
6279
6449
  end
6280
6450
 
@@ -6323,7 +6493,7 @@ module SyntaxTree
6323
6493
 
6324
6494
  alias deconstruct child_nodes
6325
6495
 
6326
- def deconstruct_keys(keys)
6496
+ def deconstruct_keys(_keys)
6327
6497
  { arguments: arguments, location: location, comments: comments }
6328
6498
  end
6329
6499
 
@@ -6360,7 +6530,7 @@ module SyntaxTree
6360
6530
 
6361
6531
  alias deconstruct child_nodes
6362
6532
 
6363
- def deconstruct_keys(keys)
6533
+ def deconstruct_keys(_keys)
6364
6534
  { value: value, location: location, comments: comments }
6365
6535
  end
6366
6536
 
@@ -6406,7 +6576,7 @@ module SyntaxTree
6406
6576
 
6407
6577
  alias deconstruct child_nodes
6408
6578
 
6409
- def deconstruct_keys(keys)
6579
+ def deconstruct_keys(_keys)
6410
6580
  {
6411
6581
  target: target,
6412
6582
  operator: operator,
@@ -6474,7 +6644,16 @@ module SyntaxTree
6474
6644
  # This approach maintains the nice conciseness of the inline version, while
6475
6645
  # keeping the correct semantic meaning.
6476
6646
  module Parentheses
6477
- NODES = [Args, Assign, Assoc, Binary, Call, Defined, MAssign, OpAssign]
6647
+ NODES = [
6648
+ Args,
6649
+ Assign,
6650
+ Assoc,
6651
+ Binary,
6652
+ Call,
6653
+ Defined,
6654
+ MAssign,
6655
+ OpAssign
6656
+ ].freeze
6478
6657
 
6479
6658
  def self.flat(q)
6480
6659
  return yield unless NODES.include?(q.parent.class)
@@ -6506,6 +6685,8 @@ module SyntaxTree
6506
6685
  # def method(param) end
6507
6686
  #
6508
6687
  class Params < Node
6688
+ # Formats the optional position of the parameters. This includes the label,
6689
+ # as well as the default value.
6509
6690
  class OptionalFormatter
6510
6691
  # [Ident] the name of the parameter
6511
6692
  attr_reader :name
@@ -6529,6 +6710,8 @@ module SyntaxTree
6529
6710
  end
6530
6711
  end
6531
6712
 
6713
+ # Formats the keyword position of the parameters. This includes the label,
6714
+ # as well as an optional default value.
6532
6715
  class KeywordFormatter
6533
6716
  # [Ident] the name of the parameter
6534
6717
  attr_reader :name
@@ -6555,6 +6738,8 @@ module SyntaxTree
6555
6738
  end
6556
6739
  end
6557
6740
 
6741
+ # Formats the keyword_rest position of the parameters. This can be the **nil
6742
+ # syntax, the ... syntax, or the ** syntax.
6558
6743
  class KeywordRestFormatter
6559
6744
  # [:nil | ArgsForward | KwRestParam] the value of the parameter
6560
6745
  attr_reader :value
@@ -6568,11 +6753,7 @@ module SyntaxTree
6568
6753
  end
6569
6754
 
6570
6755
  def format(q)
6571
- if value == :nil
6572
- q.text("**nil")
6573
- else
6574
- q.format(value)
6575
- end
6756
+ value == :nil ? q.text("**nil") : q.format(value)
6576
6757
  end
6577
6758
  end
6578
6759
 
@@ -6653,7 +6834,7 @@ module SyntaxTree
6653
6834
 
6654
6835
  alias deconstruct child_nodes
6655
6836
 
6656
- def deconstruct_keys(keys)
6837
+ def deconstruct_keys(_keys)
6657
6838
  {
6658
6839
  location: location,
6659
6840
  requireds: requireds,
@@ -6674,18 +6855,17 @@ module SyntaxTree
6674
6855
  ]
6675
6856
 
6676
6857
  parts << rest if rest && !rest.is_a?(ExcessedComma)
6677
- parts +=
6678
- [
6679
- *posts,
6680
- *keywords.map { |(name, value)| KeywordFormatter.new(name, value) }
6681
- ]
6858
+ parts += [
6859
+ *posts,
6860
+ *keywords.map { |(name, value)| KeywordFormatter.new(name, value) }
6861
+ ]
6682
6862
 
6683
6863
  parts << KeywordRestFormatter.new(keyword_rest) if keyword_rest
6684
6864
  parts << block if block
6685
6865
 
6686
6866
  contents = -> do
6687
6867
  q.seplist(parts) { |part| q.format(part) }
6688
- q.format(rest) if rest && rest.is_a?(ExcessedComma)
6868
+ q.format(rest) if rest.is_a?(ExcessedComma)
6689
6869
  end
6690
6870
 
6691
6871
  if ![Def, Defs, DefEndless].include?(q.parent.class) || parts.empty?
@@ -6735,7 +6915,7 @@ module SyntaxTree
6735
6915
 
6736
6916
  alias deconstruct child_nodes
6737
6917
 
6738
- def deconstruct_keys(keys)
6918
+ def deconstruct_keys(_keys)
6739
6919
  {
6740
6920
  lparen: lparen,
6741
6921
  contents: contents,
@@ -6786,7 +6966,7 @@ module SyntaxTree
6786
6966
 
6787
6967
  alias deconstruct child_nodes
6788
6968
 
6789
- def deconstruct_keys(keys)
6969
+ def deconstruct_keys(_keys)
6790
6970
  { value: value, location: location, comments: comments }
6791
6971
  end
6792
6972
 
@@ -6819,7 +6999,7 @@ module SyntaxTree
6819
6999
 
6820
7000
  alias deconstruct child_nodes
6821
7001
 
6822
- def deconstruct_keys(keys)
7002
+ def deconstruct_keys(_keys)
6823
7003
  { statements: statements, location: location, comments: comments }
6824
7004
  end
6825
7005
 
@@ -6864,7 +7044,7 @@ module SyntaxTree
6864
7044
 
6865
7045
  alias deconstruct child_nodes
6866
7046
 
6867
- def deconstruct_keys(keys)
7047
+ def deconstruct_keys(_keys)
6868
7048
  {
6869
7049
  beginning: beginning,
6870
7050
  elements: elements,
@@ -6919,18 +7099,9 @@ module SyntaxTree
6919
7099
 
6920
7100
  alias deconstruct child_nodes
6921
7101
 
6922
- def deconstruct_keys(keys)
7102
+ def deconstruct_keys(_keys)
6923
7103
  { value: value, location: location }
6924
7104
  end
6925
-
6926
- def pretty_print(q)
6927
- q.group(2, "(", ")") do
6928
- q.text("qsymbols_beg")
6929
-
6930
- q.breakable
6931
- q.pp(value)
6932
- end
6933
- end
6934
7105
  end
6935
7106
 
6936
7107
  # QWords represents a string literal array without interpolation.
@@ -6964,7 +7135,7 @@ module SyntaxTree
6964
7135
 
6965
7136
  alias deconstruct child_nodes
6966
7137
 
6967
- def deconstruct_keys(keys)
7138
+ def deconstruct_keys(_keys)
6968
7139
  {
6969
7140
  beginning: beginning,
6970
7141
  elements: elements,
@@ -7019,7 +7190,7 @@ module SyntaxTree
7019
7190
 
7020
7191
  alias deconstruct child_nodes
7021
7192
 
7022
- def deconstruct_keys(keys)
7193
+ def deconstruct_keys(_keys)
7023
7194
  { value: value, location: location }
7024
7195
  end
7025
7196
  end
@@ -7051,7 +7222,7 @@ module SyntaxTree
7051
7222
 
7052
7223
  alias deconstruct child_nodes
7053
7224
 
7054
- def deconstruct_keys(keys)
7225
+ def deconstruct_keys(_keys)
7055
7226
  { value: value, location: location, comments: comments }
7056
7227
  end
7057
7228
 
@@ -7080,7 +7251,7 @@ module SyntaxTree
7080
7251
 
7081
7252
  alias deconstruct child_nodes
7082
7253
 
7083
- def deconstruct_keys(keys)
7254
+ def deconstruct_keys(_keys)
7084
7255
  { value: value, location: location }
7085
7256
  end
7086
7257
  end
@@ -7105,7 +7276,7 @@ module SyntaxTree
7105
7276
 
7106
7277
  alias deconstruct child_nodes
7107
7278
 
7108
- def deconstruct_keys(keys)
7279
+ def deconstruct_keys(_keys)
7109
7280
  { value: value, location: location }
7110
7281
  end
7111
7282
  end
@@ -7137,7 +7308,7 @@ module SyntaxTree
7137
7308
 
7138
7309
  alias deconstruct child_nodes
7139
7310
 
7140
- def deconstruct_keys(keys)
7311
+ def deconstruct_keys(_keys)
7141
7312
  { value: value, location: location, comments: comments }
7142
7313
  end
7143
7314
 
@@ -7176,7 +7347,7 @@ module SyntaxTree
7176
7347
 
7177
7348
  alias deconstruct child_nodes
7178
7349
 
7179
- def deconstruct_keys(keys)
7350
+ def deconstruct_keys(_keys)
7180
7351
  { beginning: beginning, parts: parts, location: location }
7181
7352
  end
7182
7353
  end
@@ -7209,7 +7380,7 @@ module SyntaxTree
7209
7380
 
7210
7381
  alias deconstruct child_nodes
7211
7382
 
7212
- def deconstruct_keys(keys)
7383
+ def deconstruct_keys(_keys)
7213
7384
  { value: value, location: location }
7214
7385
  end
7215
7386
  end
@@ -7243,7 +7414,7 @@ module SyntaxTree
7243
7414
 
7244
7415
  alias deconstruct child_nodes
7245
7416
 
7246
- def deconstruct_keys(keys)
7417
+ def deconstruct_keys(_keys)
7247
7418
  { value: value, location: location }
7248
7419
  end
7249
7420
  end
@@ -7284,10 +7455,11 @@ module SyntaxTree
7284
7455
 
7285
7456
  alias deconstruct child_nodes
7286
7457
 
7287
- def deconstruct_keys(keys)
7458
+ def deconstruct_keys(_keys)
7288
7459
  {
7289
7460
  beginning: beginning,
7290
7461
  ending: ending,
7462
+ options: options,
7291
7463
  parts: parts,
7292
7464
  location: location,
7293
7465
  comments: comments
@@ -7295,7 +7467,7 @@ module SyntaxTree
7295
7467
  end
7296
7468
 
7297
7469
  def format(q)
7298
- braces = ambiguous?(q) || include?(%r{\/})
7470
+ braces = ambiguous?(q) || include?(%r{/})
7299
7471
 
7300
7472
  if braces && include?(/[{}]/)
7301
7473
  q.group do
@@ -7322,18 +7494,22 @@ module SyntaxTree
7322
7494
  end
7323
7495
 
7324
7496
  q.text("}")
7325
- q.text(ending[1..-1])
7497
+ q.text(options)
7326
7498
  end
7327
7499
  else
7328
7500
  q.group do
7329
7501
  q.text("/")
7330
7502
  q.format_each(parts)
7331
7503
  q.text("/")
7332
- q.text(ending[1..-1])
7504
+ q.text(options)
7333
7505
  end
7334
7506
  end
7335
7507
  end
7336
7508
 
7509
+ def options
7510
+ ending[1..]
7511
+ end
7512
+
7337
7513
  private
7338
7514
 
7339
7515
  def include?(pattern)
@@ -7389,7 +7565,7 @@ module SyntaxTree
7389
7565
 
7390
7566
  alias deconstruct child_nodes
7391
7567
 
7392
- def deconstruct_keys(keys)
7568
+ def deconstruct_keys(_keys)
7393
7569
  {
7394
7570
  exceptions: exceptions,
7395
7571
  variable: variable,
@@ -7464,7 +7640,10 @@ module SyntaxTree
7464
7640
 
7465
7641
  if consequent
7466
7642
  consequent.bind_end(end_char, end_column)
7467
- statements.bind_end(consequent.location.start_char, consequent.location.start_column)
7643
+ statements.bind_end(
7644
+ consequent.location.start_char,
7645
+ consequent.location.start_column
7646
+ )
7468
7647
  else
7469
7648
  statements.bind_end(end_char, end_column)
7470
7649
  end
@@ -7480,7 +7659,7 @@ module SyntaxTree
7480
7659
 
7481
7660
  alias deconstruct child_nodes
7482
7661
 
7483
- def deconstruct_keys(keys)
7662
+ def deconstruct_keys(_keys)
7484
7663
  {
7485
7664
  keyword: keyword,
7486
7665
  exception: exception,
@@ -7547,7 +7726,7 @@ module SyntaxTree
7547
7726
 
7548
7727
  alias deconstruct child_nodes
7549
7728
 
7550
- def deconstruct_keys(keys)
7729
+ def deconstruct_keys(_keys)
7551
7730
  {
7552
7731
  statement: statement,
7553
7732
  value: value,
@@ -7601,7 +7780,7 @@ module SyntaxTree
7601
7780
 
7602
7781
  alias deconstruct child_nodes
7603
7782
 
7604
- def deconstruct_keys(keys)
7783
+ def deconstruct_keys(_keys)
7605
7784
  { name: name, location: location, comments: comments }
7606
7785
  end
7607
7786
 
@@ -7638,7 +7817,7 @@ module SyntaxTree
7638
7817
 
7639
7818
  alias deconstruct child_nodes
7640
7819
 
7641
- def deconstruct_keys(keys)
7820
+ def deconstruct_keys(_keys)
7642
7821
  { value: value, location: location, comments: comments }
7643
7822
  end
7644
7823
 
@@ -7674,7 +7853,7 @@ module SyntaxTree
7674
7853
 
7675
7854
  alias deconstruct child_nodes
7676
7855
 
7677
- def deconstruct_keys(keys)
7856
+ def deconstruct_keys(_keys)
7678
7857
  { arguments: arguments, location: location, comments: comments }
7679
7858
  end
7680
7859
 
@@ -7710,7 +7889,7 @@ module SyntaxTree
7710
7889
 
7711
7890
  alias deconstruct child_nodes
7712
7891
 
7713
- def deconstruct_keys(keys)
7892
+ def deconstruct_keys(_keys)
7714
7893
  { value: value, location: location, comments: comments }
7715
7894
  end
7716
7895
 
@@ -7739,7 +7918,7 @@ module SyntaxTree
7739
7918
 
7740
7919
  alias deconstruct child_nodes
7741
7920
 
7742
- def deconstruct_keys(keys)
7921
+ def deconstruct_keys(_keys)
7743
7922
  { value: value, location: location }
7744
7923
  end
7745
7924
  end
@@ -7778,7 +7957,7 @@ module SyntaxTree
7778
7957
 
7779
7958
  alias deconstruct child_nodes
7780
7959
 
7781
- def deconstruct_keys(keys)
7960
+ def deconstruct_keys(_keys)
7782
7961
  {
7783
7962
  target: target,
7784
7963
  bodystmt: bodystmt,
@@ -7880,7 +8059,7 @@ module SyntaxTree
7880
8059
 
7881
8060
  alias deconstruct child_nodes
7882
8061
 
7883
- def deconstruct_keys(keys)
8062
+ def deconstruct_keys(_keys)
7884
8063
  { parser: parser, body: body, location: location, comments: comments }
7885
8064
  end
7886
8065
 
@@ -7950,12 +8129,19 @@ module SyntaxTree
7950
8129
  comment = parser_comments[comment_index]
7951
8130
  location = comment.location
7952
8131
 
7953
- if !comment.inline? && (start_char <= location.start_char) && (end_char >= location.end_char) && !comment.ignore?
7954
- while (node = body[body_index]) && (node.is_a?(VoidStmt) || node.location.start_char < location.start_char)
8132
+ if !comment.inline? && (start_char <= location.start_char) &&
8133
+ (end_char >= location.end_char) && !comment.ignore?
8134
+ while (node = body[body_index]) &&
8135
+ (
8136
+ node.is_a?(VoidStmt) ||
8137
+ node.location.start_char < location.start_char
8138
+ )
7955
8139
  body_index += 1
7956
8140
  end
7957
8141
 
7958
- if body_index != 0 && body[body_index - 1].location.start_char < location.start_char && body[body_index - 1].location.end_char > location.start_char
8142
+ if body_index != 0 &&
8143
+ body[body_index - 1].location.start_char < location.start_char &&
8144
+ body[body_index - 1].location.end_char > location.start_char
7959
8145
  # The previous node entirely encapsules the comment, so we don't
7960
8146
  # want to attach it here since it will get attached normally. This
7961
8147
  # is mostly in the case of hash and array literals.
@@ -7995,7 +8181,7 @@ module SyntaxTree
7995
8181
 
7996
8182
  alias deconstruct child_nodes
7997
8183
 
7998
- def deconstruct_keys(keys)
8184
+ def deconstruct_keys(_keys)
7999
8185
  { parts: parts, location: location }
8000
8186
  end
8001
8187
  end
@@ -8033,14 +8219,14 @@ module SyntaxTree
8033
8219
 
8034
8220
  alias deconstruct child_nodes
8035
8221
 
8036
- def deconstruct_keys(keys)
8222
+ def deconstruct_keys(_keys)
8037
8223
  { left: left, right: right, location: location, comments: comments }
8038
8224
  end
8039
8225
 
8040
8226
  def format(q)
8041
8227
  q.group do
8042
8228
  q.format(left)
8043
- q.text(' \\')
8229
+ q.text(" \\")
8044
8230
  q.indent do
8045
8231
  q.breakable(force: true)
8046
8232
  q.format(right)
@@ -8078,7 +8264,7 @@ module SyntaxTree
8078
8264
 
8079
8265
  alias deconstruct child_nodes
8080
8266
 
8081
- def deconstruct_keys(keys)
8267
+ def deconstruct_keys(_keys)
8082
8268
  { variable: variable, location: location, comments: comments }
8083
8269
  end
8084
8270
 
@@ -8118,7 +8304,7 @@ module SyntaxTree
8118
8304
 
8119
8305
  alias deconstruct child_nodes
8120
8306
 
8121
- def deconstruct_keys(keys)
8307
+ def deconstruct_keys(_keys)
8122
8308
  { statements: statements, location: location, comments: comments }
8123
8309
  end
8124
8310
 
@@ -8176,7 +8362,7 @@ module SyntaxTree
8176
8362
 
8177
8363
  alias deconstruct child_nodes
8178
8364
 
8179
- def deconstruct_keys(keys)
8365
+ def deconstruct_keys(_keys)
8180
8366
  { parts: parts, quote: quote, location: location, comments: comments }
8181
8367
  end
8182
8368
 
@@ -8239,7 +8425,7 @@ module SyntaxTree
8239
8425
 
8240
8426
  alias deconstruct child_nodes
8241
8427
 
8242
- def deconstruct_keys(keys)
8428
+ def deconstruct_keys(_keys)
8243
8429
  { arguments: arguments, location: location, comments: comments }
8244
8430
  end
8245
8431
 
@@ -8292,7 +8478,7 @@ module SyntaxTree
8292
8478
 
8293
8479
  alias deconstruct child_nodes
8294
8480
 
8295
- def deconstruct_keys(keys)
8481
+ def deconstruct_keys(_keys)
8296
8482
  { value: value, location: location }
8297
8483
  end
8298
8484
  end
@@ -8322,7 +8508,7 @@ module SyntaxTree
8322
8508
 
8323
8509
  alias deconstruct child_nodes
8324
8510
 
8325
- def deconstruct_keys(keys)
8511
+ def deconstruct_keys(_keys)
8326
8512
  { value: value, location: location }
8327
8513
  end
8328
8514
  end
@@ -8356,7 +8542,7 @@ module SyntaxTree
8356
8542
 
8357
8543
  alias deconstruct child_nodes
8358
8544
 
8359
- def deconstruct_keys(keys)
8545
+ def deconstruct_keys(_keys)
8360
8546
  { value: value, location: location, comments: comments }
8361
8547
  end
8362
8548
 
@@ -8397,7 +8583,7 @@ module SyntaxTree
8397
8583
 
8398
8584
  alias deconstruct child_nodes
8399
8585
 
8400
- def deconstruct_keys(keys)
8586
+ def deconstruct_keys(_keys)
8401
8587
  {
8402
8588
  beginning: beginning,
8403
8589
  elements: elements,
@@ -8453,7 +8639,7 @@ module SyntaxTree
8453
8639
 
8454
8640
  alias deconstruct child_nodes
8455
8641
 
8456
- def deconstruct_keys(keys)
8642
+ def deconstruct_keys(_keys)
8457
8643
  { value: value, location: location }
8458
8644
  end
8459
8645
  end
@@ -8482,7 +8668,7 @@ module SyntaxTree
8482
8668
 
8483
8669
  alias deconstruct child_nodes
8484
8670
 
8485
- def deconstruct_keys(keys)
8671
+ def deconstruct_keys(_keys)
8486
8672
  { value: value, location: location }
8487
8673
  end
8488
8674
  end
@@ -8512,7 +8698,7 @@ module SyntaxTree
8512
8698
 
8513
8699
  alias deconstruct child_nodes
8514
8700
 
8515
- def deconstruct_keys(keys)
8701
+ def deconstruct_keys(_keys)
8516
8702
  { value: value, location: location }
8517
8703
  end
8518
8704
  end
@@ -8546,7 +8732,7 @@ module SyntaxTree
8546
8732
 
8547
8733
  alias deconstruct child_nodes
8548
8734
 
8549
- def deconstruct_keys(keys)
8735
+ def deconstruct_keys(_keys)
8550
8736
  { constant: constant, location: location, comments: comments }
8551
8737
  end
8552
8738
 
@@ -8584,7 +8770,7 @@ module SyntaxTree
8584
8770
 
8585
8771
  alias deconstruct child_nodes
8586
8772
 
8587
- def deconstruct_keys(keys)
8773
+ def deconstruct_keys(_keys)
8588
8774
  { constant: constant, location: location, comments: comments }
8589
8775
  end
8590
8776
 
@@ -8623,7 +8809,7 @@ module SyntaxTree
8623
8809
 
8624
8810
  alias deconstruct child_nodes
8625
8811
 
8626
- def deconstruct_keys(keys)
8812
+ def deconstruct_keys(_keys)
8627
8813
  { value: value, location: location }
8628
8814
  end
8629
8815
  end
@@ -8663,7 +8849,7 @@ module SyntaxTree
8663
8849
 
8664
8850
  alias deconstruct child_nodes
8665
8851
 
8666
- def deconstruct_keys(keys)
8852
+ def deconstruct_keys(_keys)
8667
8853
  { value: value, location: location, comments: comments }
8668
8854
  end
8669
8855
 
@@ -8701,7 +8887,7 @@ module SyntaxTree
8701
8887
 
8702
8888
  alias deconstruct child_nodes
8703
8889
 
8704
- def deconstruct_keys(keys)
8890
+ def deconstruct_keys(_keys)
8705
8891
  { value: value, location: location }
8706
8892
  end
8707
8893
  end
@@ -8737,7 +8923,7 @@ module SyntaxTree
8737
8923
 
8738
8924
  alias deconstruct child_nodes
8739
8925
 
8740
- def deconstruct_keys(keys)
8926
+ def deconstruct_keys(_keys)
8741
8927
  {
8742
8928
  statement: statement,
8743
8929
  parentheses: parentheses,
@@ -8748,7 +8934,9 @@ module SyntaxTree
8748
8934
 
8749
8935
  def format(q)
8750
8936
  parent = q.parents.take(2)[1]
8751
- ternary = (parent.is_a?(If) || parent.is_a?(Unless)) && Ternaryable.call(q, parent)
8937
+ ternary =
8938
+ (parent.is_a?(If) || parent.is_a?(Unless)) &&
8939
+ Ternaryable.call(q, parent)
8752
8940
 
8753
8941
  q.text("not")
8754
8942
 
@@ -8802,7 +8990,7 @@ module SyntaxTree
8802
8990
 
8803
8991
  alias deconstruct child_nodes
8804
8992
 
8805
- def deconstruct_keys(keys)
8993
+ def deconstruct_keys(_keys)
8806
8994
  {
8807
8995
  operator: operator,
8808
8996
  statement: statement,
@@ -8822,6 +9010,9 @@ module SyntaxTree
8822
9010
  # undef method
8823
9011
  #
8824
9012
  class Undef < Node
9013
+ # Undef accepts a variable number of arguments that can be either DynaSymbol
9014
+ # or SymbolLiteral objects. For SymbolLiteral objects we descend directly
9015
+ # into the value in order to have it come out as bare words.
8825
9016
  class UndefArgumentFormatter
8826
9017
  # [DynaSymbol | SymbolLiteral] the symbol to undefine
8827
9018
  attr_reader :node
@@ -8865,7 +9056,7 @@ module SyntaxTree
8865
9056
 
8866
9057
  alias deconstruct child_nodes
8867
9058
 
8868
- def deconstruct_keys(keys)
9059
+ def deconstruct_keys(_keys)
8869
9060
  { symbols: symbols, location: location, comments: comments }
8870
9061
  end
8871
9062
 
@@ -8924,7 +9115,7 @@ module SyntaxTree
8924
9115
 
8925
9116
  alias deconstruct child_nodes
8926
9117
 
8927
- def deconstruct_keys(keys)
9118
+ def deconstruct_keys(_keys)
8928
9119
  {
8929
9120
  predicate: predicate,
8930
9121
  statements: statements,
@@ -8970,7 +9161,7 @@ module SyntaxTree
8970
9161
 
8971
9162
  alias deconstruct child_nodes
8972
9163
 
8973
- def deconstruct_keys(keys)
9164
+ def deconstruct_keys(_keys)
8974
9165
  {
8975
9166
  statement: statement,
8976
9167
  predicate: predicate,
@@ -9009,13 +9200,15 @@ module SyntaxTree
9009
9200
  end
9010
9201
 
9011
9202
  q.group do
9012
- q.if_break { format_break(q) }.if_flat do
9013
- Parentheses.flat(q) do
9014
- q.format(statements)
9015
- q.text(" #{keyword} ")
9016
- q.format(node.predicate)
9203
+ q
9204
+ .if_break { format_break(q) }
9205
+ .if_flat do
9206
+ Parentheses.flat(q) do
9207
+ q.format(statements)
9208
+ q.text(" #{keyword} ")
9209
+ q.format(node.predicate)
9210
+ end
9017
9211
  end
9018
- end
9019
9212
  end
9020
9213
  end
9021
9214
 
@@ -9065,7 +9258,7 @@ module SyntaxTree
9065
9258
 
9066
9259
  alias deconstruct child_nodes
9067
9260
 
9068
- def deconstruct_keys(keys)
9261
+ def deconstruct_keys(_keys)
9069
9262
  {
9070
9263
  predicate: predicate,
9071
9264
  statements: statements,
@@ -9121,7 +9314,7 @@ module SyntaxTree
9121
9314
 
9122
9315
  alias deconstruct child_nodes
9123
9316
 
9124
- def deconstruct_keys(keys)
9317
+ def deconstruct_keys(_keys)
9125
9318
  {
9126
9319
  statement: statement,
9127
9320
  predicate: predicate,
@@ -9187,7 +9380,7 @@ module SyntaxTree
9187
9380
 
9188
9381
  alias deconstruct child_nodes
9189
9382
 
9190
- def deconstruct_keys(keys)
9383
+ def deconstruct_keys(_keys)
9191
9384
  { left: left, right: right, location: location, comments: comments }
9192
9385
  end
9193
9386
 
@@ -9230,7 +9423,7 @@ module SyntaxTree
9230
9423
 
9231
9424
  alias deconstruct child_nodes
9232
9425
 
9233
- def deconstruct_keys(keys)
9426
+ def deconstruct_keys(_keys)
9234
9427
  { value: value, location: location, comments: comments }
9235
9428
  end
9236
9429
 
@@ -9274,7 +9467,7 @@ module SyntaxTree
9274
9467
 
9275
9468
  alias deconstruct child_nodes
9276
9469
 
9277
- def deconstruct_keys(keys)
9470
+ def deconstruct_keys(_keys)
9278
9471
  { value: value, location: location, comments: comments }
9279
9472
  end
9280
9473
 
@@ -9315,7 +9508,7 @@ module SyntaxTree
9315
9508
 
9316
9509
  alias deconstruct child_nodes
9317
9510
 
9318
- def deconstruct_keys(keys)
9511
+ def deconstruct_keys(_keys)
9319
9512
  { value: value, location: location, comments: comments }
9320
9513
  end
9321
9514
 
@@ -9355,7 +9548,7 @@ module SyntaxTree
9355
9548
 
9356
9549
  alias deconstruct child_nodes
9357
9550
 
9358
- def deconstruct_keys(keys)
9551
+ def deconstruct_keys(_keys)
9359
9552
  { value: value, location: location, comments: comments }
9360
9553
  end
9361
9554
 
@@ -9390,7 +9583,7 @@ module SyntaxTree
9390
9583
 
9391
9584
  alias deconstruct child_nodes
9392
9585
 
9393
- def deconstruct_keys(keys)
9586
+ def deconstruct_keys(_keys)
9394
9587
  { location: location, comments: comments }
9395
9588
  end
9396
9589
 
@@ -9441,7 +9634,7 @@ module SyntaxTree
9441
9634
 
9442
9635
  alias deconstruct child_nodes
9443
9636
 
9444
- def deconstruct_keys(keys)
9637
+ def deconstruct_keys(_keys)
9445
9638
  {
9446
9639
  arguments: arguments,
9447
9640
  statements: statements,
@@ -9522,7 +9715,7 @@ module SyntaxTree
9522
9715
 
9523
9716
  alias deconstruct child_nodes
9524
9717
 
9525
- def deconstruct_keys(keys)
9718
+ def deconstruct_keys(_keys)
9526
9719
  {
9527
9720
  predicate: predicate,
9528
9721
  statements: statements,
@@ -9578,7 +9771,7 @@ module SyntaxTree
9578
9771
 
9579
9772
  alias deconstruct child_nodes
9580
9773
 
9581
- def deconstruct_keys(keys)
9774
+ def deconstruct_keys(_keys)
9582
9775
  {
9583
9776
  statement: statement,
9584
9777
  predicate: predicate,
@@ -9647,7 +9840,7 @@ module SyntaxTree
9647
9840
 
9648
9841
  alias deconstruct child_nodes
9649
9842
 
9650
- def deconstruct_keys(keys)
9843
+ def deconstruct_keys(_keys)
9651
9844
  { parts: parts, location: location, comments: comments }
9652
9845
  end
9653
9846
 
@@ -9687,7 +9880,7 @@ module SyntaxTree
9687
9880
 
9688
9881
  alias deconstruct child_nodes
9689
9882
 
9690
- def deconstruct_keys(keys)
9883
+ def deconstruct_keys(_keys)
9691
9884
  {
9692
9885
  beginning: beginning,
9693
9886
  elements: elements,
@@ -9743,7 +9936,7 @@ module SyntaxTree
9743
9936
 
9744
9937
  alias deconstruct child_nodes
9745
9938
 
9746
- def deconstruct_keys(keys)
9939
+ def deconstruct_keys(_keys)
9747
9940
  { value: value, location: location }
9748
9941
  end
9749
9942
  end
@@ -9772,7 +9965,7 @@ module SyntaxTree
9772
9965
 
9773
9966
  alias deconstruct child_nodes
9774
9967
 
9775
- def deconstruct_keys(keys)
9968
+ def deconstruct_keys(_keys)
9776
9969
  { parts: parts, location: location }
9777
9970
  end
9778
9971
  end
@@ -9805,7 +9998,7 @@ module SyntaxTree
9805
9998
 
9806
9999
  alias deconstruct child_nodes
9807
10000
 
9808
- def deconstruct_keys(keys)
10001
+ def deconstruct_keys(_keys)
9809
10002
  { parts: parts, location: location, comments: comments }
9810
10003
  end
9811
10004
 
@@ -9843,7 +10036,7 @@ module SyntaxTree
9843
10036
 
9844
10037
  alias deconstruct child_nodes
9845
10038
 
9846
- def deconstruct_keys(keys)
10039
+ def deconstruct_keys(_keys)
9847
10040
  { arguments: arguments, location: location, comments: comments }
9848
10041
  end
9849
10042
 
@@ -9893,7 +10086,7 @@ module SyntaxTree
9893
10086
 
9894
10087
  alias deconstruct child_nodes
9895
10088
 
9896
- def deconstruct_keys(keys)
10089
+ def deconstruct_keys(_keys)
9897
10090
  { value: value, location: location, comments: comments }
9898
10091
  end
9899
10092
 
@@ -9929,7 +10122,7 @@ module SyntaxTree
9929
10122
 
9930
10123
  alias deconstruct child_nodes
9931
10124
 
9932
- def deconstruct_keys(keys)
10125
+ def deconstruct_keys(_keys)
9933
10126
  { value: value, location: location, comments: comments }
9934
10127
  end
9935
10128