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, #pretty_print, #to_json

Constructor Details

#initialize(value:, location:, comments: []) ⇒ VarRef

Returns a new instance of VarRef.



9769
9770
9771
9772
9773
# File 'lib/syntax_tree/node.rb', line 9769

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



9767
9768
9769
# File 'lib/syntax_tree/node.rb', line 9767

def comments
  @comments
end

#valueObject (readonly)

Const | CVar | GVar | Ident | IVar | Kw

the value of this node



9764
9765
9766
# File 'lib/syntax_tree/node.rb', line 9764

def value
  @value
end

Instance Method Details

#accept(visitor) ⇒ Object



9775
9776
9777
# File 'lib/syntax_tree/node.rb', line 9775

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

#child_nodesObject Also known as: deconstruct



9779
9780
9781
# File 'lib/syntax_tree/node.rb', line 9779

def child_nodes
  [value]
end

#deconstruct_keys(_keys) ⇒ Object



9785
9786
9787
# File 'lib/syntax_tree/node.rb', line 9785

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

#format(q) ⇒ Object



9789
9790
9791
# File 'lib/syntax_tree/node.rb', line 9789

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

#pin(parent) ⇒ 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.



9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
# File 'lib/syntax_tree/node.rb', line 9800

def pin(parent)
  replace = PinnedVarRef.new(value: value, location: 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
      end
    end
end