Class: SyntaxTree::VarRef

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

Overview

VarRef represents a variable reference.

true

This can be a plain local variable like the example above. It can also be a constant, a class variable, a global variable, an instance variable, a keyword (like self, nil, true, or false), or a numbered block variable.

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#construct_keys, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid

Constructor Details

#initialize(value:, location:) ⇒ VarRef

Returns a new instance of VarRef.



11603
11604
11605
11606
11607
# File 'lib/syntax_tree/node.rb', line 11603

def initialize(value:, location:)
  @value = value
  @location = location
  @comments = []
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



11601
11602
11603
# File 'lib/syntax_tree/node.rb', line 11601

def comments
  @comments
end

#valueObject (readonly)

Const | CVar | GVar | Ident | IVar | Kw

the value of this node



11598
11599
11600
# File 'lib/syntax_tree/node.rb', line 11598

def value
  @value
end

Instance Method Details

#===(other) ⇒ Object



11638
11639
11640
# File 'lib/syntax_tree/node.rb', line 11638

def ===(other)
  other.is_a?(VarRef) && value === other.value
end

#accept(visitor) ⇒ Object



11609
11610
11611
# File 'lib/syntax_tree/node.rb', line 11609

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

#child_nodesObject Also known as: deconstruct



11613
11614
11615
# File 'lib/syntax_tree/node.rb', line 11613

def child_nodes
  [value]
end

#copy(value: nil, location: nil) ⇒ Object



11617
11618
11619
11620
11621
11622
11623
11624
11625
11626
# File 'lib/syntax_tree/node.rb', line 11617

def copy(value: nil, location: nil)
  node =
    VarRef.new(
      value: value || self.value,
      location: location || self.location
    )

  node.comments.concat(comments.map(&:copy))
  node
end

#deconstruct_keys(_keys) ⇒ Object



11630
11631
11632
# File 'lib/syntax_tree/node.rb', line 11630

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

#format(q) ⇒ Object



11634
11635
11636
# File 'lib/syntax_tree/node.rb', line 11634

def format(q)
  q.format(value)
end

#pin(parent, pin) ⇒ Object

Oh man I hate this so much. Basically, ripper doesn’t provide enough functionality to actually know where pins are within an expression. So we have to walk the tree ourselves and insert more information. In doing so, we have to replace this node by a pinned node when necessary.

To be clear, this method should just not exist. It’s not good. It’s a place of shame. But it’s necessary for now, so I’m keeping it.



11649
11650
11651
11652
11653
11654
11655
11656
11657
11658
11659
11660
11661
11662
11663
11664
11665
11666
11667
11668
# File 'lib/syntax_tree/node.rb', line 11649

def pin(parent, pin)
  replace =
    PinnedVarRef.new(value: value, location: pin.location.to(location))

  parent
    .deconstruct_keys([])
    .each do |key, value|
      if value == self
        parent.instance_variable_set(:"@#{key}", replace)
        break
      elsif value.is_a?(Array) && (index = value.index(self))
        parent.public_send(key)[index] = replace
        break
      elsif value.is_a?(Array) &&
            (index = value.index { |(_k, v)| v == self })
        parent.public_send(key)[index][1] = replace
        break
      end
    end
end