Class: SyntaxTree::Const

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

Overview

Const represents a literal value that looks like a constant. This could actually be a reference to a constant:

Constant

It could also be something that looks like a constant in another context, as in a method call to a capitalized method:

object.Constant

or a symbol that starts with a capital letter:

:Constant

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(value:, location:) ⇒ Const

Returns a new instance of Const.



3816
3817
3818
3819
3820
# File 'lib/syntax_tree/node.rb', line 3816

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



3814
3815
3816
# File 'lib/syntax_tree/node.rb', line 3814

def comments
  @comments
end

#valueObject (readonly)

String

the name of the constant



3811
3812
3813
# File 'lib/syntax_tree/node.rb', line 3811

def value
  @value
end

Instance Method Details

#===(other) ⇒ Object



3851
3852
3853
# File 'lib/syntax_tree/node.rb', line 3851

def ===(other)
  other.is_a?(Const) && value === other.value
end

#accept(visitor) ⇒ Object



3822
3823
3824
# File 'lib/syntax_tree/node.rb', line 3822

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

#child_nodesObject Also known as: deconstruct



3826
3827
3828
# File 'lib/syntax_tree/node.rb', line 3826

def child_nodes
  []
end

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



3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
# File 'lib/syntax_tree/node.rb', line 3830

def copy(value: nil, location: nil)
  node =
    Const.new(
      value: value || self.value,
      location: location || self.location
    )

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

#deconstruct_keys(_keys) ⇒ Object



3843
3844
3845
# File 'lib/syntax_tree/node.rb', line 3843

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

#format(q) ⇒ Object



3847
3848
3849
# File 'lib/syntax_tree/node.rb', line 3847

def format(q)
  q.text(value)
end