Class: Prism::CallOrWriteNode

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.

foo.bar ||= value
^^^^^^^^^^^^^^^^^

Explore code sample

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, node_id, location, flags, receiver, call_operator_loc, message_loc, read_name, write_name, operator_loc, value) ⇒ CallOrWriteNode

Initialize a new CallOrWriteNode node.

: (Source source, Integer node_id, Location location, Integer flags, Prism::node? receiver, Location? call_operator_loc, Location? message_loc, Symbol read_name, Symbol write_name, Location operator_loc, Prism::node value) -> void

Parameters:

  • source (Source)
  • node_id (Integer)
  • location (Location)
  • flags (Integer)
  • receiver (Prism::node, nil)
  • call_operator_loc (Location, nil)
  • message_loc (Location, nil)
  • read_name (Symbol)
  • write_name (Symbol)
  • operator_loc (Location)
  • value (Prism::node)


5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
# File 'lib/prism/node.rb', line 5456

def initialize(source, node_id, location, flags, receiver, call_operator_loc, message_loc, read_name, write_name, operator_loc, value)
  @source = source
  @node_id = node_id
  @location = location
  @flags = flags
  @receiver = receiver
  @call_operator_loc = call_operator_loc
  @message_loc = message_loc
  @read_name = read_name
  @write_name = write_name
  @operator_loc = operator_loc
  @value = value
end

Class Method Details

.type ⇒ :call_or_write_node

See Node.type.

: () -> :call_or_write_node

Returns:

  • (:call_or_write_node)


5560
5561
5562
# File 'lib/prism/node.rb', line 5560

def self.type
  :call_or_write_node
end

Instance Method Details

#===(other) ⇒ boolish

: (untyped other) -> boolish

Parameters:

  • other (Object)

Returns:

  • (boolish)


5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
# File 'lib/prism/node.rb', line 5803

def ===(other) # :nodoc:
  other.is_a?(CallOrWriteNode) &&
    (flags === other.flags) &&
    (receiver === other.receiver) &&
    (call_operator_loc.nil? == other.call_operator_loc.nil?) &&
    (message_loc.nil? == other.message_loc.nil?) &&
    (read_name === other.read_name) &&
    (write_name === other.write_name) &&
    (operator_loc.nil? == other.operator_loc.nil?) &&
    (value === other.value)
end

#accept(visitor) ⇒ Object

See Node.accept.

: (_Visitor visitor) -> untyped

Parameters:

Returns:

  • (Object)


5484
5485
5486
# File 'lib/prism/node.rb', line 5484

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

#attribute_write? ⇒ Boolean

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

: () -> bool

Returns:

  • (Boolean)


5591
5592
5593
# File 'lib/prism/node.rb', line 5591

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

#call_operator ⇒ String?

:call-seq:

call_operator -> String | nil

Slice the location of call_operator_loc from the source.

: () -> String?

Returns:

  • (String, nil)


5776
5777
5778
# File 'lib/prism/node.rb', line 5776

def call_operator
  call_operator_loc&.slice
end

#call_operator_loc ⇒ Location?

:category: Locations :call-seq:

call_operator_loc -> Location | nil

Represents the Location of the call operator.

foo.bar ||= value
   ^

Explore code sample

: () -> Location?

Returns:



5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
# File 'lib/prism/node.rb', line 5634

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?])


5491
5492
5493
# File 'lib/prism/node.rb', line 5491

def child_nodes
  [receiver, value]
end

#compact_child_nodes ⇒ Array[node]

See Node.compact_child_nodes.

: () -> Array

Returns:

  • (Array[node])


5509
5510
5511
5512
5513
5514
# File 'lib/prism/node.rb', line 5509

def compact_child_nodes
  compact = [] #: Array[Prism::node]
  if (receiver = self.receiver); compact << receiver; 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, message_loc: self.message_loc, read_name: self.read_name, write_name: self.write_name, operator_loc: self.operator_loc, value: self.value) ⇒ CallOrWriteNode

:call-seq:

copy(**fields) -> CallOrWriteNode

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?, ?message_loc: Location?, ?read_name: Symbol, ?write_name: Symbol, ?operator_loc: Location, ?value: Prism::node) -> CallOrWriteNode

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)
  • message_loc: (Location, nil) (defaults to: self.message_loc)
  • read_name: (Symbol) (defaults to: self.read_name)
  • write_name: (Symbol) (defaults to: self.write_name)
  • operator_loc: (Location) (defaults to: self.operator_loc)
  • value: (Prism::node) (defaults to: self.value)

Returns:



5522
5523
5524
# File 'lib/prism/node.rb', line 5522

def copy(node_id: self.node_id, location: self.location, flags: self.flags, receiver: self.receiver, call_operator_loc: self.call_operator_loc, message_loc: self.message_loc, read_name: self.read_name, write_name: self.write_name, operator_loc: self.operator_loc, value: self.value)
  CallOrWriteNode.new(source, node_id, location, flags, receiver, call_operator_loc, message_loc, read_name, write_name, operator_loc, value)
