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:) ⇒ ARefField

Returns a new instance of ARefField.



645
646
647
648
649
650
# File 'lib/syntax_tree/node.rb', line 645

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

Instance Attribute Details

#collectionObject (readonly)

untyped

the value being indexed



637
638
639
# File 'lib/syntax_tree/node.rb', line 637

def collection
  @collection
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



643
644
645
# File 'lib/syntax_tree/node.rb', line 643

def comments
  @comments
end

#indexObject (readonly)

nil | Args

the value being passed within the brackets



640
641
642
# File 'lib/syntax_tree/node.rb', line 640

def index
  @index
end

Instance Method Details

#===(other) ⇒ Object



700
701
702
703
# File 'lib/syntax_tree/node.rb', line 700

def ===(other)
  other.is_a?(ARefField) && collection === other.collection &&
    index === other.index
end

#accept(visitor) ⇒ Object



652
653
654
# File 'lib/syntax_tree/node.rb', line 652

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

#child_nodesObject Also known as: deconstruct



656
657
658
# File 'lib/syntax_tree/node.rb', line 656

def child_nodes
  [collection, index]
end

#copy(collection: nil, index: nil, location: nil) ⇒ Object



660
661
662
663
664
665
666
667
668
669
670
# File 'lib/syntax_tree/node.rb', line 660

def copy(collection: nil, index: nil, location: nil)
  node =
    ARefField.new(
      collection: collection || self.collection,
      index: index || self.index,
      location: location || self.location
    )

  node.comments.concat(comments.map(&:copy))
  node
end

#deconstruct_keys(_keys) ⇒ Object



674
675
676
677
678
679
680
681
# File 'lib/syntax_tree/node.rb', line 674

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

#format(q) ⇒ Object



683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
# File 'lib/syntax_tree/node.rb', line 683

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