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.



11481
11482
11483
11484
11485
# File 'lib/syntax_tree/node.rb', line 11481

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



11479
11480
11481
# File 'lib/syntax_tree/node.rb', line 11479

def comments
  @comments
end

#valueObject (readonly)

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

the target of this node



11476
11477
11478
# File 'lib/syntax_tree/node.rb', line 11476

def value
  @value
end

Instance Method Details

#===(other) ⇒ Object



11520
11521
11522
# File 'lib/syntax_tree/node.rb', line 11520

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

#accept(visitor) ⇒ Object



11487
11488
11489
# File 'lib/syntax_tree/node.rb', line 11487

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

#child_nodesObject Also known as: deconstruct



11491
11492
11493
# File 'lib/syntax_tree/node.rb', line 11491

def child_nodes
  [value]
end

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



11495
11496
11497
11498
11499
11500
11501
11502
11503
11504
# File 'lib/syntax_tree/node.rb', line 11495

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



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

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

#format(q) ⇒ Object



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

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