@@ -4853,95 +4853,6 @@ def ===(other)
48534853 end
48544854 end
48554855
4856- # Elsif represents another clause in an +if+ or +unless+ chain.
4857- #
4858- # if variable
4859- # elsif other_variable
4860- # end
4861- #
4862- class Elsif < Node
4863- # [Node] the expression to be checked
4864- attr_reader :predicate
4865-
4866- # [Statements] the expressions to be executed
4867- attr_reader :statements
4868-
4869- # [nil | Elsif | Else] the next clause in the chain
4870- attr_reader :consequent
4871-
4872- # [Array[ Comment | EmbDoc ]] the comments attached to this node
4873- attr_reader :comments
4874-
4875- def initialize ( predicate :, statements :, consequent :, location :)
4876- @predicate = predicate
4877- @statements = statements
4878- @consequent = consequent
4879- @location = location
4880- @comments = [ ]
4881- end
4882-
4883- def accept ( visitor )
4884- visitor . visit_elsif ( self )
4885- end
4886-
4887- def child_nodes
4888- [ predicate , statements , consequent ]
4889- end
4890-
4891- def copy ( predicate : nil , statements : nil , consequent : nil , location : nil )
4892- node =
4893- Elsif . new (
4894- predicate : predicate || self . predicate ,
4895- statements : statements || self . statements ,
4896- consequent : consequent || self . consequent ,
4897- location : location || self . location
4898- )
4899-
4900- node . comments . concat ( comments . map ( &:copy ) )
4901- node
4902- end
4903-
4904- alias deconstruct child_nodes
4905-
4906- def deconstruct_keys ( _keys )
4907- {
4908- predicate : predicate ,
4909- statements : statements ,
4910- consequent : consequent ,
4911- location : location ,
4912- comments : comments
4913- }
4914- end
4915-
4916- def format ( q )
4917- q . group do
4918- q . group do
4919- q . text ( "elsif " )
4920- q . nest ( "elsif" . length - 1 ) { q . format ( predicate ) }
4921- end
4922-
4923- unless statements . empty?
4924- q . indent do
4925- q . breakable_force
4926- q . format ( statements )
4927- end
4928- end
4929-
4930- if consequent
4931- q . group do
4932- q . breakable_force
4933- q . format ( consequent )
4934- end
4935- end
4936- end
4937- end
4938-
4939- def ===( other )
4940- other . is_a? ( Elsif ) && predicate === other . predicate &&
4941- statements === other . statements && consequent === other . consequent
4942- end
4943- end
4944-
49454856 # EmbDoc represents a multi-line comment.
49464857 #
49474858 # =begin
@@ -6460,25 +6371,29 @@ def contains_conditional?
64606371 end
64616372 end
64626373
6463- # If represents the first clause in an +if+ chain.
6374+ # If an +if+ or +elsif+ clause in an +if+ chain.
64646375 #
64656376 # if predicate
64666377 # end
64676378 #
64686379 class IfNode < Node
6380+ # [Kw] the opening keyword of the conditional statement
6381+ attr_reader :keyword
6382+
64696383 # [Node] the expression to be checked
64706384 attr_reader :predicate
64716385
64726386 # [Statements] the expressions to be executed
64736387 attr_reader :statements
64746388
6475- # [nil | Elsif | Else] the next clause in the chain
6389+ # [nil | IfNode | Else] the next clause in the chain
64766390 attr_reader :consequent
64776391
64786392 # [Array[ Comment | EmbDoc ]] the comments attached to this node
64796393 attr_reader :comments
64806394
6481- def initialize ( predicate :, statements :, consequent :, location :)
6395+ def initialize ( keyword :, predicate :, statements :, consequent :, location :)
6396+ @keyword = keyword
64826397 @predicate = predicate
64836398 @statements = statements
64846399 @consequent = consequent
@@ -6494,9 +6409,16 @@ def child_nodes
64946409 [ predicate , statements , consequent ]
64956410 end
64966411
6497- def copy ( predicate : nil , statements : nil , consequent : nil , location : nil )
6412+ def copy (
6413+ keyword : nil ,
6414+ predicate : nil ,
6415+ statements : nil ,
6416+ consequent : nil ,
6417+ location : nil
6418+ )
64986419 node =
64996420 IfNode . new (
6421+ keyword : keyword || self . keyword ,
65006422 predicate : predicate || self . predicate ,
65016423 statements : statements || self . statements ,
65026424 consequent : consequent || self . consequent ,
@@ -6515,17 +6437,42 @@ def deconstruct_keys(_keys)
65156437 statements : statements ,
65166438 consequent : consequent ,
65176439 location : location ,
6440+ keyword : keyword ,
65186441 comments : comments
65196442 }
65206443 end
65216444
65226445 def format ( q )
6523- ConditionalFormatter . new ( "if" , self ) . format ( q )
6446+ if keyword . value == "elsif"
6447+ q . group do
6448+ q . group do
6449+ q . text ( "elsif " )
6450+ q . nest ( "elsif" . length - 1 ) { q . format ( predicate ) }
6451+ end
6452+
6453+ unless statements . empty?
6454+ q . indent do
6455+ q . breakable_force
6456+ q . format ( statements )
6457+ end
6458+ end
6459+
6460+ if consequent
6461+ q . group do
6462+ q . breakable_force
6463+ q . format ( consequent )
6464+ end
6465+ end
6466+ end
6467+ else
6468+ ConditionalFormatter . new ( keyword . value , self ) . format ( q )
6469+ end
65246470 end
65256471
65266472 def ===( other )
65276473 other . is_a? ( IfNode ) && predicate === other . predicate &&
6528- statements === other . statements && consequent === other . consequent
6474+ statements === other . statements && consequent === other . consequent &&
6475+ keyword === other . keyword
65296476 end
65306477
65316478 # Checks if the node was originally found in the modifier form.
@@ -11328,7 +11275,7 @@ class UnlessNode < Node
1132811275 # [Statements] the expressions to be executed
1132911276 attr_reader :statements
1133011277
11331- # [nil | Elsif | Else] the next clause in the chain
11278+ # [nil | IfNode | Else] the next clause in the chain
1133211279 attr_reader :consequent
1133311280
1133411281 # [Array[ Comment | EmbDoc ]] the comments attached to this node
0 commit comments