Class: Prism::UnlessNode

Inherits:
PrismNode
  • Object
show all
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
^^^^^^^^^^^^^^^^^^^^^^^

Explore code sample

Class Method Summary collapse

Instance Method Summary collapse

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

Parameters:



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

Returns:

  • (: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

Parameters:

  • other (Object)

Returns:

  • (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

Parameters:

Returns:

  • (Object)


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

Returns:

  • (Array[node?])


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

Returns:

  • (Array[node])


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

Parameters:

  • node_id: (Integer) (defaults to: self.node_id)
  • location: (Location) (defaults to: self.location)
  • flags: (Integer) (defaults to: self.flags)
  • unless_keyword_loc: (Location) (defaults to: self.unless_keyword_loc)
  • predicate: (Prism::node) (defaults to: self.predicate)
  • then_keyword_loc: (Location, nil) (defaults to: self.then_keyword_loc)
  • statements: (StatementsNode, nil) (defaults to: self.statements)
  • else_clause: (ElseNode, nil) (defaults to: self.else_clause)
  • end_keyword_loc: (Location, nil) (defaults to: self.end_keyword_loc)

Returns:



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]

Parameters:

  • keys (Array[Symbol], nil)

Returns:

  • (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

Overloads:

  • #each_child_node ⇒ Enumerator[node, void]

    Returns:

    • (Enumerator[node, void])
  • #each_child_node ⇒ void

    This method returns an undefined value.

Yields:

Yield Parameters:

  • arg0 (node)

Yield Returns:

  • (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
                     ^^^^^^^^^^^^

Explore code sample

: () -> ElseNode?

Returns:



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?

Returns:

  • (String, nil)


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
                     ^^^

Explore code sample

: () -> Location?

Returns:



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

Returns:

  • (String)


32239
32240
32241
# File 'lib/prism/node.rb', line 32239

def inspect # :nodoc:
  InspectVisitor.compose(self)
end

#keyword ⇒ String

: () -> String

Returns:

  • (String)


426
427
428
# File 'lib/prism/node_ext.rb', line 426

def keyword # :nodoc
  unless_keyword
end

#keyword_loc ⇒ Location

: () -> Location

Returns:



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

Parameters:

  • lines (Array[bool])


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
           ^^^^

Explore code sample

: () -> Prism::node

Returns:

  • (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?

Parameters:

Returns:



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?

Parameters:

Returns:



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

Parameters:

Returns:



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
                 ^^^

Explore code sample

: () -> StatementsNode?

Returns:



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?

Returns:

  • (String, nil)


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
            ^^^^

Explore code sample

: () -> Location?

Returns:



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

Returns:

  • (: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

Returns:

  • (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
    ^^^^^^

Explore code sample

: () -> Location

Returns:



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