Class: SyntaxTree::Assign

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_tree.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

Instance Method Summary collapse

Constructor Details

#initialize(target:, value:, location:, comments: []) ⇒ Assign

Returns a new instance of Assign.



1665
1666
1667
1668
1669
1670
# File 'lib/syntax_tree.rb', line 1665

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



1663
1664
1665
# File 'lib/syntax_tree.rb', line 1663

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



1660
1661
1662
# File 'lib/syntax_tree.rb', line 1660

def location
  @location
end

#targetObject (readonly)

ARefField | ConstPathField | Field | TopConstField | VarField

the target

to assign the result of the expression to



1654
1655
1656
# File 'lib/syntax_tree.rb', line 1654

def target
  @target
end

#valueObject (readonly)

untyped

the expression to be assigned



1657
1658
1659
# File 'lib/syntax_tree.rb', line 1657

def value
  @value
end

Instance Method Details

#child_nodesObject



1672
1673
1674
# File 'lib/syntax_tree.rb', line 1672

def child_nodes
  [target, value]
end

#format(q) ⇒ Object



1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
# File 'lib/syntax_tree.rb', line 1676

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
        q.format(value)
      end
    end
  end
end

#pretty_print(q) ⇒ Object



1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
# File 'lib/syntax_tree.rb', line 1693

def pretty_print(q)
  q.group(2, "(", ")") do
    q.text("assign")

    q.breakable
    q.pp(target)

    q.breakable
    q.pp(value)

    q.pp(Comment::List.new(comments))
  end
end

#to_json(*opts) ⇒ Object



1707
1708
1709
1710
1711
1712
1713
1714
1715
# File 'lib/syntax_tree.rb', line 1707

def to_json(*opts)
  {
    type: :assign,
    target: target,
    value: value,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end