Class: Prism::IfNode
| Relationships & Source Files | |
| Super Chains via Extension / Inclusion / Inheritance | |
|
Class Chain:
self,
PrismNode
|
|
|
Instance Chain:
self,
PrismNode
|
|
| Inherits: |
PrismNode
|
| Defined in: | lib/prism/node.rb, ext/prism/api_node.c, lib/prism/node_ext.rb, lib/prism/parse_result/newlines.rb |
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
^^^^^^^^^^^^^^^
Class Method Summary
-
.new(source, node_id, location, flags, if_keyword_loc, predicate, then_keyword_loc, statements, consequent, end_keyword_loc) ⇒ IfNode
constructor
Initialize a new
IfNodenode. -
.type
Return a symbol representation of this node type.
Instance Attribute Summary
-
#consequent
readonly
Represents an
ElseNodeor anIfNodewhen there is anelseor anelsifin theifstatement. -
#predicate
readonly
The node for the condition the
IfNodeis testing. -
#statements
readonly
Represents the body of statements that will be executed when the predicate is evaluated as truthy.
Instance Method Summary
-
#===(other)
Implements case-equality for the node.
-
#accept(visitor)
def accept: (Visitor visitor) -> void.
-
#child_nodes
(also: #deconstruct)
def child_nodes: () -> Array[nil | Node].
-
#comment_targets
def comment_targets: () -> Array[Node | Location].
-
#compact_child_nodes
def compact_child_nodes: () -> Array.
-
#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, consequent: self.consequent, end_keyword_loc: self.end_keyword_loc)
def copy: (?node_id: Integer, ?location:
Location, ?flags: Integer, ?if_keyword_loc: Location?, ?predicate:Prism.node, ?then_keyword_loc: Location?, ?statements: StatementsNode?, ?consequent:Prism.node?, ?end_keyword_loc: Location?) ->IfNode. -
#deconstruct
Alias for #child_nodes.
- #deconstruct_keys(keys)
-
#end_keyword
def end_keyword: () -> String?
-
#end_keyword_loc
The location of the
endkeyword if present,nilotherwise. -
#if_keyword
def if_keyword: () -> String?
-
#if_keyword_loc
The location of the
ifkeyword if present. -
#inspect
def inspect -> String.
-
#then_keyword
def then_keyword: () -> String?
-
#then_keyword_loc
The location of the
thenkeyword (if present) or the?in a ternary expression,nilotherwise. -
#type
Return a symbol representation of this node type.
- #newline_flag!(lines) Internal use only
Constructor Details
.new(source, node_id, location, flags, if_keyword_loc, predicate, then_keyword_loc, statements, consequent, end_keyword_loc) ⇒ IfNode
Initialize a new IfNode node.
# File 'lib/prism/node.rb', line 7306
def initialize(source, node_id, location, flags, if_keyword_loc, predicate, then_keyword_loc, statements, consequent, 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 @consequent = consequent @end_keyword_loc = end_keyword_loc end
Class Method Details
.type
Return a symbol representation of this node type. See Node::type.
# File 'lib/prism/node.rb', line 7473
def self.type :if_node end
Instance Attribute Details
#consequent (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
^^^^^^^^^^^^
# File 'lib/prism/node_ext.rb', line 488
attr_reader :consequent
#predicate (readonly)
The node for the condition the IfNode is testing.
if foo
^^^
end
if foo
^^^
foo ? : baz
^^^
# File 'lib/prism/node.rb', line 7386
attr_reader :predicate
#statements (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
# File 'lib/prism/node.rb', line 7414
attr_reader :statements
Instance Method Details
#===(other)
Implements case-equality for the node. This is effectively == but without comparing the value of locations. Locations are checked only for presence.
# File 'lib/prism/node.rb', line 7479
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) && (consequent === other.consequent) && (end_keyword_loc.nil? == other.end_keyword_loc.nil?) end
#accept(visitor)
def accept: (Visitor visitor) -> void
# File 'lib/prism/node.rb', line 7320
def accept(visitor) visitor.visit_if_node(self) end
#child_nodes Also known as: #deconstruct
def child_nodes: () -> Array[nil | Node]
# File 'lib/prism/node.rb', line 7325
def child_nodes [predicate, statements, consequent] end
#comment_targets
def comment_targets: () -> Array[Node | Location]
# File 'lib/prism/node.rb', line 7339
def comment_targets [*if_keyword_loc, predicate, *then_keyword_loc, *statements, *consequent, *end_keyword_loc] #: Array[Prism::node | Location] end
#compact_child_nodes
def compact_child_nodes: () -> Array
# File 'lib/prism/node.rb', line 7330
def compact_child_nodes compact = [] #: Array[Prism::node] compact << predicate compact << statements if statements compact << consequent if consequent compact 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, consequent: self.consequent, end_keyword_loc: self.end_keyword_loc)
def copy: (?node_id: Integer, ?location: Location, ?flags: Integer, ?if_keyword_loc: Location?, ?predicate: Prism.node, ?then_keyword_loc: Location?, ?statements: StatementsNode?, ?consequent: Prism.node?, ?end_keyword_loc: Location?) -> IfNode
# File 'lib/prism/node.rb', line 7344
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, consequent: self.consequent, end_keyword_loc: self.end_keyword_loc) IfNode.new(source, node_id, location, flags, if_keyword_loc, predicate, then_keyword_loc, statements, consequent, end_keyword_loc) end
#deconstruct
Alias for #child_nodes.
# File 'lib/prism/node.rb', line 7349
alias deconstruct child_nodes
#deconstruct_keys(keys)
# File 'lib/prism/node.rb', line 7352
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, consequent: consequent, end_keyword_loc: end_keyword_loc } end
#end_keyword
def end_keyword: () -> String?
# File 'lib/prism/node.rb', line 7458
def end_keyword end_keyword_loc&.slice end
#end_keyword_loc
The location of the end keyword if present, nil otherwise.
if foo
end
^^^
#if_keyword
def if_keyword: () -> String?
# File 'lib/prism/node.rb', line 7448
def if_keyword if_keyword_loc&.slice end
#if_keyword_loc
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.
#inspect
def inspect -> String
# File 'lib/prism/node.rb', line 7463
def inspect InspectVisitor.compose(self) end
#newline_flag!(lines)
# File 'lib/prism/parse_result/newlines.rb', line 92
def newline_flag!(lines) # :nodoc: predicate.newline_flag!(lines) end
#then_keyword
def then_keyword: () -> String?
# File 'lib/prism/node.rb', line 7453
def then_keyword then_keyword_loc&.slice end
#then_keyword_loc
The location of the then keyword (if present) or the ? in a ternary expression, nil otherwise.
if foo then end
^^^^
a ? b : c
^
#type
Return a symbol representation of this node type. See Node#type.
# File 'lib/prism/node.rb', line 7468
def type :if_node end