Class: Prism::IndexOperatorWriteNode
- Inherits:
-
PrismNode
- Object
- PrismNode
- Prism::IndexOperatorWriteNode
- 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
-
#arguments ⇒ Object
readonly
attr_reader arguments: ArgumentsNode?.
-
#block ⇒ Object
readonly
attr_reader block: Node?.
-
#call_operator_loc ⇒ Object
readonly
attr_reader call_operator_loc: Location?.
-
#closing_loc ⇒ Object
readonly
attr_reader closing_loc: Location.
-
#flags ⇒ Object
readonly
Returns the value of attribute flags.
-
#opening_loc ⇒ Object
readonly
attr_reader opening_loc: Location.
-
#operator ⇒ Object
readonly
attr_reader operator: Symbol.
-
#operator_loc ⇒ Object
readonly
attr_reader operator_loc: Location.
-
#receiver ⇒ Object
readonly
attr_reader receiver: Node?.
-
#value ⇒ Object
readonly
attr_reader value: Node.
Class Method Summary collapse
-
.type ⇒ Object
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.
Instance Method Summary collapse
-
#accept(visitor) ⇒ Object
def accept: (visitor: Visitor) -> void.
-
#attribute_write? ⇒ Boolean
def attribute_write?: () -> bool.
-
#call_operator ⇒ Object
def call_operator: () -> String?.
-
#child_nodes ⇒ Object
(also: #deconstruct)
def child_nodes: () -> Array[nil | Node].
-
#closing ⇒ Object
def closing: () -> String.
-
#comment_targets ⇒ Object
def comment_targets: () -> Array[Node | Location].
-
#compact_child_nodes ⇒ Object
def compact_child_nodes: () -> Array.
-
#copy(**params) ⇒ Object
def copy: (**params) -> IndexOperatorWriteNode.
- #deconstruct_keys(keys) ⇒ Object
-
#initialize(flags, receiver, call_operator_loc, opening_loc, arguments, closing_loc, block, operator, operator_loc, value, location) ⇒ IndexOperatorWriteNode
constructor
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.
-
#inspect(inspector = NodeInspector.new) ⇒ Object
def inspect(inspector: NodeInspector) -> String.
-
#opening ⇒ Object
def opening: () -> String.
-
#safe_navigation? ⇒ Boolean
def safe_navigation?: () -> bool.
-
#type ⇒ Object
Sometimes you want to check an instance of a node against a list of classes to see what kind of behavior to perform.
-
#variable_call? ⇒ Boolean
def variable_call?: () -> bool.
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
#arguments ⇒ Object (readonly)
attr_reader arguments: ArgumentsNode?
8458 8459 8460 |
# File 'lib/prism/node.rb', line 8458 def arguments @arguments end |
#block ⇒ Object (readonly)
attr_reader block: Node?
8464 8465 8466 |
# File 'lib/prism/node.rb', line 8464 def block @block end |
#call_operator_loc ⇒ Object (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_loc ⇒ Object (readonly)
attr_reader closing_loc: Location
8461 8462 8463 |
# File 'lib/prism/node.rb', line 8461 def closing_loc @closing_loc end |
#flags ⇒ Object (readonly)
Returns the value of attribute flags.
8446 8447 8448 |
# File 'lib/prism/node.rb', line 8446 def flags @flags end |
#opening_loc ⇒ Object (readonly)
attr_reader opening_loc: Location
8455 8456 8457 |
# File 'lib/prism/node.rb', line 8455 def opening_loc @opening_loc end |
#operator ⇒ Object (readonly)
attr_reader operator: Symbol
8467 8468 8469 |
# File 'lib/prism/node.rb', line 8467 def operator @operator end |
#operator_loc ⇒ Object (readonly)
attr_reader operator_loc: Location
8470 8471 8472 |
# File 'lib/prism/node.rb', line 8470 def operator_loc @operator_loc end |
#receiver ⇒ Object (readonly)
attr_reader receiver: Node?
8449 8450 8451 |
# File 'lib/prism/node.rb', line 8449 def receiver @receiver end |
#value ⇒ Object (readonly)
attr_reader value: Node
8473 8474 8475 |
# File 'lib/prism/node.rb', line 8473 def value @value end |
Class Method Details
.type ⇒ Object
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
8551 8552 8553 |
# File 'lib/prism/node.rb', line 8551 def attribute_write? flags.anybits?(CallNodeFlags::ATTRIBUTE_WRITE) end |
#call_operator ⇒ Object
def call_operator: () -> String?
8556 8557 8558 |
# File 'lib/prism/node.rb', line 8556 def call_operator call_operator_loc&.slice end |
#child_nodes ⇒ Object 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 |
#closing ⇒ Object
def closing: () -> String
8566 8567 8568 |
# File 'lib/prism/node.rb', line 8566 def closing closing_loc.slice end |
#comment_targets ⇒ Object
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_nodes ⇒ Object
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
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 ), ("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 |
#opening ⇒ Object
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
8541 8542 8543 |
# File 'lib/prism/node.rb', line 8541 def flags.anybits?(CallNodeFlags::SAFE_NAVIGATION) end |
#type ⇒ Object
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
8546 8547 8548 |
# File 'lib/prism/node.rb', line 8546 def variable_call? flags.anybits?(CallNodeFlags::VARIABLE_CALL) end |