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.
-
#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.
8549 8550 8551 8552 8553 8554 8555 8556 8557 8558 8559 8560 |
# File 'lib/prism/node.rb', line 8549 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
^^^
8635 8636 8637 |
# File 'lib/prism/node.rb', line 8635 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
8670 8671 8672 |
# File 'lib/prism/node.rb', line 8670 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
^^^^^^^^^^^^
8685 8686 8687 |
# File 'lib/prism/node.rb', line 8685 def subsequent @subsequent end |
Class Method Details
.type ⇒ Object
Return a symbol representation of this node type. See ‘Node::type`.
8737 8738 8739 |
# File 'lib/prism/node.rb', line 8737 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.
8743 8744 8745 8746 8747 8748 8749 8750 8751 |
# File 'lib/prism/node.rb', line 8743 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
8563 8564 8565 |
# File 'lib/prism/node.rb', line 8563 def accept(visitor) visitor.visit_if_node(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
def child_nodes: () -> Array
8568 8569 8570 |
# File 'lib/prism/node.rb', line 8568 def child_nodes [predicate, statements, subsequent] end |
#comment_targets ⇒ Object
def comment_targets: () -> Array[Node | Location]
8582 8583 8584 |
# File 'lib/prism/node.rb', line 8582 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
8573 8574 8575 8576 8577 8578 8579 |
# File 'lib/prism/node.rb', line 8573 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
8587 8588 8589 |
# File 'lib/prism/node.rb', line 8587 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? }
8595 8596 8597 |
# File 'lib/prism/node.rb', line 8595 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?
8722 8723 8724 |
# File 'lib/prism/node.rb', line 8722 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
^^^
8693 8694 8695 8696 8697 8698 8699 8700 8701 8702 8703 |
# File 'lib/prism/node.rb', line 8693 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?
8712 8713 8714 |
# File 'lib/prism/node.rb', line 8712 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.
8605 8606 8607 8608 8609 8610 8611 8612 8613 8614 8615 |
# File 'lib/prism/node.rb', line 8605 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
8727 8728 8729 |
# File 'lib/prism/node.rb', line 8727 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.
8707 8708 8709 |
# File 'lib/prism/node.rb', line 8707 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.
8619 8620 8621 |
# File 'lib/prism/node.rb', line 8619 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.
8658 8659 8660 |
# File 'lib/prism/node.rb', line 8658 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?
8717 8718 8719 |
# File 'lib/prism/node.rb', line 8717 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
^
8644 8645 8646 8647 8648 8649 8650 8651 8652 8653 8654 |
# File 'lib/prism/node.rb', line 8644 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`.
8732 8733 8734 |
# File 'lib/prism/node.rb', line 8732 def type :if_node end |