@@ -792,9 +792,10 @@ def arity
792
792
private
793
793
794
794
def trailing_comma?
795
+ arguments = self . arguments
795
796
return false unless arguments . is_a? ( Args )
796
- parts = arguments . parts
797
797
798
+ parts = arguments . parts
798
799
if parts . last . is_a? ( ArgBlock )
799
800
# If the last argument is a block, then we can't put a trailing comma
800
801
# after it without resulting in a syntax error.
@@ -1188,8 +1189,11 @@ def deconstruct_keys(_keys)
1188
1189
end
1189
1190
1190
1191
def format ( q )
1191
- if lbracket . comments . empty? && contents && contents . comments . empty? &&
1192
- contents . parts . length > 1
1192
+ lbracket = self . lbracket
1193
+ contents = self . contents
1194
+
1195
+ if lbracket . is_a? ( LBracket ) && lbracket . comments . empty? && contents &&
1196
+ contents . comments . empty? && contents . parts . length > 1
1193
1197
if qwords?
1194
1198
QWordsFormatter . new ( contents ) . format ( q )
1195
1199
return
@@ -2091,6 +2095,7 @@ def deconstruct_keys(_keys)
2091
2095
end
2092
2096
2093
2097
def format ( q )
2098
+ left = self . left
2094
2099
power = operator == :**
2095
2100
2096
2101
q . group do
@@ -2307,6 +2312,8 @@ def initialize(
2307
2312
end
2308
2313
2309
2314
def bind ( parser , start_char , start_column , end_char , end_column )
2315
+ rescue_clause = self . rescue_clause
2316
+
2310
2317
@location =
2311
2318
Location . new (
2312
2319
start_line : location . start_line ,
@@ -2330,6 +2337,7 @@ def bind(parser, start_char, start_column, end_char, end_column)
2330
2337
# Next we're going to determine the rescue clause if there is one
2331
2338
if rescue_clause
2332
2339
consequent = else_clause || ensure_clause
2340
+
2333
2341
rescue_clause . bind_end (
2334
2342
consequent ? consequent . location . start_char : end_char ,
2335
2343
consequent ? consequent . location . start_column : end_column
@@ -2735,7 +2743,7 @@ def format(q)
2735
2743
children << receiver
2736
2744
end
2737
2745
when MethodAddBlock
2738
- if receiver . call . is_a? ( CallNode ) && !receiver . call . receiver . nil?
2746
+ if ( call = receiver . call ) . is_a? ( CallNode ) && !call . receiver . nil?
2739
2747
children << receiver
2740
2748
else
2741
2749
break
@@ -2744,8 +2752,8 @@ def format(q)
2744
2752
break
2745
2753
end
2746
2754
when MethodAddBlock
2747
- if child . call . is_a? ( CallNode ) && !child . call . receiver . nil?
2748
- children << child . call
2755
+ if ( call = child . call ) . is_a? ( CallNode ) && !call . receiver . nil?
2756
+ children << call
2749
2757
else
2750
2758
break
2751
2759
end
@@ -2767,8 +2775,8 @@ def format(q)
2767
2775
# of just Statements nodes.
2768
2776
parent = parents [ 3 ] if parent . is_a? ( BlockNode ) && parent . keywords?
2769
2777
2770
- if parent . is_a? ( MethodAddBlock ) && parent . call . is_a? ( CallNode ) &&
2771
- parent . call . message . value == "sig"
2778
+ if parent . is_a? ( MethodAddBlock ) &&
2779
+ ( call = parent . call ) . is_a? ( CallNode ) && call . message . value == "sig"
2772
2780
threshold = 2
2773
2781
end
2774
2782
end
@@ -2813,10 +2821,10 @@ def format_chain(q, children)
2813
2821
2814
2822
while ( child = children . pop )
2815
2823
if child . is_a? ( CallNode )
2816
- if child . receiver . is_a? ( CallNode ) &&
2817
- ( child . receiver . message != :call ) &&
2818
- ( child . receiver . message . value == "where" ) &&
2819
- ( child . message . value == "not" )
2824
+ if ( receiver = child . receiver ) . is_a? ( CallNode ) &&
2825
+ ( receiver . message != :call ) &&
2826
+ ( receiver . message . value == "where" ) &&
2827
+ ( message . value == "not" )
2820
2828
# This is very specialized behavior wherein we group
2821
2829
# .where.not calls together because it looks better. For more
2822
2830
# information, see
@@ -2872,7 +2880,8 @@ def self.chained?(node)
2872
2880
when CallNode
2873
2881
!node . receiver . nil?
2874
2882
when MethodAddBlock
2875
- node . call . is_a? ( CallNode ) && !node . call . receiver . nil?
2883
+ call = node . call
2884
+ call . is_a? ( CallNode ) && !call . receiver . nil?
2876
2885
else
2877
2886
false
2878
2887
end
@@ -3629,6 +3638,10 @@ def deconstruct_keys(_keys)
3629
3638
end
3630
3639
3631
3640
def format ( q )
3641
+ message = self . message
3642
+ arguments = self . arguments
3643
+ block = self . block
3644
+
3632
3645
q . group do
3633
3646
doc =
3634
3647
q . nest ( 0 ) do
@@ -3637,7 +3650,7 @@ def format(q)
3637
3650
# If there are leading comments on the message then we know we have
3638
3651
# a newline in the source that is forcing these things apart. In
3639
3652
# this case we will have to use a trailing operator.
3640
- if message . comments . any? ( &:leading? )
3653
+ if message != :call && message . comments . any? ( &:leading? )
3641
3654
q . format ( CallOperatorFormatter . new ( operator ) , stackable : false )
3642
3655
q . indent do
3643
3656
q . breakable_empty
@@ -4153,6 +4166,9 @@ def deconstruct_keys(_keys)
4153
4166
end
4154
4167
4155
4168
def format ( q )
4169
+ params = self . params
4170
+ bodystmt = self . bodystmt
4171
+
4156
4172
q . group do
4157
4173
q . group do
4158
4174
q . text ( "def" )
@@ -4209,6 +4225,8 @@ def endless?
4209
4225
end
4210
4226
4211
4227
def arity
4228
+ params = self . params
4229
+
4212
4230
case params
4213
4231
when Params
4214
4232
params . arity
@@ -5293,6 +5311,7 @@ def accept(visitor)
5293
5311
end
5294
5312
5295
5313
def child_nodes
5314
+ operator = self . operator
5296
5315
[ parent , ( operator if operator != :"::" ) , name ]
5297
5316
end
5298
5317
@@ -5674,7 +5693,7 @@ def accept(visitor)
5674
5693
end
5675
5694
5676
5695
def child_nodes
5677
- [ lbrace ] + assocs
5696
+ [ lbrace ] . concat ( assocs )
5678
5697
end
5679
5698
5680
5699
def copy ( lbrace : nil , assocs : nil , location : nil )
@@ -5766,7 +5785,7 @@ class Heredoc < Node
5766
5785
# [Array[ Comment | EmbDoc ]] the comments attached to this node
5767
5786
attr_reader :comments
5768
5787
5769
- def initialize ( beginning :, ending : nil , dedent : 0 , parts : [ ] , location : )
5788
+ def initialize ( beginning :, location : , ending : nil , dedent : 0 , parts : [ ] )
5770
5789
@beginning = beginning
5771
5790
@ending = ending
5772
5791
@dedent = dedent
@@ -6134,6 +6153,8 @@ def ===(other)
6134
6153
private
6135
6154
6136
6155
def format_contents ( q , parts , nested )
6156
+ keyword_rest = self . keyword_rest
6157
+
6137
6158
q . group { q . seplist ( parts ) { |part | q . format ( part , stackable : false ) } }
6138
6159
6139
6160
# If there isn't a constant, and there's a blank keyword_rest, then we
@@ -6763,6 +6784,8 @@ def deconstruct_keys(_keys)
6763
6784
6764
6785
def format ( q )
6765
6786
keyword = "in "
6787
+ pattern = self . pattern
6788
+ consequent = self . consequent
6766
6789
6767
6790
q . group do
6768
6791
q . text ( keyword )
@@ -7165,6 +7188,8 @@ def deconstruct_keys(_keys)
7165
7188
end
7166
7189
7167
7190
def format ( q )
7191
+ params = self . params
7192
+
7168
7193
q . text ( "->" )
7169
7194
q . group do
7170
7195
if params . is_a? ( Paren )
@@ -7643,7 +7668,7 @@ class MLHS < Node
7643
7668
# [Array[ Comment | EmbDoc ]] the comments attached to this node
7644
7669
attr_reader :comments
7645
7670
7646
- def initialize ( parts :, comma : false , location : )
7671
+ def initialize ( parts :, location : , comma : false )
7647
7672
@parts = parts
7648
7673
@comma = comma
7649
7674
@location = location
@@ -7704,7 +7729,7 @@ class MLHSParen < Node
7704
7729
# [Array[ Comment | EmbDoc ]] the comments attached to this node
7705
7730
attr_reader :comments
7706
7731
7707
- def initialize ( contents :, comma : false , location : )
7732
+ def initialize ( contents :, location : , comma : false )
7708
7733
@contents = contents
7709
7734
@comma = comma
7710
7735
@location = location
@@ -8287,14 +8312,14 @@ def format(q)
8287
8312
attr_reader :comments
8288
8313
8289
8314
def initialize (
8315
+ location :,
8290
8316
requireds : [ ] ,
8291
8317
optionals : [ ] ,
8292
8318
rest : nil ,
8293
8319
posts : [ ] ,
8294
8320
keywords : [ ] ,
8295
8321
keyword_rest : nil ,
8296
- block : nil ,
8297
- location :
8322
+ block : nil
8298
8323
)
8299
8324
@requireds = requireds
8300
8325
@optionals = optionals
@@ -8321,6 +8346,8 @@ def accept(visitor)
8321
8346
end
8322
8347
8323
8348
def child_nodes
8349
+ keyword_rest = self . keyword_rest
8350
+
8324
8351
[
8325
8352
*requireds ,
8326
8353
*optionals . flatten ( 1 ) ,
@@ -8375,16 +8402,19 @@ def deconstruct_keys(_keys)
8375
8402
end
8376
8403
8377
8404
def format ( q )
8405
+ rest = self . rest
8406
+ keyword_rest = self . keyword_rest
8407
+
8378
8408
parts = [
8379
8409
*requireds ,
8380
8410
*optionals . map { |( name , value ) | OptionalFormatter . new ( name , value ) }
8381
8411
]
8382
8412
8383
8413
parts << rest if rest && !rest . is_a? ( ExcessedComma )
8384
- parts += [
8385
- * posts ,
8386
- * keywords . map { |( name , value ) | KeywordFormatter . new ( name , value ) }
8387
- ]
8414
+ parts . concat ( posts )
8415
+ parts . concat (
8416
+ keywords . map { |( name , value ) | KeywordFormatter . new ( name , value ) }
8417
+ )
8388
8418
8389
8419
parts << KeywordRestFormatter . new ( keyword_rest ) if keyword_rest
8390
8420
parts << block if block
@@ -8511,6 +8541,8 @@ def deconstruct_keys(_keys)
8511
8541
end
8512
8542
8513
8543
def format ( q )
8544
+ contents = self . contents
8545
+
8514
8546
q . group do
8515
8547
q . format ( lparen )
8516
8548
@@ -9425,11 +9457,11 @@ def bind_end(end_char, end_column)
9425
9457
end_column : end_column
9426
9458
)
9427
9459
9428
- if consequent
9429
- consequent . bind_end ( end_char , end_column )
9460
+ if ( next_node = consequent )
9461
+ next_node . bind_end ( end_char , end_column )
9430
9462
statements . bind_end (
9431
- consequent . location . start_char ,
9432
- consequent . location . start_column
9463
+ next_node . location . start_char ,
9464
+ next_node . location . start_column
9433
9465
)
9434
9466
else
9435
9467
statements . bind_end ( end_char , end_column )
@@ -9872,8 +9904,8 @@ def bind(parser, start_char, start_column, end_char, end_column)
9872
9904
end_column : end_column
9873
9905
)
9874
9906
9875
- if body [ 0 ] . is_a? ( VoidStmt )
9876
- location = body [ 0 ] . location
9907
+ if ( void_stmt = body [ 0 ] ) . is_a? ( VoidStmt )
9908
+ location = void_stmt . location
9877
9909
location =
9878
9910
Location . new (
9879
9911
start_line : location . start_line ,
@@ -10352,7 +10384,7 @@ def format(q)
10352
10384
opening_quote , closing_quote =
10353
10385
if !Quotes . locked? ( self , q . quote )
10354
10386
[ q . quote , q . quote ]
10355
- elsif quote . start_with? ( "%" )
10387
+ elsif quote & .start_with? ( "%" )
10356
10388
[ quote , Quotes . matching ( quote [ /%[qQ]?(.)/ , 1 ] ) ]
10357
10389
else
10358
10390
[ quote , quote ]
@@ -11521,7 +11553,7 @@ def accept(visitor)
11521
11553
end
11522
11554
11523
11555
def child_nodes
11524
- [ value ]
11556
+ value == :nil ? [ ] : [ value ]
11525
11557
end
11526
11558
11527
11559
def copy ( value : nil , location : nil )
0 commit comments