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

Constructor Details

#initialize(constant:, location:) ⇒ TopConstField

Returns a new instance of TopConstField.



10654
10655
10656
10657
10658
# File 'lib/syntax_tree/node.rb', line 10654

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



10652
10653
10654
# File 'lib/syntax_tree/node.rb', line 10652

def comments
  @comments
end

#constantObject (readonly)

Const

the constant being assigned



10649
10650
10651
# File 'lib/syntax_tree/node.rb', line 10649

def constant
  @constant
end

Instance Method Details

#===(other) ⇒ Object



10690
10691
10692
# File 'lib/syntax_tree/node.rb', line 10690

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

#accept(visitor) ⇒ Object



10660
10661
10662
# File 'lib/syntax_tree/node.rb', line 10660

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

#child_nodesObject Also known as: deconstruct



10664
10665
10666
# File 'lib/syntax_tree/node.rb', line 10664

def child_nodes
  [constant]
end

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



10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
# File 'lib/syntax_tree/node.rb', line 10668

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



10681
10682
10683
# File 'lib/syntax_tree/node.rb', line 10681

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

#format(q) ⇒ Object



10685
10686
10687
10688
# File 'lib/syntax_tree/node.rb', line 10685

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