Class: Prism::IfNode

Inherits:
PrismNode
  • Object
show all
Defined in:
lib/prism/node.rb,
lib/prism/node_ext.rb,
lib/prism/parse_result/newlines.rb,
ext/prism/api_node.c

Overview

Represents the use of the ‘if` keyword, either in the block form or the modifier form, or a ternary expression.

bar if foo
^^^^^^^^^^

if foo then bar end
^^^^^^^^^^^^^^^^^^^

foo ? bar : baz
^^^^^^^^^^^^^^^

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, node_id, location, flags, if_keyword_loc, predicate, then_keyword_loc, statements, subsequent, end_keyword_loc) ⇒ IfNode

Initialize a new IfNode node.



7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
# File 'lib/prism/node.rb', line 7445

def initialize(source, node_id, location, flags, if_keyword_loc, predicate, then_keyword_loc, statements, subsequent, end_keyword_loc)
  @source = source
  @node_id = node_id
  @location = location
  @flags = flags
  @if_keyword_loc = if_keyword_loc
  @predicate = predicate
  @then_keyword_loc = then_keyword_loc
  @statements = statements
  @subsequent = subsequent
  @end_keyword_loc = end_keyword_loc
end

Instance Attribute Details

#predicateObject (readonly)

The node for the condition the ‘IfNode` is testing.

if foo
   ^^^
  bar
end

bar if foo
       ^^^

foo ? bar : baz
^^^


7525
7526
7527
# File 'lib/prism/node.rb', line 7525

def predicate
  @predicate
end

#statementsObject (readonly)

Represents the body of statements that will be executed when the predicate is evaluated as truthy. Will be ‘nil` when no body is provided.

if foo
  bar
  ^^^
  baz
  ^^^
end


7554
7555
7556
# File 'lib/prism/node.rb', line 7554

def statements
  @statements
end

#subsequentObject (readonly)

Represents an ‘ElseNode` or an `IfNode` when there is an `else` or an `elsif` in the `if` statement.

if foo
  bar
elsif baz
^^^^^^^^^
  qux
  ^^^
end
^^^

if foo then bar else baz end
                ^^^^^^^^^^^^


7569
7570
7571
# File 'lib/prism/node.rb', line 7569

def subsequent
  @subsequent
end

Class Method Details

.typeObject

