@@ -800,7 +800,7 @@ def trailing_comma?
800
800
# after it without resulting in a syntax error.
801
801
false
802
802
elsif ( parts . length == 1 ) && ( part = parts . first ) &&
803
- ( part . is_a? ( Command ) || part . is_a? ( CommandCall ) )
803
+ ( part . is_a? ( Command ) || part . is_a? ( CommandCall ) )
804
804
# If the only argument is a command or command call, then a trailing
805
805
# comma would be parsed as part of that expression instead of on this
806
806
# one, so we don't want to add a trailing comma.
@@ -4804,95 +4804,6 @@ def ===(other)
4804
4804
end
4805
4805
end
4806
4806
4807
- # Elsif represents another clause in an +if+ or +unless+ chain.
4808
- #
4809
- # if variable
4810
- # elsif other_variable
4811
- # end
4812
- #
4813
- class Elsif < Node
4814
- # [Node] the expression to be checked
4815
- attr_reader :predicate
4816
-
4817
- # [Statements] the expressions to be executed
4818
- attr_reader :statements
4819
-
4820
- # [nil | Elsif | Else] the next clause in the chain
4821
- attr_reader :consequent
4822
-
4823
- # [Array[ Comment | EmbDoc ]] the comments attached to this node
4824
- attr_reader :comments
4825
-
4826
- def initialize ( predicate :, statements :, consequent :, location :)
4827
- @predicate = predicate
4828
- @statements = statements
4829
- @consequent = consequent
4830
- @location = location
4831
- @comments = [ ]
4832
- end
4833
-
4834
- def accept ( visitor )
4835
- visitor . visit_elsif ( self )
4836
- end
4837
-
4838
- def child_nodes
4839
- [ predicate , statements , consequent ]
4840
- end
4841
-
4842
- def copy ( predicate : nil , statements : nil , consequent : nil , location : nil )
4843
- node =
4844
- Elsif . new (
4845
- predicate : predicate || self . predicate ,
4846
- statements : statements || self . statements ,
4847
- consequent : consequent || self . consequent ,
4848
- location : location || self . location
4849
- )
4850
-
4851
- node . comments . concat ( comments . map ( &:copy ) )
4852
- node
4853
- end
4854
-
4855
- alias deconstruct child_nodes
4856
-
4857
- def deconstruct_keys ( _keys )
4858
- {
4859
- predicate : predicate ,
4860
- statements : statements ,
4861
- consequent : consequent ,
4862
- location : location ,
4863
- comments : comments
4864
- }
4865
- end
4866
-
4867
- def format ( q )
4868
- q . group do
4869
- q . group do
4870
- q . text ( "elsif " )
4871
- q . nest ( "elsif" . length - 1 ) { q . format ( predicate ) }
4872
- end
4873
-
4874
- unless statements . empty?
4875
- q . indent do
4876
- q . breakable_force
4877
- q . format ( statements )
4878
- end
4879
- end
4880
-
4881
- if consequent
4882
- q . group do
4883
- q . breakable_force
4884
- q . format ( consequent )
4885
- end
4886
- end
4887
- end
4888
- end
4889
-
4890
- def ===( other )
4891
- other . is_a? ( Elsif ) && predicate === other . predicate &&
4892
- statements === other . statements && consequent === other . consequent
4893
- end
4894
- end
4895
-
4896
4807
# EmbDoc represents a multi-line comment.
4897
4808
#
4898
4809
# =begin
@@ -6288,7 +6199,7 @@ def format(q)
6288
6199
# If we can transform this node into a ternary, then we're going to
6289
6200
# print a special version that uses the ternary operator if it fits on
6290
6201
# one line.
6291
- if Ternaryable . call ( q , node )
6202
+ if Ternaryable . call ( q , node ) && keyword != "elsif"
6292
6203
format_ternary ( q )
6293
6204
return
6294
6205
end
@@ -6297,7 +6208,7 @@ def format(q)
6297
6208
# case we can't know for certain that that assignment doesn't impact the
6298
6209
# statements inside the conditional) then we can't use the modifier form
6299
6210
# and we must use the block form.
6300
- if ContainsAssignment . call ( node . predicate )
6211
+ if keyword == "elsif" || ContainsAssignment . call ( node . predicate )
6301
6212
format_break ( q , force : true )
6302
6213
return
6303
6214
end
@@ -6346,8 +6257,10 @@ def format_break(q, force:)
6346
6257
q . format ( node . consequent )
6347
6258
end
6348
6259
6349
- force ? q . breakable_force : q . breakable_space
6350
- q . text ( "end" )
6260
+ unless keyword == "elsif"
6261
+ force ? q . breakable_force : q . breakable_space
6262
+ q . text ( "end" )
6263
+ end
6351
6264
end
6352
6265
6353
6266
def format_ternary ( q )
@@ -6408,7 +6321,7 @@ def contains_conditional?
6408
6321
end
6409
6322
end
6410
6323
6411
- # If represents the first clause in an +if+ chain.
6324
+ # If an +if+ or +elsif+ clause in an +if+ chain.
6412
6325
#
6413
6326
# if predicate
6414
6327
# end
@@ -6420,17 +6333,21 @@ class IfNode < Node
6420
6333
# [Statements] the expressions to be executed
6421
6334
attr_reader :statements
6422
6335
6423
- # [nil | Elsif | Else] the next clause in the chain
6336
+ # [nil | IfNode | Else] the next clause in the chain
6424
6337
attr_reader :consequent
6425
6338
6426
6339
# [Array[ Comment | EmbDoc ]] the comments attached to this node
6427
6340
attr_reader :comments
6428
6341
6429
- def initialize ( predicate :, statements :, consequent :, location :)
6342
+ # [ String ] the opening of the conditional statement
6343
+ attr_reader :beginning
6344
+
6345
+ def initialize ( predicate :, statements :, consequent :, location :, beginning :)
6430
6346
@predicate = predicate
6431
6347
@statements = statements
6432
6348
@consequent = consequent
6433
6349
@location = location
6350
+ @beginning = beginning
6434
6351
@comments = [ ]
6435
6352
end
6436
6353
@@ -6442,13 +6359,20 @@ def child_nodes
6442
6359
[ predicate , statements , consequent ]
6443
6360
end
6444
6361
6445
- def copy ( predicate : nil , statements : nil , consequent : nil , location : nil )
6362
+ def copy (
6363
+ predicate : nil ,
6364
+ statements : nil ,
6365
+ consequent : nil ,
6366
+ location : nil ,
6367
+ beginning : nil
6368
+ )
6446
6369
node =
6447
6370
IfNode . new (
6448
6371
predicate : predicate || self . predicate ,
6449
6372
statements : statements || self . statements ,
6450
6373
consequent : consequent || self . consequent ,
6451
- location : location || self . location
6374
+ location : location || self . location ,
6375
+ beginning : beginning || self . beginning
6452
6376
)
6453
6377
6454
6378
node . comments . concat ( comments . map ( &:copy ) )
@@ -6463,17 +6387,19 @@ def deconstruct_keys(_keys)
6463
6387
statements : statements ,
6464
6388
consequent : consequent ,
6465
6389
location : location ,
6390
+ beginning : beginning ,
6466
6391
comments : comments
6467
6392
}
6468
6393
end
6469
6394
6470
6395
def format ( q )
6471
- ConditionalFormatter . new ( "if" , self ) . format ( q )
6396
+ ConditionalFormatter . new ( beginning , self ) . format ( q )
6472
6397
end
6473
6398
6474
6399
def ===( other )
6475
6400
other . is_a? ( IfNode ) && predicate === other . predicate &&
6476
- statements === other . statements && consequent === other . consequent
6401
+ statements === other . statements && consequent === other . consequent &&
6402
+ beginning === other . beginning
6477
6403
end
6478
6404
6479
6405
# Checks if the node was originally found in the modifier form.
@@ -9929,7 +9855,7 @@ def format(q)
9929
9855
q . breakable_force
9930
9856
q . format ( statement )
9931
9857
elsif ( statement . is_a? ( VCall ) && statement . access_control? ) ||
9932
- ( previous . is_a? ( VCall ) && previous . access_control? )
9858
+ ( previous . is_a? ( VCall ) && previous . access_control? )
9933
9859
q . breakable_force
9934
9860
q . breakable_force
9935
9861
q . format ( statement )
@@ -11264,7 +11190,7 @@ class UnlessNode < Node
11264
11190
# [Statements] the expressions to be executed
11265
11191
attr_reader :statements
11266
11192
11267
- # [nil | Elsif | Else] the next clause in the chain
11193
+ # [nil | IfNode | Else] the next clause in the chain
11268
11194
attr_reader :consequent
11269
11195
11270
11196
# [Array[ Comment | EmbDoc ]] the comments attached to this node
0 commit comments