Class: SyntaxTree::ARef

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

Overview

ARef represents when you’re pulling a value out of a collection at a specific index. Put another way, it’s any time you’re calling the method #[].

collection[index]

The nodes usually contains two children, the collection and the index. In some cases, you don’t necessarily have the second child node, because you can call procs with a pretty esoteric syntax. In the following example, you wouldn’t have a second child node:

collection[]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of ARef.



804
805
806
807
808
809
# File 'lib/syntax_tree.rb', line 804

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



793
794
795
# File 'lib/syntax_tree.rb', line 793

def collection
  @collection
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



802
803
804
# File 'lib/syntax_tree.rb', line 802

def comments
  @comments
end

#indexObject (readonly)

nil | Args

the value being passed within the brackets



796
797
798
# File 'lib/syntax_tree.rb', line 796

def index
  @index
end

#locationObject (readonly)

Location

the location of this node



799
800
801
# File 'lib/syntax_tree.rb', line 799

def location
  @location
end

Instance Method Details

#child_nodesObject



811
812
813
# File 'lib/syntax_tree.rb', line 811

def child_nodes
  [collection, index]
end

#format(q) ⇒ Object



815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
# File 'lib/syntax_tree.rb', line 815

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

#pretty_print(q) ⇒ Object



832
833
834
835
836
837
838
839
840
841
842
843
844
# File 'lib/syntax_tree.rb', line 832

def pretty_print(q)
  q.group(2, "(", ")") do
    q.text("aref")

    q.breakable
    q.pp(collection)

    q.breakable
    q.pp(index)

    q.pp(Comment::List.new(comments))
  end
end

#to_json(*opts) ⇒ Object



846
847
848
849
850
851
852
853
854
# File 'lib/syntax_tree.rb', line 846

def to_json(*opts)
  {
    type: :aref,
    collection: collection,
    index: index,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end