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.



12189
12190
12191
12192
12193
# File 'lib/syntax_tree.rb', line 12189

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



12187
12188
12189
# File 'lib/syntax_tree.rb', line 12187

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



12184
12185
12186
# File 'lib/syntax_tree.rb', line 12184

def location
  @location
end

#valueObject (readonly)

nil | Const | CVar | GVar | Ident | IVar

the target of this node



12181
12182
12183
# File 'lib/syntax_tree.rb', line 12181

def value
  @value
end

Instance Method Details

#child_nodesObject



12195
12196
12197
# File 'lib/syntax_tree.rb', line 12195

def child_nodes
  [value]
end

#format(q) ⇒ Object



12199
12200
12201
# File 'lib/syntax_tree.rb', line 12199

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

#pretty_print(q) ⇒ Object



12203
12204
12205
12206
12207
12208
12209
12210
12211
12212
# File 'lib/syntax_tree.rb', line 12203

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



12214
12215
12216
12217
12218
# File 'lib/syntax_tree.rb', line 12214

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