@@ -4795,95 +4795,6 @@ def ===(other)
4795
4795
end
4796
4796
end
4797
4797
4798
- # Elsif represents another clause in an +if+ or +unless+ chain.
4799
- #
4800
- # if variable
4801
- # elsif other_variable
4802
- # end
4803
- #
4804
- class Elsif < Node
4805
- # [Node] the expression to be checked
4806
- attr_reader :predicate
4807
-
4808
- # [Statements] the expressions to be executed
4809
- attr_reader :statements
4810
-
4811
- # [nil | Elsif | Else] the next clause in the chain
4812
- attr_reader :consequent
4813
-
4814
- # [Array[ Comment | EmbDoc ]] the comments attached to this node
4815
- attr_reader :comments
4816
-
4817
- def initialize ( predicate :, statements :, consequent :, location :)
4818
- @predicate = predicate
4819
- @statements = statements
4820
- @consequent = consequent
4821
- @location = location
4822
- @comments = [ ]
4823
- end
4824
-
4825
- def accept ( visitor )
4826
- visitor . visit_elsif ( self )
4827
- end
4828
-
4829
- def child_nodes
4830
- [ predicate , statements , consequent ]
4831
- end
4832
-
4833
- def copy ( predicate : nil , statements : nil , consequent : nil , location : nil )
4834
- node =
4835
- Elsif . new (
4836
- predicate : predicate || self . predicate ,
4837
- statements : statements || self . statements ,
4838
- consequent : consequent || self . consequent ,
4839
- location : location || self . location
4840
- )
4841
-
4842
- node . comments . concat ( comments . map ( &:copy ) )
4843
- node
4844
- end
4845
-
4846
- alias deconstruct child_nodes
4847
-
4848
- def deconstruct_keys ( _keys )
4849
- {
4850
- predicate : predicate ,
4851
- statements : statements ,
4852
- consequent : consequent ,
4853
- location : location ,
4854
- comments : comments
4855
- }
4856
- end
4857
-
4858
- def format ( q )
4859
- q . group do
4860
- q . group do
4861
- q . text ( "elsif " )
4862
- q . nest ( "elsif" . length - 1 ) { q . format ( predicate ) }
4863
- end
4864
-
4865
- unless statements . empty?
4866
- q . indent do
4867
- q . breakable_force
4868
- q . format ( statements )
4869
- end
4870
- end
4871
-
4872
- if consequent
4873
- q . group do
4874
- q . breakable_force
4875
- q . format ( consequent )
4876
- end
4877
- end
4878
- end
4879
- end
4880
-
4881
- def ===( other )
4882
- other . is_a? ( Elsif ) && predicate === other . predicate &&
4883
- statements === other . statements && consequent === other . consequent
4884
- end
4885
- end
4886
-
4887
4798
# EmbDoc represents a multi-line comment.
4888
4799
#
4889
4800
# =begin
@@ -6279,7 +6190,7 @@ def format(q)
6279
6190
# If we can transform this node into a ternary, then we're going to
6280
6191
# print a special version that uses the ternary operator if it fits on
6281
6192
# one line.
6282
- if Ternaryable . call ( q , node )
6193
+ if Ternaryable . call ( q , node ) && keyword != "elsif"
6283
6194
format_ternary ( q )
6284
6195
return
6285
6196
end
@@ -6288,7 +6199,7 @@ def format(q)
6288
6199
# case we can't know for certain that that assignment doesn't impact the
6289
6200
# statements inside the conditional) then we can't use the modifier form
6290
6201
# and we must use the block form.
6291
- if ContainsAssignment . call ( node . predicate )
6202
+ if keyword == "elsif" || ContainsAssignment . call ( node . predicate )
6292
6203
format_break ( q , force : true )
6293
6204
return
6294
6205
end
@@ -6337,8 +6248,10 @@ def format_break(q, force:)
6337
6248
q . format ( node . consequent )
6338
6249
end
6339
6250
6340
- force ? q . breakable_force : q . breakable_space
6341
- q . text ( "end" )
6251
+ unless keyword == "elsif"
6252
+ force ? q . breakable_force : q . breakable_space
6253
+ q . text ( "end" )
6254
+ end
6342
6255
end
6343
6256
6344
6257
def format_ternary ( q )
@@ -6399,7 +6312,7 @@ def contains_conditional?
6399
6312
end
6400
6313
end
6401
6314
6402
- # If represents the first clause in an +if+ chain.
6315
+ # If an +if+ or +elsif+ clause in an +if+ chain.
6403
6316
#
6404
6317
# if predicate
6405
6318
# end
@@ -6411,17 +6324,21 @@ class IfNode < Node
6411
6324
# [Statements] the expressions to be executed
6412
6325
attr_reader :statements
6413
6326
6414
- # [nil | Elsif | Else] the next clause in the chain
6327
+ # [nil | IfNode | Else] the next clause in the chain
6415
6328
attr_reader :consequent
6416
6329
6417
6330
# [Array[ Comment | EmbDoc ]] the comments attached to this node
6418
6331
attr_reader :comments
6419
6332
6420
- def initialize ( predicate :, statements :, consequent :, location :)
6333
+ # [ String ] the opening of the conditional statement
6334
+ attr_reader :beginning
6335
+
6336
+ def initialize ( predicate :, statements :, consequent :, location :, beginning :)
6421
6337
@predicate = predicate
6422
6338
@statements = statements
6423
6339
@consequent = consequent
6424
6340
@location = location
6341
+ @beginning = beginning
6425
6342
@comments = [ ]
6426
6343
end
6427
6344
@@ -6433,13 +6350,15 @@ def child_nodes
6433
6350
[ predicate , statements , consequent ]
6434
6351
end
6435
6352
6436
- def copy ( predicate : nil , statements : nil , consequent : nil , location : nil )
6353
+ def copy ( predicate : nil , statements : nil , consequent : nil , location : nil ,
6354
+ beginning : nil )
6437
6355
node =
6438
6356
IfNode . new (
6439
6357
predicate : predicate || self . predicate ,
6440
6358
statements : statements || self . statements ,
6441
6359
consequent : consequent || self . consequent ,
6442
- location : location || self . location
6360
+ location : location || self . location ,
6361
+ beginning : beginning || self . beginning ,
6443
6362
)
6444
6363
6445
6364
node . comments . concat ( comments . map ( &:copy ) )
@@ -6454,17 +6373,19 @@ def deconstruct_keys(_keys)
6454
6373
statements : statements ,
6455
6374
consequent : consequent ,
6456
6375
location : location ,
6376
+ beginning : beginning ,
6457
6377
comments : comments
6458
6378
}
6459
6379
end
6460
6380
6461
6381
def format ( q )
6462
- ConditionalFormatter . new ( "if" , self ) . format ( q )
6382
+ ConditionalFormatter . new ( beginning , self ) . format ( q )
6463
6383
end
6464
6384
6465
6385
def ===( other )
6466
6386
other . is_a? ( IfNode ) && predicate === other . predicate &&
6467
- statements === other . statements && consequent === other . consequent
6387
+ statements === other . statements && consequent === other . consequent &&
6388
+ beginning === other . beginning
6468
6389
end
6469
6390
6470
6391
# Checks if the node was originally found in the modifier form.
@@ -11260,7 +11181,7 @@ class UnlessNode < Node
11260
11181
# [Statements] the expressions to be executed
11261
11182
attr_reader :statements
11262
11183
11263
- # [nil | Elsif | Else] the next clause in the chain
11184
+ # [nil | IfNode | Else] the next clause in the chain
11264
11185
attr_reader :consequent
11265
11186
11266
11187
# [Array[ Comment | EmbDoc ]] the comments attached to this node
0 commit comments