Class: SyntaxTree::Label

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

Overview

Label represents the use of an identifier to associate with an object. You can find it in a hash key, as in:

{ key: value }

In this case “key:” would be the body of the label. You can also find it in pattern matching, as in:

case value
in key:
end

In this case “key:” would be the body of the label.

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Label.



6541
6542
6543
6544
6545
# File 'lib/syntax_tree/node.rb', line 6541

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



6539
6540
6541
# File 'lib/syntax_tree/node.rb', line 6539

def comments
  @comments
end

#valueObject (readonly)

String

the value of the label



6536
6537
6538
# File 'lib/syntax_tree/node.rb', line 6536

def value
  @value
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



6547
6548
6549
# File 'lib/syntax_tree/node.rb', line 6547

def child_nodes
  []
end

#deconstruct_keys(keys) ⇒ Object



6553
6554
6555
# File 'lib/syntax_tree/node.rb', line 6553

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

#format(q) ⇒ Object



6557
6558
6559
# File 'lib/syntax_tree/node.rb', line 6557

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

#pretty_print(q) ⇒ Object



6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
# File 'lib/syntax_tree/node.rb', line 6561

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

    q.breakable
    q.text(":")
    q.text(value[0...-1])

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

#to_json(*opts) ⇒ Object



6573
6574
6575
6576
6577
# File 'lib/syntax_tree/node.rb', line 6573

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