Class: SyntaxTree::ARefField

Inherits:
Node
  • Object
show all
Defined in:
lib/syntax_tree/node.rb

Overview

ARefField represents assigning values into collections at specific indices. Put another way, it’s any time you’re calling the method #[]=. The ARefField node itself is just the left side of the assignment, and they’re always wrapped in assign nodes.

collection[index] = value

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#construct_keys, #pretty_print, #to_json

Constructor Details

#initialize(collection:, index:, location:, comments: []) ⇒ ARefField

Returns a new instance of ARefField.



517
518
519
520
521
522
# File 'lib/syntax_tree/node.rb', line 517

def initialize(collection:, index:, location:, comments: [])
  @collection = collection
  @index = index
  @location = location
  @comments = comments
end

Instance Attribute Details

#collectionObject (readonly)

untyped

the value being indexed



509
510
511
# File 'lib/syntax_tree/node.rb', line 509

def collection
  @collection
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



515
516
517
# File 'lib/syntax_tree/node.rb', line 515

def comments
  @comments
end

#indexObject (readonly)

nil | Args

the value being passed within the brackets



512
513
514
# File 'lib/syntax_tree/node.rb', line 512

def index
  @index
end

Instance Method Details

#accept(visitor) ⇒ Object



524
525
526
# File 'lib/syntax_tree/node.rb', line 524

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

#child_nodesObject Also known as: deconstruct



528
529
530
# File 'lib/syntax_tree/node.rb', line 528

def child_nodes
  [collection, index]
end

#deconstruct_keys(_keys) ⇒ Object



534
535
536
537
538
539
540
541
# File 'lib/syntax_tree/node.rb', line 534

def deconstruct_keys(_keys)
  {
    collection: collection,
    index: index,
    location: location,
    comments: comments
  }
end

#format(q) ⇒ Object



543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
# File 'lib/syntax_tree/node.rb', line 543

def format(q)
  q.group do
    q.format(collection)
    q.text("[")

    if index
      q.indent do
        q.breakable_empty
        q.format(index)
      end
      q.breakable_empty
    end

    q.text("]")
  end
end