Class: SyntaxTree::ARef

Inherits:
Node
  • Object
show all
Defined in:
lib/syntax_tree/node.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

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:, comments: []) ⇒ ARef

Returns a new instance of ARef.



446
447
448
449
450
451
# File 'lib/syntax_tree/node.rb', line 446

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



438
439
440
# File 'lib/syntax_tree/node.rb', line 438

def collection
  @collection
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



444
445
446
# File 'lib/syntax_tree/node.rb', line 444

def comments
  @comments
end

#indexObject (readonly)

nil | Args

the value being passed within the brackets



441
442
443
# File 'lib/syntax_tree/node.rb', line 441

def index
  @index
end

Instance Method Details

#accept(visitor) ⇒ Object



453
454
455
# File 'lib/syntax_tree/node.rb', line 453

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

#child_nodesObject Also known as: deconstruct



457
458
459
# File 'lib/syntax_tree/node.rb', line 457

def child_nodes
  [collection, index]
end

#deconstruct_keys(_keys) ⇒ Object



463
464
465
466
467
468
469
470
# File 'lib/syntax_tree/node.rb', line 463

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

#format(q) ⇒ Object



472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
# File 'lib/syntax_tree/node.rb', line 472

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