Class: SyntaxTree::VarField

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_tree.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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of VarField.



12805
12806
12807
12808
12809
# File 'lib/syntax_tree.rb', line 12805

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



12803
12804
12805
# File 'lib/syntax_tree.rb', line 12803

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



12800
12801
12802
# File 'lib/syntax_tree.rb', line 12800

def location
  @location
end

#valueObject (readonly)

nil | Const | CVar | GVar | Ident | IVar

the target of this node



12797
12798
12799
# File 'lib/syntax_tree.rb', line 12797

def value
  @value
end

Instance Method Details

#child_nodesObject



12811
12812
12813
# File 'lib/syntax_tree.rb', line 12811

def child_nodes
  [value]
end

#format(q) ⇒ Object



12815
12816
12817
# File 'lib/syntax_tree.rb', line 12815

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

#pretty_print(q) ⇒ Object



12819
12820
12821
12822
12823
12824
12825
12826
12827
12828
# File 'lib/syntax_tree.rb', line 12819

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



12830
12831
12832
12833
12834
# File 'lib/syntax_tree.rb', line 12830

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