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

Methods inherited from Node

#construct_keys, #pretty_print, #to_json

Constructor Details

#initialize(value:, location:) ⇒ VarField

Returns a new instance of VarField.



11388
11389
11390
11391
11392
# File 'lib/syntax_tree/node.rb', line 11388

def initialize(value:, location:)
  @value = value
  @location = location
  @comments = []
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



11386
11387
11388
# File 'lib/syntax_tree/node.rb', line 11386

def comments
  @comments
end

#valueObject (readonly)

nil | Const | CVar | GVar | Ident | IVar

the target of this node



11383
11384
11385
# File 'lib/syntax_tree/node.rb', line 11383

def value
  @value
end

Instance Method Details

#===(other) ⇒ Object



11427
11428
11429
# File 'lib/syntax_tree/node.rb', line 11427

def ===(other)
  other.is_a?(VarField) && value === other.value
end

#accept(visitor) ⇒ Object



11394
11395
11396
# File 'lib/syntax_tree/node.rb', line 11394

def accept(visitor)
  visitor.visit_var_field(self)
end

#child_nodesObject Also known as: deconstruct



11398
11399
11400
# File 'lib/syntax_tree/node.rb', line 11398

def child_nodes
  [value]
end

#copy(value: nil, location: nil) ⇒ Object



11402
11403
11404
11405
11406
11407
11408
11409
11410
11411
# File 'lib/syntax_tree/node.rb', line 11402

def copy(value: nil, location: nil)
  node =
    VarField.new(
      value: value || self.value,
      location: location || self.location
    )

  node.comments.concat(comments.map(&:copy))
  node
end

#deconstruct_keys(_keys) ⇒ Object



11415
11416
11417
# File 'lib/syntax_tree/node.rb', line 11415

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

#format(q) ⇒ Object



11419
11420
11421
11422
11423
11424
11425
# File 'lib/syntax_tree/node.rb', line 11419

def format(q)
  if value == :nil
    q.text("nil")
  elsif value
    q.format(value)
  end
end