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

#pretty_print, #to_json

Constructor Details

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

Returns a new instance of ARefField.



467
468
469
470
471
472
# File 'lib/syntax_tree/node.rb', line 467

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



459
460
461
# File 'lib/syntax_tree/node.rb', line 459

def collection
  @collection
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



465
466
467
# File 'lib/syntax_tree/node.rb', line 465

def comments
  @comments
end

#indexObject (readonly)

nil | Args

the value being passed within the brackets



462
463
464
# File 'lib/syntax_tree/node.rb', line 462

def index
  @index
end

Instance Method Details

#accept(visitor) ⇒ Object



474
475
476
# File 'lib/syntax_tree/node.rb', line 474

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

#child_nodesObject Also known as: deconstruct



478
479
480
# File 'lib/syntax_tree/node.rb', line 478

def child_nodes
  [collection, index]
end

#deconstruct_keys(keys) ⇒ Object



484
485
486
487
488
489
490
491
# File 'lib/syntax_tree/node.rb', line 484

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

#format(q) ⇒ Object



493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
# File 'lib/syntax_tree/node.rb', line 493

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

    if index
      q.indent do
        q.breakable("")
        q.format(index)
      end
      q.breakable("")
    end

    q.text("]")
  end
end