Class: SyntaxTree::ConstPathField

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

Overview

ConstPathField represents the child node of some kind of assignment. It represents when you’re assigning to a constant that is being referenced as a child of another variable.

object::Const = value

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(parent:, constant:, location:) ⇒ ConstPathField

Returns a new instance of ConstPathField.



3872
3873
3874
3875
3876
3877
# File 'lib/syntax_tree/node.rb', line 3872

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



3870
3871
3872
# File 'lib/syntax_tree/node.rb', line 3870

def comments
  @comments
end

#constantObject (readonly)

Const

the constant itself



3867
3868
3869
# File 'lib/syntax_tree/node.rb', line 3867

def constant
  @constant
end

#parentObject (readonly)

Node

the source of the constant



3864
3865
3866
# File 'lib/syntax_tree/node.rb', line 3864

def parent
  @parent
end

Instance Method Details

#===(other) ⇒ Object



3916
3917
3918
3919
# File 'lib/syntax_tree/node.rb', line 3916

def ===(other)
  other.is_a?(ConstPathField) && parent === other.parent &&
    constant === other.constant
end

#accept(visitor) ⇒ Object



3879
3880
3881
# File 'lib/syntax_tree/node.rb', line 3879

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

#child_nodesObject Also known as: deconstruct



3883
3884
3885
# File 'lib/syntax_tree/node.rb', line 3883

def child_nodes
  [parent, constant]
end

#copy(parent: nil, constant: nil, location: nil) ⇒ Object



3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
# File 'lib/syntax_tree/node.rb', line 3887

def copy(parent: nil, constant: nil, location: nil)
  node =
    ConstPathField.new(
      parent: parent || self.parent,
      constant: constant || self.constant,
      location: location || self.location
    )

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

#deconstruct_keys(_keys) ⇒ Object



3901
3902
3903
3904
3905
3906
3907
3908
# File 'lib/syntax_tree/node.rb', line 3901

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

#format(q) ⇒ Object



3910
3911
3912
3913
3914
# File 'lib/syntax_tree/node.rb', line 3910

def format(q)
  q.format(parent)
  q.text("::")
  q.format(constant)
end