Class: Prism::IndexOperatorWriteNode

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

Overview

Represents the use of an assignment operator on a call to ‘[]`.

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(flags, receiver, call_operator_loc, opening_loc, arguments, closing_loc, block, operator, operator_loc, value, location) ⇒ IndexOperatorWriteNode

def initialize: (flags: Integer, receiver: Node?, call_operator_loc: Location?, opening_loc: Location, arguments: ArgumentsNode?, closing_loc: Location, block: Node?, operator: Symbol, operator_loc: Location, value: Node, location: Location) -> void



8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
# File 'lib/prism/node.rb', line 8476

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

Instance Attribute Details

#argumentsObject (readonly)

attr_reader arguments: ArgumentsNode?



8458
8459
8460
# File 'lib/prism/node.rb', line 8458

def arguments
  @arguments
end

#blockObject (readonly)

attr_reader block: Node?



8464
8465
8466
# File 'lib/prism/node.rb', line 8464

def block
  @block
end

#call_operator_locObject (readonly)

attr_reader call_operator_loc: Location?



8452
8453
8454
# File 'lib/prism/node.rb', line 8452

def call_operator_loc
  @call_operator_loc
end

#closing_locObject (readonly)

attr_reader closing_loc: Location



8461
8462
8463
# File 'lib/prism/node.rb', line 8461

def closing_loc
  @closing_loc
end

#flagsObject (readonly)

Returns the value of attribute flags.



8446
8447
8448
# File 'lib/prism/node.rb', line 8446

def flags
  @flags
end

#opening_locObject (readonly)

attr_reader opening_loc: Location



8455
8456
8457
# File 'lib/prism/node.rb', line 8455

def opening_loc
  @opening_loc
end

#operatorObject (readonly)

attr_reader operator: Symbol



8467
8468
8469
# File 'lib/prism/node.rb', line 8467

def operator
  @operator
end

#operator_locObject (readonly)

attr_reader operator_loc: Location



8470
8471
8472
# File 'lib/prism/node.rb', line 8470

def operator_loc
  @operator_loc
end

#receiverObject (readonly)

attr_reader receiver: Node?



8449
8450
8451
# File 'lib/prism/node.rb', line 8449

def receiver
  @receiver
end

#valueObject (readonly)

attr_reader value: Node



8473
8474
8475
# File 'lib/prism/node.rb', line 8473

def value
  @value
end

Class Method Details

.typeObject

Similar to #type, this method returns a symbol that you can use for splitting on the type of the node without having to do a long === chain. Note that like #type, it will still be slower than using == for a single class, but should be faster in a case statement or an array comparison.

def self.type: () -> Symbol



8627
8628
8629
# File 'lib/prism/node.rb', line 8627

def self.type
  :index_operator_write_node
end

Instance Method Details

#accept(visitor) ⇒ Object

def accept: (visitor: Visitor) -> void



8491
8492
8493
# File 'lib/prism/node.rb', line 8491

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

#attribute_write?Boolean

def attribute_write?: () -> bool

Returns:

  • (Boolean)


8551
8552
8553
# File 'lib/prism/node.rb', line 8551

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

#call_operatorObject

def call_operator: () -> String?



8556
8557
8558
# File 'lib/prism/node.rb', line 8556

def call_operator
  call_operator_loc&.slice
end

#child_nodesObject Also known as: deconstruct

def child_nodes: () -> Array[nil | Node]



8496
8497
8498
# File 'lib/prism/node.rb', line 8496

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

#closingObject

def closing: () -> String



8566
8567
8568
# File 'lib/prism/node.rb', line 8566

def closing
  closing_loc.slice
end

#comment_targetsObject

def comment_targets: () -> Array[Node | Location]



8511
8512
8513
# File 'lib/prism/node.rb', line 8511

def comment_targets
  [*receiver, *call_operator_loc, opening_loc, *arguments, closing_loc, *block, operator_loc, value]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



8501
8502
8503
8504
8505
8506
8507
8508
# File 'lib/prism/node.rb', line 8501

def compact_child_nodes
  compact = []
  compact << receiver if receiver
  compact << arguments if arguments
  compact << block if block
  compact << value
  compact
end

#copy(**params) ⇒ Object

def copy: (**params) -> IndexOperatorWriteNode



8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
# File 'lib/prism/node.rb', line 8516

def copy(**params)
  IndexOperatorWriteNode.new(
    params.fetch(:flags) { flags },
    params.fetch(:receiver) { receiver },
    params.fetch(:call_operator_loc) { call_operator_loc },
    params.fetch(:opening_loc) { opening_loc },
    params.fetch(:arguments) { arguments },
    params.fetch(:closing_loc) { closing_loc },
    params.fetch(:block) { block },
    params.fetch(:operator) { operator },
    params.fetch(:operator_loc) { operator_loc },
    params.fetch(:value) { value },
    params.fetch(:location) { location },
  )
end

#deconstruct_keys(keys) ⇒ Object

def deconstruct_keys: (keys: Array) -> Hash[Symbol, nil | Node | Array | String | Token | Array | Location]



8536
8537
8538
# File 'lib/prism/node.rb', line 8536

def deconstruct_keys(keys)
  { flags: flags, receiver: receiver, call_operator_loc: call_operator_loc, opening_loc: opening_loc, arguments: arguments, closing_loc: closing_loc, block: block, operator: operator, operator_loc: operator_loc, value: value, location: location }
end

#inspect(inspector = NodeInspector.new) ⇒ Object

def inspect(inspector: NodeInspector) -> String



8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
# File 'lib/prism/node.rb', line 8571

def inspect(inspector = NodeInspector.new)
  inspector << inspector.header(self)
  flags = [("safe_navigation" if safe_navigation?), ("variable_call" if variable_call?), ("attribute_write" if attribute_write?)].compact
  inspector << "├── flags: #{flags.empty? ? "" : flags.join(", ")}\n"
  if (receiver = self.receiver).nil?
    inspector << "├── receiver: ∅\n"
  else
    inspector << "├── receiver:\n"
    inspector << receiver.inspect(inspector.child_inspector("")).delete_prefix(inspector.prefix)
  end
  inspector << "├── call_operator_loc: #{inspector.location(call_operator_loc)}\n"
  inspector << "├── opening_loc: #{inspector.location(opening_loc)}\n"
  if (arguments = self.arguments).nil?
    inspector << "├── arguments: ∅\n"
  else
    inspector << "├── arguments:\n"
    inspector << arguments.inspect(inspector.child_inspector("")).delete_prefix(inspector.prefix)
  end
  inspector << "├── closing_loc: #{inspector.location(closing_loc)}\n"
  if (block = self.block).nil?
    inspector << "├── block: ∅\n"
  else
    inspector << "├── block:\n"
    inspector << block.inspect(inspector.child_inspector("")).delete_prefix(inspector.prefix)
  end
  inspector << "├── operator: #{operator.inspect}\n"
  inspector << "├── operator_loc: #{inspector.location(operator_loc)}\n"
  inspector << "└── value:\n"
  inspector << inspector.child_node(value, "    ")
  inspector.to_str
end

#openingObject

def opening: () -> String



8561
8562
8563
# File 'lib/prism/node.rb', line 8561

def opening
  opening_loc.slice
end

#safe_navigation?Boolean

def safe_navigation?: () -> bool

Returns:

  • (Boolean)


8541
8542
8543
# File 'lib/prism/node.rb', line 8541

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

#typeObject

Sometimes you want to check an instance of a node against a list of classes to see what kind of behavior to perform. Usually this is done by calling ‘[cls1, cls2].include?(node.class)` or putting the node into a case statement and doing `case node; when cls1; when cls2; end`. Both of these approaches are relatively slow because of the constant lookups, method calls, and/or array allocations.

Instead, you can call #type, which will return to you a symbol that you can use for comparison. This is faster than the other approaches because it uses a single integer comparison, but also because if you’re on CRuby you can take advantage of the fact that case statements with all symbol keys will use a jump table.

def type: () -> Symbol



8617
8618
8619
# File 'lib/prism/node.rb', line 8617

def type
  :index_operator_write_node
end

#variable_call?Boolean

def variable_call?: () -> bool

Returns:

  • (Boolean)


8546
8547
8548
# File 'lib/prism/node.rb', line 8546

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