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, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid

Constructor Details

#initialize(value:, location:) ⇒ VarField

Returns a new instance of VarField.



11512
11513
11514
11515
11516
# File 'lib/syntax_tree/node.rb', line 11512

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



11510
11511
11512
# File 'lib/syntax_tree/node.rb', line 11510

def comments
  @comments
end

#valueObject (readonly)

nil | :nil | Const | CVar | GVar | Ident | IVar

the target of this node



11507
11508
11509
# File 'lib/syntax_tree/node.rb', line 11507

def value
  @value
end

Instance Method Details

#===(other) ⇒ Object



11551
11552
11553
# File 'lib/syntax_tree/node.rb', line 11551

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

#accept(visitor) ⇒ Object



11518
11519
11520
# File 'lib/syntax_tree/node.rb', line 11518

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

#child_nodesObject Also known as: deconstruct



11522
11523
11524
# File 'lib/syntax_tree/node.rb', line 11522

def child_nodes
  [value]
end

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



11526
11527
11528
11529
11530
11531
11532
11533
11534
11535
# File 'lib/syntax_tree/node.rb', line 11526

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



11539
11540
11541
# File 'lib/syntax_tree/node.rb', line 11539

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

#format(q) ⇒ Object



11543
11544
11545
11546
11547
11548
11549
# File 'lib/syntax_tree/node.rb', line 11543

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