Class: Prism::IfNode
- Inherits:
-
PrismNode
- Object
- PrismNode
- Prism::IfNode
- 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
-
#predicate ⇒ Object
readonly
The node for the condition the ‘IfNode` is testing.
-
#statements ⇒ Object
readonly
Represents the body of statements that will be executed when the predicate is evaluated as truthy.
-
#subsequent ⇒ Object
readonly
Represents an ‘ElseNode` or an `IfNode` when there is an `else` or an `elsif` in the `if` statement.
Class Method Summary collapse
-
.type ⇒ Object
Return a symbol representation of this node type.
Instance Method Summary collapse
-
#===(other) ⇒ Object
Implements case-equality for the node.
-
#accept(visitor) ⇒ Object
def accept: (Visitor visitor) -> void.
-
#child_nodes ⇒ Object
(also: #deconstruct)
def child_nodes: () -> Array[nil | Node].
-
#comment_targets ⇒ Object
def comment_targets: () -> Array[Node | Location].
-
#compact_child_nodes ⇒ Object
def compact_child_nodes: () -> Array.
-
#consequent ⇒ Object
Returns the subsequent if/elsif/else clause of the if node.
-
#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.
-
#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? }.
-
#end_keyword ⇒ Object
def end_keyword: () -> String?.
-
#end_keyword_loc ⇒ Object
The location of the ‘end` keyword if present, `nil` otherwise.
-
#if_keyword ⇒ Object
def if_keyword: () -> String?.
-
#if_keyword_loc ⇒ Object
The location of the ‘if` keyword if present.
-
#initialize(source, node_id, location, flags, if_keyword_loc, predicate, then_keyword_loc, statements, subsequent, end_keyword_loc) ⇒ IfNode
constructor
Initialize a new IfNode node.
-
#inspect ⇒ Object
def inspect -> String.
-
#newline_flag!(lines) ⇒ Object
:nodoc:.
-
#then_keyword ⇒ Object
def then_keyword: () -> String?.
-
#then_keyword_loc ⇒ Object
The location of the ‘then` keyword (if present) or the `?` in a ternary expression, `nil` otherwise.
-
#type ⇒ Object
Return a symbol representation of this node type.
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.
7482 7483 7484 7485 7486 7487 7488 7489 7490 7491 7492 7493 |
# File 'lib/prism/node.rb', line 7482 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
#predicate ⇒ Object (readonly)
The node for the condition the ‘IfNode` is testing.
if foo
^^^
bar
end
bar if foo
^^^
foo ? bar : baz
^^^
7562 7563 7564 |
# File 'lib/prism/node.rb', line 7562 def predicate @predicate end |
#statements ⇒ Object (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
7591 7592 7593 |
# File 'lib/prism/node.rb', line 7591 def statements @statements end |
#subsequent ⇒ Object (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
^^^^^^^^^^^^
7606 7607 7608 |
# File 'lib/prism/node.rb', line 7606 def subsequent @subsequent end |
Class Method Details
.type ⇒ Object
Return a symbol representation of this node type. See ‘Node::type`.
7652 7653 7654 |
# File 'lib/prism/node.rb', line 7652 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.
7658 7659 7660 7661 7662 7663 7664 7665 7666 |
# File 'lib/prism/node.rb', line 7658 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
7496 7497 7498 |
# File 'lib/prism/node.rb', line 7496 def accept(visitor) visitor.visit_if_node(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
def child_nodes: () -> Array[nil | Node]
7501 7502 7503 |
# File 'lib/prism/node.rb', line 7501 def child_nodes [predicate, statements, subsequent] end |
#comment_targets ⇒ Object
def comment_targets: () -> Array[Node | Location]
7515 7516 7517 |
# File 'lib/prism/node.rb', line 7515 def comment_targets [*if_keyword_loc, predicate, *then_keyword_loc, *statements, *subsequent, *end_keyword_loc] #: Array[Prism::node | Location] end |
#compact_child_nodes ⇒ Object
def compact_child_nodes: () -> Array
7506 7507 7508 7509 7510 7511 7512 |
# File 'lib/prism/node.rb', line 7506 def compact_child_nodes compact = [] #: Array[Prism::node] compact << predicate compact << statements if statements compact << subsequent if subsequent compact end |
#consequent ⇒ Object
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
7520 7521 7522 |
# File 'lib/prism/node.rb', line 7520 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? }
7528 7529 7530 |
# File 'lib/prism/node.rb', line 7528 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_keyword ⇒ Object
def end_keyword: () -> String?
7637 7638 7639 |
# File 'lib/prism/node.rb', line 7637 def end_keyword end_keyword_loc&.slice end |
#end_keyword_loc ⇒ Object
The location of the ‘end` keyword if present, `nil` otherwise.
if foo
bar
end
^^^
7614 7615 7616 7617 7618 7619 7620 7621 7622 7623 7624 |
# File 'lib/prism/node.rb', line 7614 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_keyword ⇒ Object
def if_keyword: () -> String?
7627 7628 7629 |
# File 'lib/prism/node.rb', line 7627 def if_keyword if_keyword_loc&.slice end |
#if_keyword_loc ⇒ Object
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.
7538 7539 7540 7541 7542 7543 7544 7545 7546 7547 7548 |
# File 'lib/prism/node.rb', line 7538 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 |
#inspect ⇒ Object
def inspect -> String
7642 7643 7644 |
# File 'lib/prism/node.rb', line 7642 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_keyword ⇒ Object
def then_keyword: () -> String?
7632 7633 7634 |
# File 'lib/prism/node.rb', line 7632 def then_keyword then_keyword_loc&.slice end |
#then_keyword_loc ⇒ Object
The location of the ‘then` keyword (if present) or the `?` in a ternary expression, `nil` otherwise.
if foo then bar end
^^^^
a ? b : c
^
7571 7572 7573 7574 7575 7576 7577 7578 7579 7580 7581 |
# File 'lib/prism/node.rb', line 7571 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 |
#type ⇒ Object
Return a symbol representation of this node type. See ‘Node#type`.
7647 7648 7649 |
# File 'lib/prism/node.rb', line 7647 def type :if_node end |