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.
if foo
^^^^^^^^^^
if foo then end
^^^^^^^^^^^^^^^^^^^
foo ? : 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.
-
#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:.
-
#save_end_keyword_loc(repository) ⇒ Object
Save the end_keyword_loc location using the given saved source so that it can be retrieved later.
-
#save_if_keyword_loc(repository) ⇒ Object
Save the if_keyword_loc location using the given saved source so that it can be retrieved later.
-
#save_then_keyword_loc(repository) ⇒ Object
Save the then_keyword_loc location using the given saved source so that it can be retrieved later.
-
#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.
8586 8587 8588 8589 8590 8591 8592 8593 8594 8595 8596 8597 |
# File 'lib/prism/node.rb', line 8586 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
^^^
end
if foo
^^^
foo ? : baz
^^^
8672 8673 8674 |
# File 'lib/prism/node.rb', line 8672 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
^^^
baz
^^^
end
8707 8708 8709 |
# File 'lib/prism/node.rb', line 8707 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
elsif baz
^^^^^^^^^
qux
^^^
end
^^^
if foo then else baz end
^^^^^^^^^^^^
8722 8723 8724 |
# File 'lib/prism/node.rb', line 8722 def subsequent @subsequent end |
Class Method Details
.type ⇒ Object
Return a symbol representation of this node type. See ‘Node::type`.
8774 8775 8776 |
# File 'lib/prism/node.rb', line 8774 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.
8780 8781 8782 8783 8784 8785 8786 8787 8788 |
# File 'lib/prism/node.rb', line 8780 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
8600 8601 8602 |
# File 'lib/prism/node.rb', line 8600 def accept(visitor) visitor.visit_if_node(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
def child_nodes: () -> Array
8605 8606 8607 |
# File 'lib/prism/node.rb', line 8605 def child_nodes [predicate, statements, subsequent] end |
#comment_targets ⇒ Object
def comment_targets: () -> Array[Node | Location]
8619 8620 8621 |
# File 'lib/prism/node.rb', line 8619 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
8610 8611 8612 8613 8614 8615 8616 |
# File 'lib/prism/node.rb', line 8610 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.
488 489 490 491 |
# File 'lib/prism/node_ext.rb', line 488 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
8624 8625 8626 |
# File 'lib/prism/node.rb', line 8624 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? }
8632 8633 8634 |
# File 'lib/prism/node.rb', line 8632 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?
8759 8760 8761 |
# File 'lib/prism/node.rb', line 8759 def end_keyword end_keyword_loc&.slice end |
#end_keyword_loc ⇒ Object
The location of the ‘end` keyword if present, `nil` otherwise.
if foo
end
^^^
8730 8731 8732 8733 8734 8735 8736 8737 8738 8739 8740 |
# File 'lib/prism/node.rb', line 8730 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?
8749 8750 8751 |
# File 'lib/prism/node.rb', line 8749 def if_keyword if_keyword_loc&.slice end |
#if_keyword_loc ⇒ Object
The location of the ‘if` keyword if present.
if foo
^^
The ‘if_keyword_loc` field will be `nil` when the `IfNode` represents a ternary expression.
8642 8643 8644 8645 8646 8647 8648 8649 8650 8651 8652 |
# File 'lib/prism/node.rb', line 8642 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
8764 8765 8766 |
# File 'lib/prism/node.rb', line 8764 def inspect InspectVisitor.compose(self) end |
#newline_flag!(lines) ⇒ Object
:nodoc:
92 93 94 |
# File 'lib/prism/parse_result/newlines.rb', line 92 def newline_flag!(lines) # :nodoc: predicate.newline_flag!(lines) end |
#save_end_keyword_loc(repository) ⇒ Object
Save the end_keyword_loc location using the given saved source so that it can be retrieved later.
8744 8745 8746 |
# File 'lib/prism/node.rb', line 8744 def save_end_keyword_loc(repository) repository.enter(node_id, :end_keyword_loc) unless @end_keyword_loc.nil? end |
#save_if_keyword_loc(repository) ⇒ Object
Save the if_keyword_loc location using the given saved source so that it can be retrieved later.
8656 8657 8658 |
# File 'lib/prism/node.rb', line 8656 def save_if_keyword_loc(repository) repository.enter(node_id, :if_keyword_loc) unless @if_keyword_loc.nil? end |
#save_then_keyword_loc(repository) ⇒ Object
Save the then_keyword_loc location using the given saved source so that it can be retrieved later.
8695 8696 8697 |
# File 'lib/prism/node.rb', line 8695 def save_then_keyword_loc(repository) repository.enter(node_id, :then_keyword_loc) unless @then_keyword_loc.nil? end |
#then_keyword ⇒ Object
def then_keyword: () -> String?
8754 8755 8756 |
# File 'lib/prism/node.rb', line 8754 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 end
^^^^
a ? b : c
^
8681 8682 8683 8684 8685 8686 8687 8688 8689 8690 8691 |
# File 'lib/prism/node.rb', line 8681 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`.
8769 8770 8771 |
# File 'lib/prism/node.rb', line 8769 def type :if_node end |