Class: Prism::WhenNode
- Inherits:
-
PrismNode
- Object
- PrismNode
- Prism::WhenNode
- Defined in:
- lib/prism/node.rb,
ext/prism/api_node.c
Overview
Represents the use of the when keyword within a case statement.
case true
when true
^^^^^^^^^
end
Instance Attribute Summary collapse
-
#conditions ⇒ Object
readonly
attr_reader conditions: Array.
-
#statements ⇒ Object
readonly
attr_reader statements: StatementsNode?.
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.
-
#copy(node_id: self.node_id, location: self.location, flags: self.flags, keyword_loc: self.keyword_loc, conditions: self.conditions, then_keyword_loc: self.then_keyword_loc, statements: self.statements) ⇒ Object
def copy: (?node_id: Integer, ?location: Location, ?flags: Integer, ?keyword_loc: Location, ?conditions: Array, ?then_keyword_loc: Location?, ?statements: StatementsNode?) -> WhenNode.
- #deconstruct_keys(keys) ⇒ Object
-
#each_child_node {|statements| ... } ⇒ Object
def each_child_node: () { (Prism::node) -> void } -> void | () -> Enumerator.
-
#initialize(source, node_id, location, flags, keyword_loc, conditions, then_keyword_loc, statements) ⇒ WhenNode
constructor
Initialize a new WhenNode node.
-
#inspect ⇒ Object
def inspect -> String.
-
#keyword ⇒ Object
def keyword: () -> String.
-
#keyword_loc ⇒ Object
attr_reader keyword_loc: Location.
-
#save_keyword_loc(repository) ⇒ Object
Save the 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
attr_reader then_keyword_loc: Location?.
-
#type ⇒ Object
Return a symbol representation of this node type.
Constructor Details
#initialize(source, node_id, location, flags, keyword_loc, conditions, then_keyword_loc, statements) ⇒ WhenNode
Initialize a new WhenNode node.
19175 19176 19177 19178 19179 19180 19181 19182 19183 19184 |
# File 'lib/prism/node.rb', line 19175 def initialize(source, node_id, location, flags, keyword_loc, conditions, then_keyword_loc, statements) @source = source @node_id = node_id @location = location @flags = flags @keyword_loc = keyword_loc @conditions = conditions @then_keyword_loc = then_keyword_loc @statements = statements end |
Instance Attribute Details
#conditions ⇒ Object (readonly)
attr_reader conditions: Array
19244 19245 19246 |
# File 'lib/prism/node.rb', line 19244 def conditions @conditions end |
#statements ⇒ Object (readonly)
attr_reader statements: StatementsNode?
19266 19267 19268 |
# File 'lib/prism/node.rb', line 19266 def statements @statements end |
Class Method Details
.type ⇒ Object
Return a symbol representation of this node type. See Node::type.
19289 19290 19291 |
# File 'lib/prism/node.rb', line 19289 def self.type :when_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.
19295 19296 19297 19298 19299 19300 19301 19302 |
# File 'lib/prism/node.rb', line 19295 def ===(other) other.is_a?(WhenNode) && (keyword_loc.nil? == other.keyword_loc.nil?) && (conditions.length == other.conditions.length) && conditions.zip(other.conditions).all? { |left, right| left === right } && (then_keyword_loc.nil? == other.then_keyword_loc.nil?) && (statements === other.statements) end |
#accept(visitor) ⇒ Object
def accept: (Visitor visitor) -> void
19187 19188 19189 |
# File 'lib/prism/node.rb', line 19187 def accept(visitor) visitor.visit_when_node(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
def child_nodes: () -> Array
19192 19193 19194 |
# File 'lib/prism/node.rb', line 19192 def child_nodes [*conditions, statements] end |
#comment_targets ⇒ Object
def comment_targets: () -> Array[Node | Location]
19213 19214 19215 |
# File 'lib/prism/node.rb', line 19213 def comment_targets [keyword_loc, *conditions, *then_keyword_loc, *statements] #: Array[Prism::node | Location] end |
#compact_child_nodes ⇒ Object
def compact_child_nodes: () -> Array
19205 19206 19207 19208 19209 19210 |
# File 'lib/prism/node.rb', line 19205 def compact_child_nodes compact = [] #: Array[Prism::node] compact.concat(conditions) compact << statements if statements compact end |
#copy(node_id: self.node_id, location: self.location, flags: self.flags, keyword_loc: self.keyword_loc, conditions: self.conditions, then_keyword_loc: self.then_keyword_loc, statements: self.statements) ⇒ Object
def copy: (?node_id: Integer, ?location: Location, ?flags: Integer, ?keyword_loc: Location, ?conditions: Array, ?then_keyword_loc: Location?, ?statements: StatementsNode?) -> WhenNode
19218 19219 19220 |
# File 'lib/prism/node.rb', line 19218 def copy(node_id: self.node_id, location: self.location, flags: self.flags, keyword_loc: self.keyword_loc, conditions: self.conditions, then_keyword_loc: self.then_keyword_loc, statements: self.statements) WhenNode.new(source, node_id, location, flags, keyword_loc, conditions, then_keyword_loc, statements) end |
#deconstruct_keys(keys) ⇒ Object
19226 19227 19228 |
# File 'lib/prism/node.rb', line 19226 def deconstruct_keys(keys) { node_id: node_id, location: location, keyword_loc: keyword_loc, conditions: conditions, then_keyword_loc: then_keyword_loc, statements: statements } end |
#each_child_node {|statements| ... } ⇒ Object
def each_child_node: () { (Prism::node) -> void } -> void | () -> Enumerator
19197 19198 19199 19200 19201 19202 |
# File 'lib/prism/node.rb', line 19197 def each_child_node return to_enum(:each_child_node) unless block_given? conditions.each { |node| yield node } yield statements if statements end |
#inspect ⇒ Object
def inspect -> String
19279 19280 19281 |
# File 'lib/prism/node.rb', line 19279 def inspect InspectVisitor.compose(self) end |
#keyword ⇒ Object
def keyword: () -> String
19269 19270 19271 |
# File 'lib/prism/node.rb', line 19269 def keyword keyword_loc.slice end |
#keyword_loc ⇒ Object
attr_reader keyword_loc: Location
19231 19232 19233 19234 19235 |
# File 'lib/prism/node.rb', line 19231 def keyword_loc location = @keyword_loc return location if location.is_a?(Location) @keyword_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF) end |
#save_keyword_loc(repository) ⇒ Object
Save the keyword_loc location using the given saved source so that it can be retrieved later.
19239 19240 19241 |
# File 'lib/prism/node.rb', line 19239 def save_keyword_loc(repository) repository.enter(node_id, :keyword_loc) 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.
19261 19262 19263 |
# File 'lib/prism/node.rb', line 19261 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?
19274 19275 19276 |
# File 'lib/prism/node.rb', line 19274 def then_keyword then_keyword_loc&.slice end |
#then_keyword_loc ⇒ Object
attr_reader then_keyword_loc: Location?
19247 19248 19249 19250 19251 19252 19253 19254 19255 19256 19257 |
# File 'lib/prism/node.rb', line 19247 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`.
19284 19285 19286 |
# File 'lib/prism/node.rb', line 19284 def type :when_node end |