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

Constructor Details

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

Returns a new instance of ARefField.



559
560
561
562
563
564
# File 'lib/syntax_tree/node.rb', line 559

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



551
552
553
# File 'lib/syntax_tree/node.rb', line 551

def collection
  @collection
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



557
558
559
# File 'lib/syntax_tree/node.rb', line 557

def comments
  @comments
end

#indexObject (readonly)

nil | Args

the value being passed within the brackets



554
555
556
# File 'lib/syntax_tree/node.rb', line 554

def index
  @index
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



566
567
568
# File 'lib/syntax_tree/node.rb', line 566

def child_nodes
  [collection, index]
end

#deconstruct_keys(keys) ⇒ Object



572
573
574
575
576
577
578
579
# File 'lib/syntax_tree/node.rb', line 572

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

#format(q) ⇒ Object



581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
# File 'lib/syntax_tree/node.rb', line 581

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



598
599
600
601
602
603
604
605
606
607
608
609
610
# File 'lib/syntax_tree/node.rb', line 598

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

    q.breakable
    q.pp(collection)

    q.breakable
    q.pp(index)

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

#to_json(*opts) ⇒ Object



612
613
614
615
616
617
618
619
620
# File 'lib/syntax_tree/node.rb', line 612

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