end

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

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

Parameters:

  • keys (Array[Symbol], nil)

Returns:

  • (Hash[Symbol, untyped])


5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
# File 'lib/prism/node.rb', line 5529

def deconstruct_keys(keys) # :nodoc:
  (keys || %i[node_id location receiver call_operator_loc message_loc read_name write_name operator_loc value call_operator message 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 :message_loc             then deconstructed[:message_loc] = self.message_loc
    when :read_name               then deconstructed[:read_name] = self.read_name
    when :write_name              then deconstructed[:write_name] = self.write_name
    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 :message                 then deconstructed[:message] = self.message
    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)


5499
5500
5501
5502
5503
5504
# File 'lib/prism/node.rb', line 5499

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

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

#ignore_visibility? ⇒ Boolean

:category: Flags a call that ignores method visibility

: () -> bool

Returns:

  • (Boolean)


5599
5600
5601
# File 'lib/prism/node.rb', line 5599

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

#inspect ⇒ String

: () -> String

Returns:

  • (String)


5565
5566
5567
# File 'lib/prism/node.rb', line 5565

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

#message ⇒ String?

:call-seq:

message -> String | nil

Slice the location of message_loc from the source.

: () -> String?

Returns:

  • (String, nil)


5786
5787
5788
# File 'lib/prism/node.rb', line 5786

def message
  message_loc&.slice
end

#message_loc ⇒ Location?

:category: Locations :call-seq:

message_loc -> Location | nil

Represents the Location of the message.

foo.bar ||= value
    ^^^

Explore code sample

: () -> Location?

Returns:



5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
# File 'lib/prism/node.rb', line 5668

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

#operator ⇒ String

:call-seq:

operator -> String

Slice the location of operator_loc from the source.

: () -> String

Returns:

  • (String)


5796
5797
5798
# File 'lib/prism/node.rb', line 5796

def operator
  operator_loc.slice
end

#operator_loc ⇒ Location

:category: Locations :call-seq:

operator_loc -> Location

Represents the Location of the operator.

foo.bar ||= value
        ^^^

Explore code sample

: () -> Location

Returns:



5736
5737
5738
5739
5740
# File 'lib/prism/node.rb', line 5736

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

#read_name ⇒ Symbol

:call-seq:

read_name -> Symbol

Represents the name of the method being called.

foo.bar ||= value # read_name `:bar`
    ^^^

Explore code sample

: () -> Symbol

Returns:

  • (Symbol)


5701
5702
5703
# File 'lib/prism/node.rb', line 5701

def read_name
  @read_name
end

#receiver ⇒ Prism::node?

:call-seq:

receiver -> Node | nil

The object that the method is being called on. This can be either nil or any non-void expressions.

foo.bar ||= value
^^^

Explore code sample

: () -> Prism::node?

Returns:

  • (Prism::node, nil)


5616
5617
5618
# File 'lib/prism/node.rb', line 5616

def receiver
  @receiver
end

#safe_navigation? ⇒ Boolean

:category: Flags &. operator

: () -> bool

Returns:

  • (Boolean)


5575
5576
5577
# File 'lib/prism/node.rb', line 5575

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:



5651
5652
5653
# File 'lib/prism/node.rb', line 5651

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

#save_message_loc(repository) ⇒ Relocation::Entry?

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

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

Parameters:

Returns:



5685
5686
5687
# File 'lib/prism/node.rb', line 5685

def save_message_loc(repository)
  repository.enter(node_id, :message_loc) unless @message_loc.nil?
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:



5747
5748
5749
# File 'lib/prism/node.rb', line 5747

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

#type ⇒ :call_or_write_node

See Node#type.

: () -> :call_or_write_node

Returns:

  • (:call_or_write_node)


5553
5554
5555
# File 'lib/prism/node.rb', line 5553

def type
  :call_or_write_node
end

#value ⇒ Prism::node

:call-seq:

value -> Node

Represents the value being assigned.

foo.bar ||= value
            ^^^^^

Explore code sample

: () -> Prism::node

Returns:

  • (Prism::node)


5764
5765
5766
# File 'lib/prism/node.rb', line 5764

def value
  @value
end

#variable_call? ⇒ Boolean

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

: () -> bool

Returns:

  • (Boolean)


5583
5584
5585
# File 'lib/prism/node.rb', line 5583

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

#write_name ⇒ Symbol

:call-seq:

write_name -> Symbol

Represents the name of the method being written to.

foo.bar ||= value # write_name `:bar=`
    ^^^

Explore code sample

: () -> Symbol

Returns:

  • (Symbol)


5718
5719
5720
# File 'lib/prism/node.rb', line 5718

def write_name
  @write_name
end