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.



10782
10783
10784
10785
10786
# File 'lib/syntax_tree/node.rb', line 10782

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



10780
10781
10782
# File 'lib/syntax_tree/node.rb', line 10780

def comments
  @comments
end

#constantObject (readonly)

Const

the constant being assigned



10777
10778
10779
# File 'lib/syntax_tree/node.rb', line 10777

def constant
  @constant
end

Instance Method Details

#===(other) ⇒ Object



10818
10819
10820
# File 'lib/syntax_tree/node.rb', line 10818

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

#accept(visitor) ⇒ Object



10788
10789
10790
# File 'lib/syntax_tree/node.rb', line 10788

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

#child_nodesObject Also known as: deconstruct



10792
10793
10794
# File 'lib/syntax_tree/node.rb', line 10792

def child_nodes
  [constant]
end

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



10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
# File 'lib/syntax_tree/node.rb', line 10796

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



10809
10810
10811
# File 'lib/syntax_tree/node.rb', line 10809

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

#format(q) ⇒ Object



10813
10814
10815
10816
# File 'lib/syntax_tree/node.rb', line 10813

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