Class: SyntaxTree::Assign

Inherits:
Node
  • Object
show all
Defined in:
lib/syntax_tree/node.rb

Overview

Assign represents assigning something to a variable or constant. Generally, the left side of the assignment is going to be any node that ends with the name “Field”.

variable = value

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(target:, value:, location:) ⇒ Assign

Returns a new instance of Assign.



1460
1461
1462
1463
1464
1465
# File 'lib/syntax_tree/node.rb', line 1460

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



1458
1459
1460
# File 'lib/syntax_tree/node.rb', line 1458

def comments
  @comments
end

#targetObject (readonly)

ARefField | ConstPathField | Field | TopConstField | VarField

the target

to assign the result of the expression to



1452
1453
1454
# File 'lib/syntax_tree/node.rb', line 1452

def target
  @target
end

#valueObject (readonly)

untyped

the expression to be assigned



1455
1456
1457
# File 'lib/syntax_tree/node.rb', line 1455

def value
  @value
end

Instance Method Details

#===(other) ⇒ Object



1510
1511
1512
# File 'lib/syntax_tree/node.rb', line 1510

def ===(other)
  other.is_a?(Assign) && target === other.target && value === other.value
end

#accept(visitor) ⇒ Object



1467
1468
1469
# File 'lib/syntax_tree/node.rb', line 1467

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

#child_nodesObject Also known as: deconstruct



1471
1472
1473
# File 'lib/syntax_tree/node.rb', line 1471

def child_nodes
  [target, value]
end

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



1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
# File 'lib/syntax_tree/node.rb', line 1475

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

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

#deconstruct_keys(_keys) ⇒ Object



1489
1490
1491
# File 'lib/syntax_tree/node.rb', line 1489

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

#format(q) ⇒ Object



1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
# File 'lib/syntax_tree/node.rb', line 1493

def format(q)
  q.group do
    q.format(target)
    q.text(" =")

    if skip_indent?
      q.text(" ")
      q.format(value)
    else
      q.indent do
        q.breakable_space
        q.format(value)
      end
    end
  end
end