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.



855
856
857
858
859
860
# File 'lib/syntax_tree.rb', line 855

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



844
845
846
# File 'lib/syntax_tree.rb', line 844

def collection
  @collection
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



853
854
855
# File 'lib/syntax_tree.rb', line 853

def comments
  @comments
end

#indexObject (readonly)

nil | Args

the value being passed within the brackets



847
848
849
# File 'lib/syntax_tree.rb', line 847

def index
  @index
end

#locationObject (readonly)

Location

the location of this node



850
851
852
# File 'lib/syntax_tree.rb', line 850

def location
  @location
end

Instance Method Details

#child_nodesObject



862
863
864
# File 'lib/syntax_tree.rb', line 862

def child_nodes
  [collection, index]
end

#format(q) ⇒ Object



866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
# File 'lib/syntax_tree.rb', line 866

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



883
884
885
886
887
888
889
890
891
892
893
894
895
# File 'lib/syntax_tree.rb', line 883

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



897
898
899
900
901
902
903
904
905
# File 'lib/syntax_tree.rb', line 897

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