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

Instance Method Summary collapse

Constructor Details

#initialize(value:, location:, comments: []) ⇒ Const

Returns a new instance of Const.



3522
3523
3524
3525
3526
# File 'lib/syntax_tree/node.rb', line 3522

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



3520
3521
3522
# File 'lib/syntax_tree/node.rb', line 3520

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



3517
3518
3519
# File 'lib/syntax_tree/node.rb', line 3517

def location
  @location
end

#valueObject (readonly)

String

the name of the constant



3514
3515
3516
# File 'lib/syntax_tree/node.rb', line 3514

def value
  @value
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



3528
3529
3530
# File 'lib/syntax_tree/node.rb', line 3528

def child_nodes
  []
end

#deconstruct_keys(keys) ⇒ Object



3534
3535
3536
# File 'lib/syntax_tree/node.rb', line 3534

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

#format(q) ⇒ Object



3538
3539
3540
# File 'lib/syntax_tree/node.rb', line 3538

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

#pretty_print(q) ⇒ Object



3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
# File 'lib/syntax_tree/node.rb', line 3542

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

    q.breakable
    q.pp(value)

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

#to_json(*opts) ⇒ Object



3553
3554
3555
3556
3557
# File 'lib/syntax_tree/node.rb', line 3553

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