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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of ARefField.



577
578
579
580
581
582
# File 'lib/syntax_tree/node.rb', line 577

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



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

def collection
  @collection
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



575
576
577
# File 'lib/syntax_tree/node.rb', line 575

def comments
  @comments
end

#indexObject (readonly)

nil | Args

the value being passed within the brackets



569
570
571
# File 'lib/syntax_tree/node.rb', line 569

def index
  @index
end

#locationObject (readonly)

Location

the location of this node



572
573
574
# File 'lib/syntax_tree/node.rb', line 572

def location
  @location
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



584
585
586
# File 'lib/syntax_tree/node.rb', line 584

def child_nodes
  [collection, index]
end

#deconstruct_keys(keys) ⇒ Object



590
591
592
593
594
595
596
597
# File 'lib/syntax_tree/node.rb', line 590

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

#format(q) ⇒ Object



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

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



616
617
618
619
620
621
622
623
624
625
626
627
628
# File 'lib/syntax_tree/node.rb', line 616

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



630
631
632
633
634
635
636
637
638
# File 'lib/syntax_tree/node.rb', line 630

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