Class: SyntaxTree::TopConstField

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

Overview

TopConstField is always the child node of some kind of assignment. It represents when you’re assigning to a constant that is being referenced at the top level.

::Constant = 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(constant:, location:) ⇒ TopConstField

Returns a new instance of TopConstField.



10845
10846
10847
10848
10849
# File 'lib/syntax_tree/node.rb', line 10845

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



10843
10844
10845
# File 'lib/syntax_tree/node.rb', line 10843

def comments
  @comments
end

#constantObject (readonly)

Const

the constant being assigned



10840
10841
10842
# File 'lib/syntax_tree/node.rb', line 10840

def constant
  @constant
end

Instance Method Details

#===(other) ⇒ Object



10881
10882
10883
# File 'lib/syntax_tree/node.rb', line 10881

def ===(other)
  other.is_a?(TopConstField) && constant === other.constant
end

#accept(visitor) ⇒ Object



10851
10852
10853
# File 'lib/syntax_tree/node.rb', line 10851

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

#child_nodesObject Also known as: deconstruct



10855
10856
10857
# File 'lib/syntax_tree/node.rb', line 10855

def child_nodes
  [constant]
end

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



10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
# File 'lib/syntax_tree/node.rb', line 10859

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

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

#deconstruct_keys(_keys) ⇒ Object



10872
10873
10874
# File 'lib/syntax_tree/node.rb', line 10872

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

#format(q) ⇒ Object



10876
10877
10878
10879
# File 'lib/syntax_tree/node.rb', line 10876

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