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

Constructor Details

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

Returns a new instance of Const.



3414
3415
3416
3417
3418
# File 'lib/syntax_tree/node.rb', line 3414

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



3412
3413
3414
# File 'lib/syntax_tree/node.rb', line 3412

def comments
  @comments
end

#valueObject (readonly)

String

the name of the constant



3409
3410
3411
# File 'lib/syntax_tree/node.rb', line 3409

def value
  @value
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



3420
3421
3422
# File 'lib/syntax_tree/node.rb', line 3420

def child_nodes
  []
end

#deconstruct_keys(keys) ⇒ Object



3426
3427
3428
# File 'lib/syntax_tree/node.rb', line 3426

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

#format(q) ⇒ Object



3430
3431
3432
# File 'lib/syntax_tree/node.rb', line 3430

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

#pretty_print(q) ⇒ Object



3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
# File 'lib/syntax_tree/node.rb', line 3434

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



3445
3446
3447
3448
3449
# File 'lib/syntax_tree/node.rb', line 3445

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