Class: Prism::IfNode
- Inherits:
-
PrismNode
- Object
- PrismNode
- Prism::IfNode
- Defined in:
- lib/prism/node.rb,
lib/prism/parse_result/newlines.rb,
sig/generated/prism/node.rbs,
sig/generated/prism/parse_result/newlines.rbs,
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
^^^^^^^^^^^^^^^
Class Method Summary collapse
-
.type ⇒ :if_node
See
Node.type.
Instance Method Summary collapse
-
#===(other) ⇒ boolish
: (untyped other) -> boolish.
-
#accept(visitor) ⇒ Object
See Node.accept.
-
#child_nodes ⇒ Array[node?]
(also: #deconstruct)
See Node.child_nodes.
-
#compact_child_nodes ⇒ Array[node]
See Node.compact_child_nodes.
-
#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
:call-seq: copy(**fields) -> IfNode.
-
#deconstruct_keys(keys) ⇒ Hash[Symbol, untyped]
: (Array? keys) -> Hash[Symbol, untyped].
-
#each_child_node {|arg0| ... } ⇒ Object
See Node.each_child_node.
-
#end_keyword ⇒ String?
:call-seq: end_keyword -> String | nil.
-
#end_keyword_loc ⇒ Location?
:category: Locations :call-seq: end_keyword_loc -> Location | nil.
-
#if_keyword ⇒ String?
:call-seq: if_keyword -> String | nil.
-
#if_keyword_loc ⇒ Location?
:category: Locations :call-seq: if_keyword_loc -> Location | nil.
-
#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 ⇒ String
: () -> String.
-
#newline_flag!(lines) ⇒ void
: (Array lines) -> void.
-
#predicate ⇒ Prism::node
:call-seq: predicate -> Node.
-
#save_end_keyword_loc(repository) ⇒ Relocation::Entry?
:category: Repository Save the end_keyword_loc location using the given saved source so that it can be retrieved later.
-
#save_if_keyword_loc(repository) ⇒ Relocation::Entry?
:category: Repository Save the if_keyword_loc location using the given saved source so that it can be retrieved later.
-
#save_then_keyword_loc(repository) ⇒ Relocation::Entry?
:category: Repository Save the then_keyword_loc location using the given saved source so that it can be retrieved later.
-
#statements ⇒ StatementsNode?
:call-seq: statements -> StatementsNode | nil.
-
#subsequent ⇒ ElseNode, ...
:call-seq: subsequent -> ElseNode | IfNode | nil.
-
#then_keyword ⇒ String?
:call-seq: then_keyword -> String | nil.
-
#then_keyword_loc ⇒ Location?
:category: Locations :call-seq: then_keyword_loc -> Location | nil.
-
#type ⇒ :if_node
See
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.
: (Source source, Integer node_id, Location location, Integer flags, Location? if_keyword_loc, Prism::node predicate, Location? then_keyword_loc, StatementsNode? statements, (ElseNode | IfNode)? subsequent, Location? end_keyword_loc) -> void
15628 15629 15630 15631 15632 15633 15634 15635 15636 15637 15638 15639 |
# File 'lib/prism/node.rb', line 15628 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 |
Class Method Details
.type ⇒ :if_node
See Node.type.
: () -> :if_node
15732 15733 15734 |
# File 'lib/prism/node.rb', line 15732 def self.type :if_node end |
Instance Method Details
#===(other) ⇒ boolish
: (untyped other) -> boolish
15959 15960 15961 15962 15963 15964 15965 15966 15967 |
# File 'lib/prism/node.rb', line 15959 def ===(other) # :nodoc: 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
See Node.accept.
: (_Visitor visitor) -> untyped
15655 15656 15657 |
# File 'lib/prism/node.rb', line 15655 def accept(visitor) visitor.visit_if_node(self) end |
#child_nodes ⇒ Array[node?] Also known as: deconstruct
See Node.child_nodes.
: () -> Array
15662 15663 15664 |
# File 'lib/prism/node.rb', line 15662 def child_nodes [predicate, statements, subsequent] end |
#compact_child_nodes ⇒ Array[node]
See Node.compact_child_nodes.
: () -> Array
15681 15682 15683 15684 15685 15686 15687 |
# File 'lib/prism/node.rb', line 15681 def compact_child_nodes compact = [] #: Array[Prism::node] compact << predicate if (statements = self.statements); compact << statements; end if (subsequent = self.subsequent); compact << subsequent; end 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, subsequent: self.subsequent, end_keyword_loc: self.end_keyword_loc) ⇒ IfNode
:call-seq:
copy(**fields) -> IfNode
Creates a copy of self with the given fields, using self as the template.
: (?node_id: Integer, ?location: Location, ?flags: Integer, ?if_keyword_loc: Location?, ?predicate: Prism::node, ?then_keyword_loc: Location?, ?statements: StatementsNode?, ?subsequent: (ElseNode | IfNode)?, ?end_keyword_loc: Location?) -> IfNode
15695 15696 15697 |
# File 'lib/prism/node.rb', line 15695 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) ⇒ Hash[Symbol, untyped]
: (Array? keys) -> Hash[Symbol, untyped]
15702 15703 15704 15705 15706 15707 15708 15709 15710 15711 15712 15713 15714 15715 15716 15717 15718 15719 15720 |
# File 'lib/prism/node.rb', line 15702 def deconstruct_keys(keys) # :nodoc: (keys || %i[node_id location if_keyword_loc predicate then_keyword_loc statements subsequent end_keyword_loc if_keyword then_keyword end_keyword]).each_with_object( {} #: Hash[Symbol, untyped] ) do |key, deconstructed| case key when :node_id then deconstructed[:node_id] = self.node_id when :location then deconstructed[:location] = self.location when :if_keyword_loc then deconstructed[:if_keyword_loc] = self.if_keyword_loc when :predicate then deconstructed[:predicate] = self.predicate when :then_keyword_loc then deconstructed[:then_keyword_loc] = self.then_keyword_loc when :statements then deconstructed[:statements] = self.statements when :subsequent then deconstructed[:subsequent] = self.subsequent when :end_keyword_loc then deconstructed[:end_keyword_loc] = self.end_keyword_loc when :if_keyword then deconstructed[:if_keyword] = self.if_keyword when :then_keyword then deconstructed[:then_keyword] = self.then_keyword when :end_keyword then deconstructed[:end_keyword] = self.end_keyword end end end |
#each_child_node ⇒ Enumerator[node, void] #each_child_node ⇒ void
See Node.each_child_node.
: () -> Enumerator[node, void] : () { (node) -> void } -> void
15670 15671 15672 15673 15674 15675 15676 |
# File 'lib/prism/node.rb', line 15670 def each_child_node(&blk) return to_enum(:each_child_node) unless block_given? yield predicate if (statements = self.statements); yield statements; end if (subsequent = self.subsequent); yield subsequent; end end |
#end_keyword ⇒ String?
:call-seq:
end_keyword -> String | nil
Slice the location of end_keyword_loc from the source.
: () -> String?
15952 15953 15954 |
# File 'lib/prism/node.rb', line 15952 def end_keyword end_keyword_loc&.slice end |
#end_keyword_loc ⇒ Location?
:category: Locations :call-seq:
end_keyword_loc -> Location | nil
The Location of the end keyword if present, nil otherwise.
if foo
bar
end
^^^
: () -> Location?
15904 15905 15906 15907 15908 15909 15910 15911 15912 15913 15914 |
# File 'lib/prism/node.rb', line 15904 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 ⇒ String?
:call-seq:
if_keyword -> String | nil
Slice the location of if_keyword_loc from the source.
: () -> String?
15932 15933 15934 |
# File 'lib/prism/node.rb', line 15932 def if_keyword if_keyword_loc&.slice end |
#if_keyword_loc ⇒ Location?
:category: Locations :call-seq:
if_keyword_loc -> Location | nil
The Location of the if keyword if present.
bar if foo
^^
Explore code sample
The if_keyword_loc field will be nil when the IfNode represents a ternary expression.
: () -> Location?
15759 15760 15761 15762 15763 15764 15765 15766 15767 15768 15769 |
# File 'lib/prism/node.rb', line 15759 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 ⇒ String
: () -> String
15737 15738 15739 |
# File 'lib/prism/node.rb', line 15737 def inspect # :nodoc: InspectVisitor.compose(self) end |
#newline_flag!(lines) ⇒ void
This method returns an undefined value.
: (Array lines) -> void
243 244 245 |
# File 'lib/prism/parse_result/newlines.rb', line 243 def newline_flag!(lines) # :nodoc: predicate.newline_flag!(lines) end |
#predicate ⇒ Prism::node
:call-seq:
predicate -> Node
The node for the condition the IfNode is testing.
if foo
^^^
bar
end
bar if foo
^^^
foo ? bar : baz
^^^
: () -> Prism::node
15800 15801 15802 |
# File 'lib/prism/node.rb', line 15800 def predicate @predicate end |
#save_end_keyword_loc(repository) ⇒ Relocation::Entry?
:category: Repository Save the end_keyword_loc location using the given saved source so that it can be retrieved later.
: (_Repository repository) -> Relocation::Entry?
15921 15922 15923 |
# File 'lib/prism/node.rb', line 15921 def save_end_keyword_loc(repository) repository.enter(node_id, :end_keyword_loc) unless @end_keyword_loc.nil? end |
#save_if_keyword_loc(repository) ⇒ Relocation::Entry?
:category: Repository Save the if_keyword_loc location using the given saved source so that it can be retrieved later.
: (_Repository repository) -> Relocation::Entry?
15776 15777 15778 |
# File 'lib/prism/node.rb', line 15776 def save_if_keyword_loc(repository) repository.enter(node_id, :if_keyword_loc) unless @if_keyword_loc.nil? end |
#save_then_keyword_loc(repository) ⇒ Relocation::Entry?
:category: Repository Save the then_keyword_loc location using the given saved source so that it can be retrieved later.
: (_Repository repository) -> Relocation::Entry?
15838 15839 15840 |
# File 'lib/prism/node.rb', line 15838 def save_then_keyword_loc(repository) repository.enter(node_id, :then_keyword_loc) unless @then_keyword_loc.nil? end |
#statements ⇒ StatementsNode?
:call-seq:
statements -> StatementsNode | nil
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
: () -> StatementsNode?
15858 15859 15860 |
# File 'lib/prism/node.rb', line 15858 def statements @statements end |
#subsequent ⇒ ElseNode, ...
:call-seq:
subsequent -> ElseNode | IfNode | nil
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
^^^^^^^^^^^^
: () -> (ElseNode | IfNode)?
15884 15885 15886 |
# File 'lib/prism/node.rb', line 15884 def subsequent @subsequent end |
#then_keyword ⇒ String?
:call-seq:
then_keyword -> String | nil
Slice the location of then_keyword_loc from the source.
: () -> String?
15942 15943 15944 |
# File 'lib/prism/node.rb', line 15942 def then_keyword then_keyword_loc&.slice end |
#then_keyword_loc ⇒ Location?
:category: Locations :call-seq:
then_keyword_loc -> Location | nil
The Location of the then keyword (if present) or the ? in a ternary expression, nil otherwise.
if foo then bar end
^^^^
a ? b : c
^
: () -> Location?
15821 15822 15823 15824 15825 15826 15827 15828 15829 15830 15831 |
# File 'lib/prism/node.rb', line 15821 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 ⇒ :if_node
See Node#type.
: () -> :if_node
15725 15726 15727 |
# File 'lib/prism/node.rb', line 15725 def type :if_node end |