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.



1795
1796
1797
1798
1799
1800
# File 'lib/syntax_tree.rb', line 1795

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



1793
1794
1795
# File 'lib/syntax_tree.rb', line 1793

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



1790
1791
1792
# File 'lib/syntax_tree.rb', line 1790

def location
  @location
end

#targetObject (readonly)

ARefField | ConstPathField | Field | TopConstField | VarField

the target

to assign the result of the expression to



1784
1785
1786
# File 'lib/syntax_tree.rb', line 1784

def target
  @target
end

#valueObject (readonly)

untyped

the expression to be assigned



1787
1788
1789
# File 'lib/syntax_tree.rb', line 1787

def value
  @value
end

Instance Method Details

#child_nodesObject



1802
1803
1804
# File 'lib/syntax_tree.rb', line 1802

def child_nodes
  [target, value]
end

#format(q) ⇒ Object



1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
# File 'lib/syntax_tree.rb', line 1806

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



1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
# File 'lib/syntax_tree.rb', line 1823

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



1837
1838
1839
1840
1841
1842
1843
1844
1845
# File 'lib/syntax_tree.rb', line 1837

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