Class: Prism::IndexOrWriteNode

Inherits:
PrismNode
  • Object
show all
Defined in:
lib/prism/node.rb,
sig/generated/prism/node.rbs,
ext/prism/api_node.c

Overview

Represents the use of the ||= operator on a call to [].

foo.bar[baz] ||= value
^^^^^^^^^^^^^^^^^^^^^^

Explore code sample

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, node_id, location, flags, receiver, call_operator_loc, opening_loc, arguments, closing_loc, block, operator_loc, value) ⇒ IndexOrWriteNode

Initialize a new IndexOrWriteNode node.

: (Source source, Integer node_id, Location location, Integer flags, Prism::node? receiver, Location? call_operator_loc, Location opening_loc, ArgumentsNode? arguments, Location closing_loc, BlockArgumentNode? block, Location operator_loc, Prism::node value) -> void

Parameters:



17349
17350
17351
17352
17353
17354
17355
17356
17357
17358
17359
17360
17361
17362
# File 'lib/prism/node.rb', line 17349

def initialize(source, node_id, location, flags, receiver, call_operator_loc, opening_loc, arguments, closing_loc, block, operator_loc, value)
  @source = source
  @node_id = node_id
  @location = location
  @flags = flags
  @receiver = receiver
  @call_operator_loc = call_operator_loc
  @opening_loc = opening_loc
  @arguments = arguments
  @closing_loc = closing_loc
  @block = block
  @operator_loc = operator_loc
  @value = value
end

Class Method Details

.type ⇒ :index_or_write_node

See Node.type.

: () -> :index_or_write_node

Returns:

  • (:index_or_write_node)


17460
17461
17462
# File 'lib/prism/node.rb', line 17460

def self.type
  :index_or_write_node
end

Instance Method Details

#===(other) ⇒ boolish

: (untyped other) -> boolish

Parameters:

  • other (Object)

Returns:

  • (boolish)


17681
17682
17683
17684
17685
17686
17687
17688
17689
17690
17691
17692
# File 'lib/prism/node.rb', line 17681

def ===(other) # :nodoc:
  other.is_a?(IndexOrWriteNode) &&
    (flags === other.flags) &&
    (receiver === other.receiver) &&
    (call_operator_loc.nil? == other.call_operator_loc.nil?) &&
    (opening_loc.nil? == other.opening_loc.nil?) &&
    (arguments === other.arguments) &&
    (closing_loc.nil? == other.closing_loc.nil?) &&
    (block === other.block) &&
    (operator_loc.nil? == other.operator_loc.nil?) &&
    (value === other.value)
end

#accept(visitor) ⇒ Object

See Node.accept.

: (_Visitor visitor) -> untyped

Parameters:

Returns:

  • (Object)


17378
17379
17380
# File 'lib/prism/node.rb', line 17378

def accept(visitor)
  visitor.visit_index_or_write_node(self)
end

#arguments ⇒ ArgumentsNode?

:call-seq:

arguments -> ArgumentsNode | nil

Returns the arguments attribute.

: () -> ArgumentsNode?

Returns:



17568
17569
17570
# File 'lib/prism/node.rb', line 17568

def arguments
  @arguments
end

#attribute_write? ⇒ Boolean

:category: Flags a call that is an attribute write, so the value being written should be returned

: () -> bool

Returns:

  • (Boolean)


17491
17492
17493
# File 'lib/prism/node.rb', line 17491

def attribute_write?
  flags.anybits?(CallNodeFlags::ATTRIBUTE_WRITE)
end

#block ⇒ BlockArgumentNode?

:call-seq:

block -> BlockArgumentNode | nil

Returns the block attribute.

: () -> BlockArgumentNode?

Returns:



17600
17601
17602
# File 'lib/prism/node.rb', line 17600

def block
  @block
end

#call_operator ⇒ String?

:call-seq:

call_operator -> String | nil

Slice the location of call_operator_loc from the source.

: () -> String?

Returns:

  • (String, nil)


17644
17645
17646
# File 'lib/prism/node.rb', line 17644

def call_operator
  call_operator_loc&.slice
end

#call_operator_loc ⇒ Location?

:category: Locations :call-seq:

call_operator_loc -> Location | nil

Returns the Location represented by call_operator_loc.

: () -> Location?

Returns:



17520
17521
17522
17523
17524
17525
17526
17527
17528
17529
17530
# File 'lib/prism/node.rb', line 17520

def call_operator_loc
  location = @call_operator_loc
  case location
  when nil
    nil
  when Location
    location
  else
    @call_operator_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF)
  end
end

#child_nodes ⇒ Array[node?] Also known as: deconstruct

See Node.child_nodes.

: () -> Array

Returns:

  • (Array[node?])


17385
17386
17387
# File 'lib/prism/node.rb', line 17385

def child_nodes
  [receiver, arguments, block, value]
end

#closing ⇒ String

