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

Constructor Details

#initialize(target:, value:, location:) ⇒ Assign

Returns a new instance of Assign.



1429
1430
1431
1432
1433
1434
# File 'lib/syntax_tree/node.rb', line 1429

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



1427
1428
1429
# File 'lib/syntax_tree/node.rb', line 1427

def comments
  @comments
end

#targetObject (readonly)

ARefField | ConstPathField | Field | TopConstField | VarField

the target

to assign the result of the expression to



1421
1422
1423
# File 'lib/syntax_tree/node.rb', line 1421

def target
  @target
end

#valueObject (readonly)

Node

the expression to be assigned



1424
1425
1426
# File 'lib/syntax_tree/node.rb', line 1424

def value
  @value
end

Instance Method Details

#===(other) ⇒ Object



1479
1480
1481
# File 'lib/syntax_tree/node.rb', line 1479

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

#accept(visitor) ⇒ Object



1436
1437
1438
# File 'lib/syntax_tree/node.rb', line 1436

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

#child_nodesObject Also known as: deconstruct



1440
1441
1442
# File 'lib/syntax_tree/node.rb', line 1440

def child_nodes
  [target, value]
end

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



1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
# File 'lib/syntax_tree/node.rb', line 1444

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



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

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

#format(q) ⇒ Object



1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
# File 'lib/syntax_tree/node.rb', line 1462

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