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.



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

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



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

def comments
  @comments
end

#targetObject (readonly)

ARefField | ConstPathField | Field | TopConstField | VarField

the target

to assign the result of the expression to



1417
1418
1419
# File 'lib/syntax_tree/node.rb', line 1417

def target
  @target
end

#valueObject (readonly)

Node

the expression to be assigned



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

def value
  @value
end

Instance Method Details

#===(other) ⇒ Object



1475
1476
1477
# File 'lib/syntax_tree/node.rb', line 1475

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

#accept(visitor) ⇒ Object



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

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

#child_nodesObject Also known as: deconstruct



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

def child_nodes
  [target, value]
end

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



1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
# File 'lib/syntax_tree/node.rb', line 1440

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



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

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

#format(q) ⇒ Object



1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
# File 'lib/syntax_tree/node.rb', line 1458

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