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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of VarField.



11037
11038
11039
11040
11041
# File 'lib/syntax_tree/node.rb', line 11037

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



11035
11036
11037
# File 'lib/syntax_tree/node.rb', line 11035

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



11032
11033
11034
# File 'lib/syntax_tree/node.rb', line 11032

def location
  @location
end

#valueObject (readonly)

nil | Const | CVar | GVar | Ident | IVar

the target of this node



11029
11030
11031
# File 'lib/syntax_tree/node.rb', line 11029

def value
  @value
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



11043
11044
11045
# File 'lib/syntax_tree/node.rb', line 11043

def child_nodes
  [value]
end

#deconstruct_keys(keys) ⇒ Object



11049
11050
11051
# File 'lib/syntax_tree/node.rb', line 11049

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

#format(q) ⇒ Object



11053
11054
11055
# File 'lib/syntax_tree/node.rb', line 11053

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

#pretty_print(q) ⇒ Object



11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
# File 'lib/syntax_tree/node.rb', line 11057

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



11068
11069
11070
11071
11072
# File 'lib/syntax_tree/node.rb', line 11068

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