Class: Prism::UnlessNode
- Inherits:
-
PrismNode
- Object
- PrismNode
- Prism::UnlessNode
- Defined in:
- lib/prism/node.rb,
lib/prism/node_ext.rb,
lib/prism/parse_result/newlines.rb,
sig/generated/prism/node.rbs,
sig/generated/prism/node_ext.rbs,
sig/generated/prism/parse_result/newlines.rbs,
ext/prism/api_node.c
Overview
Represents the use of the unless keyword, either in the block form or the modifier form.
bar unless foo
^^^^^^^^^^^^^^
unless foo then bar end
^^^^^^^^^^^^^^^^^^^^^^^
Class Method Summary collapse
-
.type ⇒ :unless_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, unless_keyword_loc: self.unless_keyword_loc, predicate: self.predicate, then_keyword_loc: self.then_keyword_loc, statements: self.statements, else_clause: self.else_clause, end_keyword_loc: self.end_keyword_loc) ⇒ UnlessNode
:call-seq: copy(**fields) -> UnlessNode.
-
#deconstruct_keys(keys) ⇒ Hash[Symbol, untyped]
: (Array? keys) -> Hash[Symbol, untyped].
-
#each_child_node {|arg0| ... } ⇒ Object
See Node.each_child_node.
-
#else_clause ⇒ ElseNode?
:call-seq: else_clause -> ElseNode | nil.
-
#end_keyword ⇒ String?
:call-seq: end_keyword -> String | nil.
-
#end_keyword_loc ⇒ Location?
:category: Locations :call-seq: end_keyword_loc -> Location | nil.
-
#initialize(source, node_id, location, flags, unless_keyword_loc, predicate, then_keyword_loc, statements, else_clause, end_keyword_loc) ⇒ UnlessNode
constructor
Initialize a new UnlessNode node.
-
#inspect ⇒ String
: () -> String.
-
#keyword ⇒ String
: () -> String.
-
#keyword_loc ⇒ Location
: () -> Location.
-
#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_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.
-
#save_unless_keyword_loc(repository) ⇒ Relocation::Entry
:category: Repository Save the unless_keyword_loc location using the given saved source so that it can be retrieved later.
-
#statements ⇒ StatementsNode?
:call-seq: statements -> StatementsNode | nil.
-
#then_keyword ⇒ String?
:call-seq: then_keyword -> String | nil.
-
#then_keyword_loc ⇒ Location?
:category: Locations :call-seq: then_keyword_loc -> Location | nil.
-
#type ⇒ :unless_node
See
Node#type. -
#unless_keyword ⇒ String
:call-seq: unless_keyword -> String.
-
#unless_keyword_loc ⇒ Location
:category: Locations :call-seq: unless_keyword_loc -> Location.
Constructor Details
#initialize(source, node_id, location, flags, unless_keyword_loc, predicate, then_keyword_loc, statements, else_clause, end_keyword_loc) ⇒ UnlessNode
Initialize a new UnlessNode node.
: (Source source, Integer node_id, Location location, Integer flags, Location unless_keyword_loc, Prism::node predicate, Location? then_keyword_loc, StatementsNode? statements, ElseNode? else_clause, Location? end_keyword_loc) -> void
32130 32131 32132 32133 32134 32135 32136 32137 32138 32139 32140 32141 |
# File 'lib/prism/node.rb', line 32130 def initialize(source, node_id, location, flags, unless_keyword_loc, predicate, then_keyword_loc, statements, else_clause, end_keyword_loc) @source = source @node_id = node_id @location = location @flags = flags @unless_keyword_loc = unless_keyword_loc @predicate = predicate @then_keyword_loc = then_keyword_loc @statements = statements @else_clause = else_clause @end_keyword_loc = end_keyword_loc end |
Class Method Details
.type ⇒ :unless_node
See Node.type.
: () -> :unless_node
32234 32235 32236 |
# File 'lib/prism/node.rb', line 32234 def self.type :unless_node end |
Instance Method Details
#===(other) ⇒ boolish
: (untyped other) -> boolish
32435 32436 32437 32438 32439 32440 32441 32442 32443 |
# File 'lib/prism/node.rb', line 32435 def ===(other) # :nodoc: other.is_a?(UnlessNode) && (unless_keyword_loc.nil? == other.unless_keyword_loc.nil?) && (predicate === other.predicate) && (then_keyword_loc.nil? == other.then_keyword_loc.nil?) && (statements === other.statements) && (else_clause === other.else_clause) && (end_keyword_loc.nil? == other.end_keyword_loc.nil?) end |
#accept(visitor) ⇒ Object
See Node.accept.
: (_Visitor visitor) -> untyped
32157 32158 32159 |
# File 'lib/prism/node.rb', line 32157 def accept(visitor) visitor.visit_unless_node(self) end |
#child_nodes ⇒ Array[node?] Also known as: deconstruct
See Node.child_nodes.
: () -> Array
32164 32165 32166 |
# File 'lib/prism/node.rb', line 32164 def child_nodes [predicate, statements, else_clause] end |
#compact_child_nodes ⇒ Array[node]
See Node.compact_child_nodes.
: () -> Array
32183 32184 32185 32186 32187 32188 32189 |
# File 'lib/prism/node.rb', line 32183 def compact_child_nodes compact = [] #: Array[Prism::node] compact << predicate if (statements = self.statements); compact << statements; end if (else_clause = self.else_clause); compact << else_clause; end compact end |
#copy(node_id: self.node_id, location: self.location, flags: self.flags, unless_keyword_loc: self.unless_keyword_loc, predicate: self.predicate, then_keyword_loc: self.then_keyword_loc, statements: self.statements, else_clause: self.else_clause, end_keyword_loc: self.end_keyword_loc) ⇒ UnlessNode
:call-seq:
copy(**fields) -> UnlessNode
Creates a copy of self with the given fields, using self as the template.
: (?node_id: Integer, ?location: Location, ?flags: Integer, ?unless_keyword_loc: Location, ?predicate: Prism::node, ?then_keyword_loc: Location?, ?statements: StatementsNode?, ?else_clause: ElseNode?, ?end_keyword_loc: Location?) -> UnlessNode
32197 32198 32199 |
# File 'lib/prism/node.rb', line 32197 def copy(node_id: self.node_id, location: self.location, flags: self.flags, unless_keyword_loc: self.unless_keyword_loc, predicate: self.predicate, then_keyword_loc: self.then_keyword_loc, statements: self.statements, else_clause: self.else_clause, end_keyword_loc: self.end_keyword_loc) UnlessNode.new(source, node_id, location, flags, unless_keyword_loc, predicate, then_keyword_loc, statements, else_clause, end_keyword_loc) end |
#deconstruct_keys(keys) ⇒ Hash[Symbol, untyped]
: (Array? keys) -> Hash[Symbol, untyped]
32204 32205 32206 32207 32208 32209 32210 32211 32212 32213 32214 32215 32216 32217 32218 32219 32220 32221 32222 |
# File 'lib/prism/node.rb', line 32204 def deconstruct_keys(keys) # :nodoc: (keys || %i[node_id location unless_keyword_loc predicate then_keyword_loc statements else_clause end_keyword_loc unless_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 :unless_keyword_loc then deconstructed[:unless_keyword_loc] = self.unless_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 :else_clause then deconstructed[:else_clause] = self.else_clause when :end_keyword_loc then deconstructed[:end_keyword_loc] = self.end_keyword_loc when :unless_keyword then deconstructed[:unless_keyword] = self.unless_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
32172 32173 32174 32175 32176 32177 32178 |
# File 'lib/prism/node.rb', line 32172 def each_child_node(&blk) return to_enum(:each_child_node) unless block_given? yield predicate if (statements = self.statements); yield statements; end if (else_clause = self.else_clause); yield else_clause; end end |
#else_clause ⇒ ElseNode?
:call-seq:
else_clause -> ElseNode | nil
The else clause of the unless expression, if present.
unless cond then bar else baz end
^^^^^^^^^^^^
: () -> ElseNode?
32362 32363 32364 |
# File 'lib/prism/node.rb', line 32362 def else_clause @else_clause end |
#end_keyword ⇒ String?
:call-seq:
end_keyword -> String | nil
Slice the location of end_keyword_loc from the source.
: () -> String?
32428 32429 32430 |
# File 'lib/prism/node.rb', line 32428 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.
unless cond then bar end
^^^
: () -> Location?
32380 32381 32382 32383 32384 32385 32386 32387 32388 32389 32390 |
# File 'lib/prism/node.rb', line 32380 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 |
#inspect ⇒ String
: () -> String
32239 32240 32241 |
# File 'lib/prism/node.rb', line 32239 def inspect # :nodoc: InspectVisitor.compose(self) end |
#keyword ⇒ String
: () -> String
426 427 428 |
# File 'lib/prism/node_ext.rb', line 426 def keyword # :nodoc unless_keyword end |
#keyword_loc ⇒ Location
: () -> Location
431 432 433 |
# File 'lib/prism/node_ext.rb', line 431 def keyword_loc # :nodoc unless_keyword_loc end |
#newline_flag!(lines) ⇒ void
This method returns an undefined value.
: (Array lines) -> void
250 251 252 |
# File 'lib/prism/parse_result/newlines.rb', line 250 def newline_flag!(lines) # :nodoc: predicate.newline_flag!(lines) end |
#predicate ⇒ Prism::node
:call-seq:
predicate -> Node
The condition to be evaluated for the unless expression. It can be any non-void expression.
unless cond then bar end
^^^^
bar unless cond
^^^^
: () -> Prism::node
32293 32294 32295 |
# File 'lib/prism/node.rb', line 32293 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?
32397 32398 32399 |
# File 'lib/prism/node.rb', line 32397 def save_end_keyword_loc(repository) repository.enter(node_id, :end_keyword_loc) unless @end_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?
32328 32329 32330 |
# File 'lib/prism/node.rb', line 32328 def save_then_keyword_loc(repository) repository.enter(node_id, :then_keyword_loc) unless @then_keyword_loc.nil? end |
#save_unless_keyword_loc(repository) ⇒ Relocation::Entry
:category: Repository Save the unless_keyword_loc location using the given saved source so that it can be retrieved later.
: (_Repository repository) -> Relocation::Entry
32273 32274 32275 |
# File 'lib/prism/node.rb', line 32273 def save_unless_keyword_loc(repository) repository.enter(node_id, :unless_keyword_loc) end |
#statements ⇒ StatementsNode?
:call-seq:
statements -> StatementsNode | nil
The body of statements that will executed if the unless condition is
falsey. Will be nil if no body is provided.
unless cond then bar end
^^^
: () -> StatementsNode?
32345 32346 32347 |
# File 'lib/prism/node.rb', line 32345 def statements @statements end |
#then_keyword ⇒ String?
:call-seq:
then_keyword -> String | nil
Slice the location of then_keyword_loc from the source.
: () -> String?
32418 32419 32420 |
# File 'lib/prism/node.rb', line 32418 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.
unless cond then bar end
^^^^
: () -> Location?
32311 32312 32313 32314 32315 32316 32317 32318 32319 32320 32321 |
# File 'lib/prism/node.rb', line 32311 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 ⇒ :unless_node
See Node#type.
: () -> :unless_node
32227 32228 32229 |
# File 'lib/prism/node.rb', line 32227 def type :unless_node end |
#unless_keyword ⇒ String
:call-seq:
unless_keyword -> String
Slice the location of unless_keyword_loc from the source.
: () -> String
32408 32409 32410 |
# File 'lib/prism/node.rb', line 32408 def unless_keyword unless_keyword_loc.slice end |
#unless_keyword_loc ⇒ Location
:category: Locations :call-seq:
unless_keyword_loc -> Location
The Location of the unless keyword.
unless cond then bar end
^^^^^^
bar unless cond
^^^^^^
: () -> Location
32262 32263 32264 32265 32266 |
# File 'lib/prism/node.rb', line 32262 def unless_keyword_loc location = @unless_keyword_loc return location if location.is_a?(Location) @unless_keyword_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF) end |