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

Constructor Details

#initialize(constant:, location:, comments: []) ⇒ TopConstField

Returns a new instance of TopConstField.



9762
9763
9764
9765
9766
# File 'lib/syntax_tree/node.rb', line 9762

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



9760
9761
9762
# File 'lib/syntax_tree/node.rb', line 9760

def comments
  @comments
end

#constantObject (readonly)

Const

the constant being assigned



9757
9758
9759
# File 'lib/syntax_tree/node.rb', line 9757

def constant
  @constant
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



9768
9769
9770
# File 'lib/syntax_tree/node.rb', line 9768

def child_nodes
  [constant]
end

#deconstruct_keys(keys) ⇒ Object



9774
9775
9776
# File 'lib/syntax_tree/node.rb', line 9774

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

#format(q) ⇒ Object



9778
9779
9780
9781
# File 'lib/syntax_tree/node.rb', line 9778

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

#pretty_print(q) ⇒ Object



9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
# File 'lib/syntax_tree/node.rb', line 9783

def pretty_print(q)
  q.group(2, "(", ")") do
    q.text("top_const_field")

    q.breakable
    q.pp(constant)

    q.pp(Comment::List.new(comments))
  end
end

#to_json(*opts) ⇒ Object



9794
9795
9796
9797
9798
9799
9800
9801
# File 'lib/syntax_tree/node.rb', line 9794

def to_json(*opts)
  {
    type: :top_const_field,
    constant: constant,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end