Class: SyntaxTree::VarField

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

Overview

VarField represents a variable that is being assigned a value. As such, it is always a child of an assignment type node.

variable = value

In the example above, the VarField node represents the variable token.

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of VarField.



10605
10606
10607
10608
10609
# File 'lib/syntax_tree/node.rb', line 10605

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



10603
10604
10605
# File 'lib/syntax_tree/node.rb', line 10603

def comments
  @comments
end

#valueObject (readonly)

nil | Const | CVar | GVar | Ident | IVar

the target of this node



10600
10601
10602
# File 'lib/syntax_tree/node.rb', line 10600

def value
  @value
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



10611
10612
10613
# File 'lib/syntax_tree/node.rb', line 10611

def child_nodes
  [value]
end

#deconstruct_keys(keys) ⇒ Object



10617
10618
10619
# File 'lib/syntax_tree/node.rb', line 10617

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

#format(q) ⇒ Object



10621
10622
10623
# File 'lib/syntax_tree/node.rb', line 10621

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

#pretty_print(q) ⇒ Object



10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
# File 'lib/syntax_tree/node.rb', line 10625

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

    q.breakable
    q.pp(value)

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

#to_json(*opts) ⇒ Object



10636
10637
10638
10639
10640
# File 'lib/syntax_tree/node.rb', line 10636

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