:call-seq:

closing -> String

Slice the location of closing_loc from the source.

: () -> String

Returns:

  • (String)


17664
17665
17666
# File 'lib/prism/node.rb', line 17664

def closing
  closing_loc.slice
end

#closing_loc ⇒ Location

:category: Locations :call-seq:

closing_loc -> Location

Returns the Location represented by closing_loc.

: () -> Location

Returns:



17579
17580
17581
17582
17583
# File 'lib/prism/node.rb', line 17579

def closing_loc
  location = @closing_loc
  return location if location.is_a?(Location)
  @closing_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF)
end

#compact_child_nodes ⇒ Array[node]

See Node.compact_child_nodes.

: () -> Array

Returns:

  • (Array[node])


17405
17406
17407
17408
17409
17410
17411
17412
# File 'lib/prism/node.rb', line 17405

def compact_child_nodes
  compact = [] #: Array[Prism::node]
  if (receiver = self.receiver); compact << receiver; end
  if (arguments = self.arguments); compact << arguments; end
  if (block = self.block); compact << block; end
  compact << value
  compact
end

#copy(node_id: self.node_id, location: self.location, flags: self.flags, receiver: self.receiver, call_operator_loc: self.call_operator_loc, opening_loc: self.opening_loc, arguments: self.arguments, closing_loc: self.closing_loc, block: self.block, operator_loc: self.operator_loc, value: self.value) ⇒ IndexOrWriteNode

:call-seq:

copy(**fields) -> IndexOrWriteNode

Creates a copy of self with the given fields, using self as the template.

: (?node_id: Integer, ?location: Location, ?flags: Integer, ?receiver: Prism::node?, ?call_operator_loc: Location?, ?opening_loc: Location, ?arguments: ArgumentsNode?, ?closing_loc: Location, ?block: BlockArgumentNode?, ?operator_loc: Location, ?value: Prism::node) -> IndexOrWriteNode

Parameters:

  • node_id: (Integer) (defaults to: self.node_id)
  • location: (Location) (defaults to: self.location)
  • flags: (Integer) (defaults to: self.flags)
  • receiver: (Prism::node, nil) (defaults to: self.receiver)
  • call_operator_loc: (Location, nil) (defaults to: self.call_operator_loc)
  • opening_loc: (Location) (defaults to: self.opening_loc)
  • arguments: (ArgumentsNode, nil) (defaults to: self.arguments)
  • closing_loc: (Location) (defaults to: self.closing_loc)
  • block: (BlockArgumentNode, nil) (defaults to: self.block)
  • operator_loc: (Location) (defaults to: self.operator_loc)
  • value: (Prism::node) (defaults to: self.value)

Returns:



17420
17421
17422
# File 'lib/prism/node.rb', line 17420

def copy(node_id: self.node_id, location: self.location, flags: self.flags, receiver: self.receiver, call_operator_loc: self.call_operator_loc, opening_loc: self.opening_loc, arguments: self.arguments, closing_loc: self.closing_loc, block: self.block, operator_loc: self.operator_loc, value: self.value)
  IndexOrWriteNode.new(source, node_id, location, flags, receiver, call_operator_loc, opening_loc, arguments, closing_loc, block, operator_loc, value)
end

#deconstruct_keys(keys) ⇒ Hash[Symbol, untyped]

: (Array? keys) -> Hash[Symbol, untyped]

Parameters:

  • keys (Array[Symbol], nil)

Returns:

  • (Hash[Symbol, untyped])


17427
17428
17429
17430
17431
17432
17433
17434
17435
17436
17437
17438
17439
17440
17441
17442
17443
17444
17445
17446
17447
17448
# File 'lib/prism/node.rb', line 17427

def deconstruct_keys(keys) # :nodoc:
  (keys || %i[node_id location receiver call_operator_loc opening_loc arguments closing_loc block operator_loc value call_operator opening closing operator]).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 :receiver                then deconstructed[:receiver] = self.receiver
    when :call_operator_loc       then deconstructed[:call_operator_loc] = self.call_operator_loc
    when :opening_loc             then deconstructed[:opening_loc] = self.opening_loc
    when :arguments               then deconstructed[:arguments] = self.arguments
    when :closing_loc             then deconstructed[:closing_loc] = self.closing_loc
    when :block                   then deconstructed[:block] = self.block
    when :operator_loc            then deconstructed[:operator_loc] = self.operator_loc
    when :value                   then deconstructed[:value] = self.value
    when :call_operator           then deconstructed[:call_operator] = self.call_operator
    when :opening                 then deconstructed[:opening] = self.opening
    when :closing                 then deconstructed[:closing] = self.closing
    when :operator                then deconstructed[:operator] = self.operator
    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)


17393
17394
17395
17396
17397
17398
17399
17400
# File 'lib/prism/node.rb', line 17393

