@@ -4780,95 +4780,6 @@ def ===(other)
4780
4780
end
4781
4781
end
4782
4782
4783
- # Elsif represents another clause in an +if+ or +unless+ chain.
4784
- #
4785
- # if variable
4786
- # elsif other_variable
4787
- # end
4788
- #
4789
- class Elsif < Node
4790
- # [untyped] the expression to be checked
4791
- attr_reader :predicate
4792
-
4793
- # [Statements] the expressions to be executed
4794
- attr_reader :statements
4795
-
4796
- # [nil | Elsif | Else] the next clause in the chain
4797
- attr_reader :consequent
4798
-
4799
- # [Array[ Comment | EmbDoc ]] the comments attached to this node
4800
- attr_reader :comments
4801
-
4802
- def initialize ( predicate :, statements :, consequent :, location :)
4803
- @predicate = predicate
4804
- @statements = statements
4805
- @consequent = consequent
4806
- @location = location
4807
- @comments = [ ]
4808
- end
4809
-
4810
- def accept ( visitor )
4811
- visitor . visit_elsif ( self )
4812
- end
4813
-
4814
- def child_nodes
4815
- [ predicate , statements , consequent ]
4816
- end
4817
-
4818
- def copy ( predicate : nil , statements : nil , consequent : nil , location : nil )
4819
- node =
4820
- Elsif . new (
4821
- predicate : predicate || self . predicate ,
4822
- statements : statements || self . statements ,
4823
- consequent : consequent || self . consequent ,
4824
- location : location || self . location
4825
- )
4826
-
4827
- node . comments . concat ( comments . map ( &:copy ) )
4828
- node
4829
- end
4830
-
4831
- alias deconstruct child_nodes
4832
-
4833
- def deconstruct_keys ( _keys )
4834
- {
4835
- predicate : predicate ,
4836
- statements : statements ,
4837
- consequent : consequent ,
4838
- location : location ,
4839
- comments : comments
4840
- }
4841
- end
4842
-
4843
- def format ( q )
4844
- q . group do
4845
- q . group do
4846
- q . text ( "elsif " )
4847
- q . nest ( "elsif" . length - 1 ) { q . format ( predicate ) }
4848
- end
4849
-
4850
- unless statements . empty?
4851
- q . indent do
4852
- q . breakable_force
4853
- q . format ( statements )
4854
- end
4855
- end
4856
-
4857
- if consequent
4858
- q . group do
4859
- q . breakable_force
4860
- q . format ( consequent )
4861
- end
4862
- end
4863
- end
4864
- end
4865
-
4866
- def ===( other )
4867
- other . is_a? ( Elsif ) && predicate === other . predicate &&
4868
- statements === other . statements && consequent === other . consequent
4869
- end
4870
- end
4871
-
4872
4783
# EmbDoc represents a multi-line comment.
4873
4784
#
4874
4785
# =begin
@@ -6264,7 +6175,7 @@ def format(q)
6264
6175
# If we can transform this node into a ternary, then we're going to
6265
6176
# print a special version that uses the ternary operator if it fits on
6266
6177
# one line.
6267
- if Ternaryable . call ( q , node )
6178
+ if Ternaryable . call ( q , node ) && keyword != "elsif"
6268
6179
format_ternary ( q )
6269
6180
return
6270
6181
end
@@ -6273,7 +6184,7 @@ def format(q)
6273
6184
# case we can't know for certain that that assignment doesn't impact the
6274
6185
# statements inside the conditional) then we can't use the modifier form
6275
6186
# and we must use the block form.
6276
- if ContainsAssignment . call ( node . predicate )
6187
+ if keyword == "elsif" || ContainsAssignment . call ( node . predicate )
6277
6188
format_break ( q , force : true )
6278
6189
return
6279
6190
end
@@ -6322,8 +6233,10 @@ def format_break(q, force:)
6322
6233
q . format ( node . consequent )
6323
6234
end
6324
6235
6325
- force ? q . breakable_force : q . breakable_space
6326
- q . text ( "end" )
6236
+ unless keyword == "elsif"
6237
+ force ? q . breakable_force : q . breakable_space
6238
+ q . text ( "end" )
6239
+ end
6327
6240
end
6328
6241
6329
6242
def format_ternary ( q )
@@ -6384,7 +6297,7 @@ def contains_conditional?
6384
6297
end
6385
6298
end
6386
6299
6387
- # If represents the first clause in an +if+ chain.
6300
+ # If an +if+ or +elsif+ clause in an +if+ chain.
6388
6301
#
6389
6302
# if predicate
6390
6303
# end
@@ -6396,17 +6309,21 @@ class IfNode < Node
6396
6309
# [Statements] the expressions to be executed
6397
6310
attr_reader :statements
6398
6311
6399
- # [nil | Elsif | Else] the next clause in the chain
6312
+ # [nil | IfNode | Else] the next clause in the chain
6400
6313
attr_reader :consequent
6401
6314
6402
6315
# [Array[ Comment | EmbDoc ]] the comments attached to this node
6403
6316
attr_reader :comments
6404
6317
6405
- def initialize ( predicate :, statements :, consequent :, location :)
6318
+ # [ String ] the opening of the conditional statement
6319
+ attr_reader :beginning
6320
+
6321
+ def initialize ( predicate :, statements :, consequent :, location :, beginning :)
6406
6322
@predicate = predicate
6407
6323
@statements = statements
6408
6324
@consequent = consequent
6409
6325
@location = location
6326
+ @beginning = beginning
6410
6327
@comments = [ ]
6411
6328
end
6412
6329
@@ -6418,13 +6335,15 @@ def child_nodes
6418
6335
[ predicate , statements , consequent ]
6419
6336
end
6420
6337
6421
- def copy ( predicate : nil , statements : nil , consequent : nil , location : nil )
6338
+ def copy ( predicate : nil , statements : nil , consequent : nil , location : nil ,
6339
+ beginning : nil )
6422
6340
node =
6423
6341
IfNode . new (
6424
6342
predicate : predicate || self . predicate ,
6425
6343
statements : statements || self . statements ,
6426
6344
consequent : consequent || self . consequent ,
6427
- location : location || self . location
6345
+ location : location || self . location ,
6346
+ beginning : beginning || self . beginning ,
6428
6347
)
6429
6348
6430
6349
node . comments . concat ( comments . map ( &:copy ) )
@@ -6439,17 +6358,19 @@ def deconstruct_keys(_keys)
6439
6358
statements : statements ,
6440
6359
consequent : consequent ,
6441
6360
location : location ,
6361
+ beginning : beginning ,
6442
6362
comments : comments
6443
6363
}
6444
6364
end
6445
6365
6446
6366
def format ( q )
6447
- ConditionalFormatter . new ( "if" , self ) . format ( q )
6367
+ ConditionalFormatter . new ( beginning , self ) . format ( q )
6448
6368
end
6449
6369
6450
6370
def ===( other )
6451
6371
other . is_a? ( IfNode ) && predicate === other . predicate &&
6452
- statements === other . statements && consequent === other . consequent
6372
+ statements === other . statements && consequent === other . consequent &&
6373
+ beginning === other . beginning
6453
6374
end
6454
6375
6455
6376
# Checks if the node was originally found in the modifier form.
@@ -11208,7 +11129,7 @@ class UnlessNode < Node
11208
11129
# [Statements] the expressions to be executed
11209
11130
attr_reader :statements
11210
11131
11211
- # [nil | Elsif | Else] the next clause in the chain
11132
+ # [nil | IfNode | Else] the next clause in the chain
11212
11133
attr_reader :consequent
11213
11134
11214
11135
# [Array[ Comment | EmbDoc ]] the comments attached to this node
0 commit comments