@@ -4835,95 +4835,6 @@ def ===(other)
4835
4835
end
4836
4836
end
4837
4837
4838
- # Elsif represents another clause in an +if+ or +unless+ chain.
4839
- #
4840
- # if variable
4841
- # elsif other_variable
4842
- # end
4843
- #
4844
- class Elsif < Node
4845
- # [Node] the expression to be checked
4846
- attr_reader :predicate
4847
-
4848
- # [Statements] the expressions to be executed
4849
- attr_reader :statements
4850
-
4851
- # [nil | Elsif | Else] the next clause in the chain
4852
- attr_reader :consequent
4853
-
4854
- # [Array[ Comment | EmbDoc ]] the comments attached to this node
4855
- attr_reader :comments
4856
-
4857
- def initialize ( predicate :, statements :, consequent :, location :)
4858
- @predicate = predicate
4859
- @statements = statements
4860
- @consequent = consequent
4861
- @location = location
4862
- @comments = [ ]
4863
- end
4864
-
4865
- def accept ( visitor )
4866
- visitor . visit_elsif ( self )
4867
- end
4868
-
4869
- def child_nodes
4870
- [ predicate , statements , consequent ]
4871
- end
4872
-
4873
- def copy ( predicate : nil , statements : nil , consequent : nil , location : nil )
4874
- node =
4875
- Elsif . new (
4876
- predicate : predicate || self . predicate ,
4877
- statements : statements || self . statements ,
4878
- consequent : consequent || self . consequent ,
4879
- location : location || self . location
4880
- )
4881
-
4882
- node . comments . concat ( comments . map ( &:copy ) )
4883
- node
4884
- end
4885
-
4886
- alias deconstruct child_nodes
4887
-
4888
- def deconstruct_keys ( _keys )
4889
- {
4890
- predicate : predicate ,
4891
- statements : statements ,
4892
- consequent : consequent ,
4893
- location : location ,
4894
- comments : comments
4895
- }
4896
- end
4897
-
4898
- def format ( q )
4899
- q . group do
4900
- q . group do
4901
- q . text ( "elsif " )
4902
- q . nest ( "elsif" . length - 1 ) { q . format ( predicate ) }
4903
- end
4904
-
4905
- unless statements . empty?
4906
- q . indent do
4907
- q . breakable_force
4908
- q . format ( statements )
4909
- end
4910
- end
4911
-
4912
- if consequent
4913
- q . group do
4914
- q . breakable_force
4915
- q . format ( consequent )
4916
- end
4917
- end
4918
- end
4919
- end
4920
-
4921
- def ===( other )
4922
- other . is_a? ( Elsif ) && predicate === other . predicate &&
4923
- statements === other . statements && consequent === other . consequent
4924
- end
4925
- end
4926
-
4927
4838
# EmbDoc represents a multi-line comment.
4928
4839
#
4929
4840
# =begin
@@ -6439,25 +6350,29 @@ def contains_conditional?
6439
6350
end
6440
6351
end
6441
6352
6442
- # If represents the first clause in an +if+ chain.
6353
+ # If an +if+ or +elsif+ clause in an +if+ chain.
6443
6354
#
6444
6355
# if predicate
6445
6356
# end
6446
6357
#
6447
6358
class IfNode < Node
6359
+ # [Kw] the opening keyword of the conditional statement
6360
+ attr_reader :keyword
6361
+
6448
6362
# [Node] the expression to be checked
6449
6363
attr_reader :predicate
6450
6364
6451
6365
# [Statements] the expressions to be executed
6452
6366
attr_reader :statements
6453
6367
6454
- # [nil | Elsif | Else] the next clause in the chain
6368
+ # [nil | IfNode | Else] the next clause in the chain
6455
6369
attr_reader :consequent
6456
6370
6457
6371
# [Array[ Comment | EmbDoc ]] the comments attached to this node
6458
6372
attr_reader :comments
6459
6373
6460
- def initialize ( predicate :, statements :, consequent :, location :)
6374
+ def initialize ( keyword :, predicate :, statements :, consequent :, location :)
6375
+ @keyword = keyword
6461
6376
@predicate = predicate
6462
6377
@statements = statements
6463
6378
@consequent = consequent
@@ -6473,9 +6388,16 @@ def child_nodes
6473
6388
[ predicate , statements , consequent ]
6474
6389
end
6475
6390
6476
- def copy ( predicate : nil , statements : nil , consequent : nil , location : nil )
6391
+ def copy (
6392
+ keyword : nil ,
6393
+ predicate : nil ,
6394
+ statements : nil ,
6395
+ consequent : nil ,
6396
+ location : nil
6397
+ )
6477
6398
node =
6478
6399
IfNode . new (
6400
+ keyword : keyword || self . keyword ,
6479
6401
predicate : predicate || self . predicate ,
6480
6402
statements : statements || self . statements ,
6481
6403
consequent : consequent || self . consequent ,
@@ -6494,17 +6416,42 @@ def deconstruct_keys(_keys)
6494
6416
statements : statements ,
6495
6417
consequent : consequent ,
6496
6418
location : location ,
6419
+ keyword : keyword ,
6497
6420
comments : comments
6498
6421
}
6499
6422
end
6500
6423
6501
6424
def format ( q )
6502
- ConditionalFormatter . new ( "if" , self ) . format ( q )
6425
+ if keyword . value == "elsif"
6426
+ q . group do
6427
+ q . group do
6428
+ q . text ( "elsif " )
6429
+ q . nest ( "elsif" . length - 1 ) { q . format ( predicate ) }
6430
+ end
6431
+
6432
+ unless statements . empty?
6433
+ q . indent do
6434
+ q . breakable_force
6435
+ q . format ( statements )
6436
+ end
6437
+ end
6438
+
6439
+ if consequent
6440
+ q . group do
6441
+ q . breakable_force
6442
+ q . format ( consequent )
6443
+ end
6444
+ end
6445
+ end
6446
+ else
6447
+ ConditionalFormatter . new ( keyword . value , self ) . format ( q )
6448
+ end
6503
6449
end
6504
6450
6505
6451
def ===( other )
6506
6452
other . is_a? ( IfNode ) && predicate === other . predicate &&
6507
- statements === other . statements && consequent === other . consequent
6453
+ statements === other . statements && consequent === other . consequent &&
6454
+ keyword === other . keyword
6508
6455
end
6509
6456
6510
6457
# Checks if the node was originally found in the modifier form.
@@ -11295,7 +11242,7 @@ class UnlessNode < Node
11295
11242
# [Statements] the expressions to be executed
11296
11243
attr_reader :statements
11297
11244
11298
- # [nil | Elsif | Else] the next clause in the chain
11245
+ # [nil | IfNode | Else] the next clause in the chain
11299
11246
attr_reader :consequent
11300
11247
11301
11248
# [Array[ Comment | EmbDoc ]] the comments attached to this node
0 commit comments