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.



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

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



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

def comments
  @comments
end

#constantObject (readonly)

Const

the constant being assigned



10658
10659
10660
# File 'lib/syntax_tree/node.rb', line 10658

def constant
  @constant
end

Instance Method Details

#===(other) ⇒ Object



10699
10700
10701
# File 'lib/syntax_tree/node.rb', line 10699

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

#accept(visitor) ⇒ Object



10669
10670
10671
# File 'lib/syntax_tree/node.rb', line 10669

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

#child_nodesObject Also known as: deconstruct



10673
10674
10675
# File 'lib/syntax_tree/node.rb', line 10673

def child_nodes
  [constant]
end

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



10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
# File 'lib/syntax_tree/node.rb', line 10677

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



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

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

#format(q) ⇒ Object



10694
10695
10696
10697
# File 'lib/syntax_tree/node.rb', line 10694

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