def each_child_node(&blk)
  return to_enum(:each_child_node) unless block_given?

  if (receiver = self.receiver); yield receiver; end
  if (arguments = self.arguments); yield arguments; end
  if (block = self.block); yield block; end
  yield value
end

#ignore_visibility? ⇒ Boolean

:category: Flags a call that ignores method visibility

: () -> bool

Returns:

  • (Boolean)


17499
17500
17501
# File 'lib/prism/node.rb', line 17499

def ignore_visibility?
  flags.anybits?(CallNodeFlags::IGNORE_VISIBILITY)
end

#inspect ⇒ String

: () -> String

Returns:

  • (String)


17465
17466
17467
# File 'lib/prism/node.rb', line 17465

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

#opening ⇒ String

:call-seq:

opening -> String

Slice the location of opening_loc from the source.

: () -> String

Returns:

  • (String)


17654
17655
17656
# File 'lib/prism/node.rb', line 17654

def opening
  opening_loc.slice
end

#opening_loc ⇒ Location

:category: Locations :call-seq:

opening_loc -> Location

Returns the Location represented by opening_loc.

: () -> Location

Returns:



17547
17548
17549
17550
17551
# File 'lib/prism/node.rb', line 17547

def opening_loc
  location = @opening_loc
  return location if location.is_a?(Location)
  @opening_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF)
end

#operator ⇒ String

:call-seq:

operator -> String

Slice the location of operator_loc from the source.

: () -> String

Returns:

  • (String)


17674
17675
17676
# File 'lib/prism/node.rb', line 17674

def operator
  operator_loc.slice
end

#operator_loc ⇒ Location

:category: Locations :call-seq:

operator_loc -> Location

Returns the Location represented by operator_loc.

: () -> Location

Returns:



17611
17612
17613
17614
17615
# File 'lib/prism/node.rb', line 17611

def operator_loc
  location = @operator_loc
  return location if location.is_a?(Location)
  @operator_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF)
end

#receiver ⇒ Prism::node?

:call-seq:

receiver -> Node | nil

Returns the receiver attribute.

: () -> Prism::node?

Returns:

  • (Prism::node, nil)


17509
17510
17511
# File 'lib/prism/node.rb', line 17509

def receiver
  @receiver
end

#safe_navigation? ⇒ Boolean

:category: Flags &. operator

: () -> bool

Returns:

  • (Boolean)


17475
17476
17477
# File 'lib/prism/node.rb', line 17475

def safe_navigation?
  flags.anybits?(CallNodeFlags::SAFE_NAVIGATION)
end

#save_call_operator_loc(repository) ⇒ Relocation::Entry?

:category: Repository Save the call_operator_loc location using the given saved source so that it can be retrieved later.

: (_Repository repository) -> Relocation::Entry?

Parameters:

Returns:



17537
17538
17539
# File 'lib/prism/node.rb', line 17537

def save_call_operator_loc(repository)
  repository.enter(node_id, :call_operator_loc) unless @call_operator_loc.nil?
end

#save_closing_loc(repository) ⇒ Relocation::Entry

:category: Repository Save the closing_loc location using the given saved source so that it can be retrieved later.

: (_Repository repository) -> Relocation::Entry

Parameters:

Returns:



17590
17591
17592
# File 'lib/prism/node.rb', line 17590

def save_closing_loc(repository)
  repository.enter(node_id, :closing_loc)
end

#save_opening_loc(repository) ⇒ Relocation::Entry

:category: Repository Save the opening_loc location using the given saved source so that it can be retrieved later.

: (_Repository repository) -> Relocation::Entry

Parameters:

Returns:



17558
17559
17560
# File 'lib/prism/node.rb', line 17558

def save_opening_loc(repository)
  repository.enter(node_id, :opening_loc)
end

#save_operator_loc(repository) ⇒ Relocation::Entry

:category: Repository Save the operator_loc location using the given saved source so that it can be retrieved later.

: (_Repository repository) -> Relocation::Entry

Parameters:

Returns:



17622
17623
17624
# File 'lib/prism/node.rb', line 17622

def save_operator_loc(repository)
  repository.enter(node_id, :operator_loc)
end

#type ⇒ :index_or_write_node

See Node#type.

: () -> :index_or_write_node

Returns:

  • (:index_or_write_node)


17453
17454
17455
# File 'lib/prism/node.rb', line 17453

def type
  :index_or_write_node
end

#value ⇒ Prism::node

:call-seq:

value -> Node

Returns the value attribute.

: () -> Prism::node

Returns:

  • (Prism::node)


17632
17633
17634
# File 'lib/prism/node.rb', line 17632

def value
  @value
end

#variable_call? ⇒ Boolean

:category: Flags a call that could have been a local variable

: () -> bool

Returns:

  • (Boolean)


17483
17484
17485
# File 'lib/prism/node.rb', line 17483

def variable_call?
  flags.anybits?(CallNodeFlags::VARIABLE_CALL)
end