Return a symbol representation of this node type. See ‘Node::type`.



7615
7616
7617
# File 'lib/prism/node.rb', line 7615

def self.type
  :if_node
end

Instance Method Details

#===(other) ⇒ Object

Implements case-equality for the node. This is effectively == but without comparing the value of locations. Locations are checked only for presence.



7621
7622
7623
7624
7625
7626
7627
7628
7629
# File 'lib/prism/node.rb', line 7621

def ===(other)
  other.is_a?(IfNode) &&
    (if_keyword_loc.nil? == other.if_keyword_loc.nil?) &&
    (predicate === other.predicate) &&
    (then_keyword_loc.nil? == other.then_keyword_loc.nil?) &&
    (statements === other.statements) &&
    (subsequent === other.subsequent) &&
    (end_keyword_loc.nil? == other.end_keyword_loc.nil?)
end

#accept(visitor) ⇒ Object

def accept: (Visitor visitor) -> void



7459
7460
7461
# File 'lib/prism/node.rb', line 7459

def accept(visitor)
  visitor.visit_if_node(self)
end

#child_nodesObject Also known as: deconstruct

def child_nodes: () -> Array[nil | Node]



7464
7465
7466
# File 'lib/prism/node.rb', line 7464

def child_nodes
  [predicate, statements, subsequent]
end

#comment_targetsObject

def comment_targets: () -> Array[Node | Location]



7478
7479
7480
# File 'lib/prism/node.rb', line 7478

def comment_targets
  [*if_keyword_loc, predicate, *then_keyword_loc, *statements, *subsequent, *end_keyword_loc] #: Array[Prism::node | Location]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



7469
7470
7471
7472
7473
7474
7475
# File 'lib/prism/node.rb', line 7469

def compact_child_nodes
  compact = [] #: Array[Prism::node]
  compact << predicate
  compact << statements if statements
  compact << subsequent if subsequent
  compact
end

#consequentObject

Returns the subsequent if/elsif/else clause of the if node. This method is deprecated in favor of #subsequent.



485
486
487
488
# File 'lib/prism/node_ext.rb', line 485

def consequent
  deprecated("subsequent")
  subsequent
end

#copy(node_id: self.node_id, location: self.location, flags: self.flags, if_keyword_loc: self.if_keyword_loc, predicate: self.predicate, then_keyword_loc: self.then_keyword_loc, statements: self.statements, subsequent: self.subsequent, end_keyword_loc: self.end_keyword_loc) ⇒ Object

def copy: (?node_id: Integer, ?location: Location, ?flags: Integer, ?if_keyword_loc: Location?, ?predicate: Prism::node, ?then_keyword_loc: Location?, ?statements: StatementsNode?, ?subsequent: ElseNode | IfNode | nil, ?end_keyword_loc: Location?) -> IfNode



7483
7484
7485
# File 'lib/prism/node.rb', line 7483

def copy(node_id: self.node_id, location: self.location, flags: self.flags, if_keyword_loc: self.if_keyword_loc, predicate: self.predicate, then_keyword_loc: self.then_keyword_loc, statements: self.statements, subsequent: self.subsequent, end_keyword_loc: self.end_keyword_loc)
  IfNode.new(source, node_id, location, flags, if_keyword_loc, predicate, then_keyword_loc, statements, subsequent, end_keyword_loc)
end

#deconstruct_keys(keys) ⇒ Object

def deconstruct_keys: (Array keys) -> { node_id: Integer, location: Location, if_keyword_loc: Location?, predicate: Prism::node, then_keyword_loc: Location?, statements: StatementsNode?, subsequent: ElseNode | IfNode | nil, end_keyword_loc: Location? }



7491
7492
7493
# File 'lib/prism/node.rb', line 7491

def deconstruct_keys(keys)
  { node_id: node_id, location: location, if_keyword_loc: if_keyword_loc, predicate: predicate, then_keyword_loc: then_keyword_loc, statements: statements, subsequent: subsequent, end_keyword_loc: end_keyword_loc }
end

#end_keywordObject

def end_keyword: () -> String?



7600
7601
7602
# File 'lib/prism/node.rb', line 7600

def end_keyword
  end_keyword_loc&.slice
end

#end_keyword_locObject

The location of the ‘end` keyword if present, `nil` otherwise.

if foo
  bar
end
^^^


7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
# File 'lib/prism/node.rb', line 7577

def end_keyword_loc
  location = @end_keyword_loc
  case location
  when nil
    nil
  when Location
    location
  else
    @end_keyword_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF)
  end
end

#if_keywordObject

def if_keyword: () -> String?



7590
7591
7592
# File 'lib/prism/node.rb', line 7590

def if_keyword
  if_keyword_loc&.slice
end

#if_keyword_locObject

The location of the ‘if` keyword if present.

bar if foo
    ^^

The ‘if_keyword_loc` field will be `nil` when the `IfNode` represents a ternary expression.



7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
# File 'lib/prism/node.rb', line 7501

def if_keyword_loc
  location = @if_keyword_loc
  case location
  when nil
    nil
  when Location
    location
  else
    @if_keyword_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF)
  end
end

#inspectObject

def inspect -> String



7605
7606
7607
# File 'lib/prism/node.rb', line 7605

def inspect
  InspectVisitor.compose(self)
end

#newline_flag!(lines) ⇒ Object

:nodoc:



91
92
93
# File 'lib/prism/parse_result/newlines.rb', line 91

def newline_flag!(lines) # :nodoc:
  predicate.newline_flag!(lines)
end

#then_keywordObject

def then_keyword: () -> String?



7595
7596
7597
# File 'lib/prism/node.rb', line 7595

def then_keyword
  then_keyword_loc&.slice
end

#then_keyword_locObject

The location of the ‘then` keyword (if present) or the `?` in a ternary expression, `nil` otherwise.

if foo then bar end
       ^^^^

a ? b : c
  ^


7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
# File 'lib/prism/node.rb', line 7534

def then_keyword_loc
  location = @then_keyword_loc
  case location
  when nil
    nil
  when Location
    location
  else
    @then_keyword_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF)
  end
end

#typeObject

Return a symbol representation of this node type. See ‘Node#type`.



7610
7611
7612
# File 'lib/prism/node.rb', line 7610

def type
  :if_node
end